how to test if a particular control has focus?
Asked Answered
K

3

5

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 ?

Karlow answered 4/12, 2013 at 10:54 Comment(1)
Does Application.Caller work in Access?Ious
M
12

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.

Magel answered 4/12, 2013 at 11:46 Comment(0)
N
4

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)
Norine answered 15/12, 2021 at 16:5 Comment(0)
D
1

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.

Dilettante answered 16/6, 2020 at 12:7 Comment(2)
Is there a solution or workaround to this situation?Agora
Sorry, I don't know.Dilettante

© 2022 - 2024 — McMap. All rights reserved.