How do you know if a textbox has focus?

How do you know if a textbox has focus?
Who is Participating?
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
Right...either check the Name OR use the IS keyword...

The example below shows both versions:

    Dim ctl As Control
    Set ctl = Me.ActiveControl
    If Not (ctl Is Nothing) Then
        If ctl.Name = "Text1" Then
            Label1.Caption = "Text1 has focus"
        ElseIf ctl Is Text2 Then
            Label1.Caption = "Text2 has focus"
        End If
    End If

Note that we also check to make sure ActiveControl did not return Nothing.  This can occur if all forms are disabled or you have no controls that can currently have the focus.
Guy Hengel [angelIII / a3]Billing EngineerCommented:
if form's ActiveControl is the Textbox
Like angelIII said:

if Me.ActiveControl = then ...
Private Sub Text1_GotFocus()
Debug.Print "getfocus"
End Sub
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.