user1921704
user1921704

Reputation: 179

how to test if a particular control has focus?

i have access 2007 form and i want to test if a particular control (toggle button) has the focus ,

something like :

if gotfocus(mytoggle) then
dosomething
endif

or maybe like :

if me.mytoggle.setfocus = true then
dosomething
endif

I have searched and cannot find this , can someone tell me what is correct top to do this ?

Upvotes: 5

Views: 35548

Answers (3)

Azzaben
Azzaben

Reputation: 41

Try this code - I've tried to account for .ActiveControl not existing.

Private Function isCurrentControl(thisControl As Control) As Boolean
    On Error GoTo err_handler
    
        If Not Me.ActiveControl Is Nothing Then
            If (Me.ActiveControl Is thisControl) Then
                isCurrentControl = True
            Else
                isCurrentControl = False
            End If
        Else
            GoTo err_handler
        End If
  
close_function:
    On Error GoTo 0
    Exit Function
        
err_handler:
        isCurrentControl = False
        Resume close_function

End Function

You just need to call the function and set the control as a parameter

''EXAMPLE: isCurrentControl(mytoggle)

Upvotes: 4

Bughater
Bughater

Reputation: 73

Unfortunately, there are situations where the .ActiveControl is temporary non-existing ! When records are scrolled in a form, the procedure Form_Current() gets run. Already at the beginning, there is no focus anymore – the focus is reset to the previous field only after Form_Current() has terminated.

Upvotes: 1

jacouh
jacouh

Reputation: 8741

This for the current form:

If (mytoggle Is Me.ActiveControl) Then

This for the current Access.Application:

If (mytoggle Is Screen.ActiveControl) Then

Be careful, if no control has focus, *.ActiveControl may not exist.

Upvotes: 12

Related Questions