ACC: "Can't Hide a Control That Has the Focus" Error Message

Last reviewed: May 7, 1997
Article ID: Q88168
The information in this article applies to:
  • Microsoft Access versions 1.0, 1.1, 2.0, 7.0, 97

SUMMARY

Moderate: Requires basic macro, coding, and interoperability skills.

If you try to change the Visible property of a control to False while that control has the focus, you may receive the following error message:

   You can't hide a control that has the focus

You cannot set the Visible property of a control to False while that control has the focus. You must set the focus on another object first, and then you can change the Visible property.

MORE INFORMATION

The following example uses two command buttons to demonstrate how to generate the error message by trying to change the Visible property of a control that has the focus, and how to avoid the error by setting the focus elsewhere.

CAUTION: Following the steps in this example will modify the sample database Northwind.mdb (or NWIND.MDB in version 2.0 or earlier). You may want to back up the Northwind.mdb (or NWIND.MDB) file and perform these steps on a copy of the database.

  1. Open the sample database Northwind.mdb (or NWIND.MDB in versions 1.x and 2.0).

  2. Open the Customers form in Design view, and add the following two command buttons to the form:

          Form: Customers
          --------------------------------------------------------
          Command Button:
    
             Name: Button1
             Caption: Error
             OnClick (or OnPush in version 1.x): [Event Procedure]
          Command Button:
             Name: Button2
             Caption: Success
             OnClick: [Event Procedure]
    
    

  3. Set the OnClick property for Button1 to the following event procedure:

    NOTE: In Microsoft Access 1.x and 2.0, the word Private before Sub is omitted by default.

          Private Sub Button1_Click()
    
             On Error GoTo errhandler
                'Set the focus on the City control
                Me!City.Setfocus
                'Make the City control invisible
                Me!City.Visible=False
                Exit Sub
             errhandler:
                'Trap the Control Has Focus error
                If Err = 2165 Then
                   MsgBox "Can't Make Control Invisible"
                Else
                   MsgBox Err & " " & Err.Description
                End If
                Exit Sub
          End Sub
    
    

  4. Set the OnClick property for Button2 to the following event procedure:

          Private Sub Button2_Click()
    
             On Error GoTo errhandler
                'Set the focus on the Region control
                Me!Region.Setfocus
                'Make the City control invisible
                Me!City.Visible=False
                Exit Sub
             errhandler:
                'Trap the Control Has Focus error
                If Err = 2165 Then
                   MsgBox "Can't Make Control Invisible"
                Else
                   MsgBox Err & " " & Err.Description
                End If
                Exit Sub
          End Sub
    
    

  5. Open the Customers form in Form view. Click the Error button and note that you receive the message "Can't Make Control Invisible." Click the Success button and note that the City field disappears.


Keywords : kberrmsg kbusage McrActn
Version : 1.0 1.1 2.0 7.0 97
Platform : WINDOWS
Hardware : x86
Issue type : kbinfo


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: May 7, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.