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

ID: Q209832


The information in this article applies to:

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


SYMPTOMS

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.


CAUSE

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 in the "Symptoms" section and how to avoid the error. Note how the code for the first command button tries to change the Visible property of a control that has the focus. The code for the second command button avoids the error by first setting the focus elsewhere.

CAUTION: Following the steps in this example will modify the sample database Northwind.mdb. You may want to back up the Northwind.mdb file and perform these steps on a copy of the database.

  1. Open the sample database Northwind.mdb.


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


  3. 
       Form: Customers
       --------------------------
       Command Button:
       Name: Button1
       Caption: Error
       OnClick: [Event Procedure]
    
       Command Button:
       Name: Button2
       Caption: Success
       OnClick: [Event Procedure] 
  4. Set the OnClick property for Button1 to the following event procedure:


  5. 
    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
     
  6. Set the OnClick property for Button2 to the following event procedure:


  7. 
    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 
  8. 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.


Additional query words: prb


Keywords          : kberrmsg kbusage kbdta McrActn 
Version           : WINDOWS:2000
Platform          : WINDOWS 
Issue type        : kbprb 

Last Reviewed: May 13, 1999