You can't hide a control that has the focus

TheSearcher

Registered User.
Local time
Yesterday, 21:53
Joined
Jul 21, 2011
Messages
346
cmb_Client is an acComboBox - but shouldn't this code prevent me from getting the "You can't hide a control that has the focus" error?

Code:
For Each ctl In Me.Controls
    If ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox Then
        ctl.Value = Null
        cmb_Client.SetFocus
        ctl.Visible = False
    End If
Next ctl
 
The loop eventually arrives at cmb_Client. You then set focus to it, and hide it. Kaboom!
One idea is to create a 1x1 pixel command button, transparent, and set focus to it before entering the loop.
 
cmb_Client is an acComboBox - but shouldn't this code prevent me from getting the "You can't hide a control that has the focus" error?

Code:
For Each ctl In Me.Controls
    If ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox Then
        ctl.Value = Null
        cmb_Client.SetFocus
        ctl.Visible = False
    End If
Next ctl
Which version of Access are you using? I thought recent versions of Access allow hiding controls while still in focus?
 
DBGuy - Microsoft 365 MSO (Version2402 Build 16.0.17328.20648) 32 bit
 
DBGuy - Microsoft 365 MSO (Version2402 Build 16.0.17328.20648) 32 bit
Thanks. I'm using Build 2409 and what works for me is disabling the control first before hiding it.
 
Which version of Access are you using? I thought recent versions of Access allow hiding controls while still in focus?
Not yet . . . though this functionality has been requested.

However, there is a simple workaround. Disable the control and it can then be hidden without first moving the focus.
See my article:
 
Thanks everyone. It was easy to find a workaround. I'm just curious why my code in my original post didn't work. It seems to me that it should have worked.
 
I'm just curious why my code in my original post didn't work. It seems to me that it should have worked.

A better test:
Code:
' ...
    If (ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox) _
    And ctl.Name <> "cmb_client" Then
' ...
 

Users who are viewing this thread

Back
Top Bottom