[Last Call] Learn how to a build a cloud-first strategyRegister Now


How do you know if a textbox has focus?

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

Expert Comment

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

Expert Comment

ID: 16859459
Like angelIII said:

if Me.ActiveControl = yourtextbox.name then ...
LVL 26

Expert Comment

ID: 16860447
Private Sub Text1_GotFocus()
Debug.Print "getfocus"
End Sub
LVL 86

Accepted Solution

Mike Tomlinson earned 2000 total points
ID: 16860844
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses
Course of the Month18 days, 2 hours left to enroll

829 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