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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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!

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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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 Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

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 Access MVP)Database Architect / Systems AnalystCommented:
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.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.