First Published 24 Feb 2023                 Last Updated 29 Mar 2024

For as long as I can remember, I’ve always moved the focus to another control in order to disable the first control. This had always seemed completely logical to me

However a recent comment by pdanes in this thread at Access World Forums : Disable ALL controls on form led me to re-check this behaviour.

I can confirm that although that had to be done up to Access 2007, from Access 2010 onwards, it is no longer necessary to move the focus before disabling a control.

To test, add a single textbox to a form and add code either to the Form_Loadevent on even the textbox On_Click event to disable the control.

Magic! It is disabled despite originally having the focus! Clearly the focus is lost when disabled and goes to the form itself

You can also lock a control which has focus, although in this case, it retains the focus.
However, you still cannot hide a control that has the focus. Trying to do so causes error 3165

Error3165
A member of the Access team has told me that the change was originally made in relation to a specific issue with web databases, but it has had a wider impact.

Anyway, this leads to some interesting side effects. For example, you can now hide a control by clicking on it providing you first disable the control.

ClickMeDisappear

In this example, the form has a single textbox control with the following code on the On_Click event

Private Sub Text2_Click()

      'first disable the control then hide it
      Me.Text2.Enabled = False
      Me.Text2.Visible = False

End Sub



NOTE: If you instead use this code to first lock the control, it retains the focus so you will still get error 3165 as shown above.

Private Sub Text2_Click()

      'first lock the control then try to hide it
      Me.Text2.Locked = True
      'error 3165 occurs on the next line
      Me.Text2.Visible = False

End Sub



Video:

UPDATE 29 Mar 2024 - I have just created a short YouTube video (2:27) to accompany this article

You can watch the Click Me And I Disappear (Hide Control With Focus) video on my Isladogs YouTube channel or you can click below:



If you liked the video, please subscribe to my Isladogs on Access channel on YouTube. Thanks.



Conclusions

Overall, I think this is a positive change though I wish the behaviour was consistent for the .Visible property as well.

Perhaps Microsoft will make that additional change in the future as it shouldn't affect any existing Access apps.



Download

      Hide Control With Focus       Approx 0.35 MB (zipped)



Feedback

Please use the contact form below to let me know whether you found this article interesting/useful or if you have any questions/comments.

Please also consider making a donation towards the costs of maintaining this website. Thank you



Colin Riddington           Mendip Data Systems                 Last Updated 29 Mar 2024



Return to Access Blog Page




Return to Top