How to set focus on a form

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a main form with a combo box to select a year. In a subform I filter
using the year selected and disable and lock some fields in the past. If I
change the year in the mainform when the subform has focus, I sometimes get
the following error: "Run-Time error 2164": You can't disable a control while
it has the focus. Does not focus get set back to the mainform when I change
the combo box? How can I avoid this error?
 
What can be confusing is that there are separate "focus" situations in the
main form and the subform.

There is a "main focus" that is what a control has when you're working
directly in/on that control. However, whichever control you last "worked
with" in the main form will have the "focus" for the main form, and
whichever control you last "worked with" in the subform will have the
"focus" for the subform.

So the error message you're seeing likely is because a control that you want
to disable in the subform is the one that you last "worked with" in that
subform. You likely will need to reset the focus in the subform as part of
your code:

' These two steps move subform's focus to "ControlToGetFocus"
' control in the subform
Me.SubformName.SetFocus
Me.SubformName.Form.ControlToGetFocus.SetFocus
' This control puts the 'main focus' back to a control on the main form
Me.ControlOnMainForm.SetFocus
 
Back
Top