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   '<<<<<<<<
    Debug.print ctl.name
 
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.

tbsgadiCommented:
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!

Gary
0
TextReportCommented:
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
Forms!fsub8.Form!Text1

Example of lowest level control
Forms!fsub8.Form!fsub7.Form!fsub6.Form!fsub5.Form!fsub4.Form!fsub3.Form!fsub2.Form!fsub1.Form!Text0

Example of control in the middle of the nested subforms.
Forms!fsub8.Form!fsub7.Form!fsub6.Form!fsub5.Form!Text1

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
    Do
       Set ctrl = ctrl.Parent
       strFullAddress = ctrl.Name & ".Form!" & strFullAddress
    Loop Until Not booLoop
    
GetActiveControlAddress_Exit:
    GetActiveControlAddress = "Forms!" & strFullAddress
    Exit Function
    
GetActiveControlAddress_Error:
    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

0

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
LambertHeenanCommented:
Don't see how this is going to help...

    booLoop = True
    Do
       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.
0
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!

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

Cheers, Andrew
0
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.

mx

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

MX,

In on current of a subform, I will have:

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

Else

   Parent!txtCode = Me!txtSample

End IF

Thank you for asking the question.

Mike
0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Parent!txtCode = Me!txtNote  is correct.
0
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
0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
The condrol was disabled and that is why I had the problem.

Thanks.
0
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.