How do you know if a textbox has focus?

Posted on 2006-06-08
Last Modified: 2010-04-07
How do you know if a textbox has focus?
Question by:Hepen
    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    if form's ActiveControl is the Textbox
    LVL 53

    Expert Comment

    Like angelIII said:

    if Me.ActiveControl = then ...
    LVL 26

    Expert Comment

    Private Sub Text1_GotFocus()
    Debug.Print "getfocus"
    End Sub
    LVL 85

    Accepted Solution

    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.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
    This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now