We help IT Professionals succeed at work.

How to determine which control has focus

Mark
Mark asked
on
4,350 Views
Last Modified: 2013-11-28
How can I determine which control currently has focus on a form?
Comment
Watch Question

Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Commented:
For more ideas from the rest of the team you could check
https://www.experts-exchange.com/Microsoft/Development/MS_Access/Q_21927195.html
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Screen.ActiveControl.Name or Me.ActiveControl.Name both work -- sort of. They work on the main form. However, I have a subform. When I click a button, the button event requeries the subform to refresh its content. The subform's Form_Current function calls a subroutine on the parent form which may try to disable the very button I clicked -- error -- it has focus. The problem with the solutions givein is that parent form must have control othwise I get an error using these.

Suggestions?

Commented:
This is difficult, Buttons,  toolbars etc tend to "keep" the focus nomatter what you do from code. The solution I normally end up using is to let the button set a timer on the form and from the timer code set focus on some other control on the main form (me.cID.SetFocus and then run the tasks normally to be run from the button. This way the button no longer has the focus and can be disabled/hidden etc

Author

Commented:
Here's my solution: As I've said, the subform's form_Current() function calls a validation routine on the parent form. This routine, in turn, call a private sub: enableButtons(enable as boolean),  to enable or disable various buttons on the parent from. If one of those very buttons caused the subform form_current() to fire, I get an error if enableButton(False) is called. So, simple solution:

private sub enableButtons(enable as boolean)
    On Error Resume Next
    me.thisButton.enabled = enable
    me.thatButton.enabled = enable
      :
      :
exit sub

I'll split the point with dqmq and TOPIO since I'll use those ideas later and it's way too much work to do the "I've answered my own question" thing.

Thanks all

Commented:
This means then, if I read the code correctly, that the button will not be disabled like you wanted it to, you just igoner the error ?
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.