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?

[Webinar] Streamline your web hosting managementRegister Today

x
 
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
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
 
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
 
LambertHeenanConnect With a Mentor Commented:
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 new generation of project management tools

With monday.com’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.

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

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
All Courses

From novice to tech pro — start learning today.