Active control name in a subform...

The attaced code is not able to find the active control.

Q 1: How to find if the subform has the focus or the main form?
Q 2: How to find out what is the name of the active control?

Thank you.
'code in subform
Private Sub Form_Current()
    Dim ctl As Control
    ' Active control could be on the subform or the form
    Set ctl = Screen.ActiveControl   '<<<<<<<<
End Sub

Open in new window

LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

TextReportConnect With a Mentor Commented:
Hi Mike, I think the attached code will do that you are after. I have tested this with AutoKeys so when I press F11 it provides a msgbox of the full Forms reference to the control.
Hope this helps.
Cheers, Andrew

PS. when you are in a textbox on the main form it includes a .form but it still evaluates OK

Example of lowest level control

Example of control in the middle of the nested subforms.

Cheers, Andrew
Function GetActiveControlAddress() As String
On Error GoTo GetActiveControlAddress_Error
Dim strFullAddress As String
Dim ctrl ' As Control
Dim booLoop As Boolean
    Set ctrl = Screen.ActiveControl
    strFullAddress = ctrl.Name
    booLoop = True
       Set ctrl = ctrl.Parent
       strFullAddress = ctrl.Name & ".Form!" & strFullAddress
    Loop Until Not booLoop
    GetActiveControlAddress = "Forms!" & strFullAddress
    Exit Function
    Select Case Err
        Case 2452: Resume GetActiveControlAddress_Exit
        Case Else
             Select Case MsgBox("An Unexpected error has ocurred in GetActiveControlAddress" & vbCrLf & vbCrLf & "Error" & vbTab & "Description" & vbCrLf & Err & vbTab & Err.Description, vbCritical + vbRetryCancel)
                Case vbCancel: Resume GetActiveControlAddress_Exit
                Case vbRetry: Resume
             End Select
    End Select
End Function

Open in new window

Hi eghtebas,

1. You can set a global boolean parameter on the Subform_Enter & Subform_Exit Events
2. Try using that Code on a button Click using Screen.PreviousControl

Good Luck!

LambertHeenanConnect With a Mentor Commented:
Don't see how this is going to help...

    booLoop = True
       Set ctrl = ctrl.Parent
       strFullAddress = ctrl.Name & ".Form!" & strFullAddress
    Loop Until Not booLoop

That's an infinite loop. Nothing changed booLoop to False, to the exit condition is never met.
The new generation of project management tools

With’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

LambertHeenan true this is an infinite loop, however, eventually you will get an error 2452 and that resumes outside the loop.

Cheers, Andrew
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Connect With a Mentor Commented:
How *exactly* are you wanting to use Screen.ActiveControl ?

"How to find out what is the name of the active control?"

Screen.ActiveControl.Name will give you the name.


Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Thank you all for the feedback. I haven't started testing the solutions yet.


In on current of a subform, I will have:

IF <txtNot Has The focus or just entered> Then
   Parent!txtCode = Me!txtNot


   Parent!txtCode = Me!txtSample

End IF

Thank you for asking the question.

Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Parent!txtCode = Me!txtNote  is correct.
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Hi andrew,

I meant to accept your solution. I will add a post to reopen the question and regrade it.

Sorry about that.

Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
The condrol was disabled and that is why I had the problem.

All Courses

From novice to tech pro — start learning today.