Find which field has focus (1 Viewer)

modest

Registered User.
Local time
Today, 06:36
Joined
Jan 4, 2005
Messages
1,220
I have a button that has the ability to change two text boxes, depending on which one has focus.

Does anyone know how to verify if a conrol hasfocus?

Code:
If Me.ControlName1.hasFocus Then
    Me.ControlName1 = GetValue()
Else If Me.ControlName2.hasFocus Then
    Me.ControlName2 = GetValue()
Else
    Exit Sub
End If

The only way I know how to do this is to use a global variable and in the control's GotFocus/LostFocus event I should update the global variable with the control name or a null value.

But I am interested in seeing if there is a way to determine if a specific field has focus (if you know the name of the control), or if there is a way to get the name of whatever currently has focus (if you don't know the name of the control).
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 03:36
Joined
Aug 30, 2003
Messages
36,118
Does the ActiveControl Property serve your needs?
 

modest

Registered User.
Local time
Today, 06:36
Joined
Jan 4, 2005
Messages
1,220
Perfect, yes that is what I was looking for... completely forgot about it.

I was using the GotFocus/LostFocus method to store in a variable what had focus, but I'd rather not waste global resources, plus it demands more processing on the pc to call the function every time one of the textboxes gains or loses focus. I rather just check when I need it.

The Screen.ActiveControl worked beautifully.
 
Last edited:

Daveyk01

Registered User.
Local time
Today, 03:36
Joined
Jul 3, 2007
Messages
144
Why doesn't this work?:
Code:
For Each Ctl In Forms!frmMain.Controls
      If Ctl.ControlType = acCommandButton Or Ctl.ControlType = acTextBox Or Ctl.ControlType = acOptionGroup Or _
        Ctl.ControlType = acListBox Or Ctl.ControlType = acComboBox Or Ctl.ControlType = acCheckBox Then
          If  Ctl <> Screen.ActiveControl Then
            Ctl.Enabled = False
          End If
      End If
    Next Ctl
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 03:36
Joined
Aug 30, 2003
Messages
36,118
Try

If ctl.Name <> Screen.ActiveControl.Name Then
 

Daveyk01

Registered User.
Local time
Today, 03:36
Joined
Jul 3, 2007
Messages
144
Try

If ctl.Name <> Screen.ActiveControl.Name Then

I sometimes get an error "The expression you entered requires the control to be in the active window".

That is on program startup after I have a message box pop up and ask the user to turn on an instrument under test. Other times, your suggestion works perfect; thank you.

It must think that the msgbox is the active window at the time the routine is called, either that or I am not sure. I have to look in to that one. Can I force an active window?

Edit:
For some reason, debug mode shows that it thinks the active form is "frmStartUp". Have no clue why it would think that. The frmMain loads and at some point in the load event it pops up a message box. When that box exits, I can see frmMain is the highlighted screen, but the Scrren object thinks it is that other form. I am confused as Access is.
 
Last edited:

ByteMyzer

AWF VIP
Local time
Today, 03:36
Joined
May 3, 2004
Messages
1,409
Why doesn't this work?:
Code:
For Each Ctl In Forms!frmMain.Controls
      If Ctl.ControlType = acCommandButton Or Ctl.ControlType = acTextBox Or Ctl.ControlType = acOptionGroup Or _
        Ctl.ControlType = acListBox Or Ctl.ControlType = acComboBox Or Ctl.ControlType = acCheckBox Then
          If  Ctl <> Screen.ActiveControl Then
            Ctl.Enabled = False
          End If
      End If
    Next Ctl

In what function, sub or event are you calling this code?
 

Daveyk01

Registered User.
Local time
Today, 03:36
Joined
Jul 3, 2007
Messages
144
In what function, sub or event are you calling this code?

I have this as a public sub.

Background. I use the MSComm Active X control to talk to an instrument under test via the serial port.

The program has been used for a while now. One of the anoyances is that while communications is running, a user can push a command button, or update a text field that can cause addition communications to be called for. I was creating a small routine to disble all buttons and lock all text fields while communications is running, or while a routine that uses communications is running. That's easy, except when the CTL is the active control and the routine tries to disable that button then the error.

So I need to disable all buttons and controls and if a control has focus you can not do that.

EDIT:
Okay, can not use the SCREEN.ACTIVE... in a routine that is called from the OPEN or LOAD events. I guess because that the form is not active yet.

The one comm routine that was running when the form was loading was one that loaded a combo box of available serial ports. I have now changed that to the combo box ENTER event. If its rowsource is blank then run the routine. It only happens once.
 
Last edited:

Daveyk01

Registered User.
Local time
Today, 03:36
Joined
Jul 3, 2007
Messages
144
In what function, sub or event are you calling this code?

Well, that's all fixed now, for the first page, frmMain. The main lesson I learned is that you shouldnot try to determine the active control before the form is fully loaded.

Now I can disable all controls except the one that has focus. For that button, combobox, etc, I use a statis BRunning boolean variable to prevent it from being re-run by a user.

my frmMain form stays loaded at all times the instrument is under test as it contains the MSCOMM activeX control that is used by all other forms.

Now I have to figure out which form is active. I can use the fIsLoaded (found elsewhere in here) routine to determine if a form is loaded but that may not be enough. I have to experiment to obtain the active form's name to determine what form to be enabling/disabling controls on using the "Screen.ActiveControl.Name" parameter.

EDIT:
Okay, just figured this one out too.

Snipit:
=====================================
Dim Ctl As Control
Dim MyForm As Form
'
Set MyForm = Screen.ActiveForm
'
On Error GoTo 0
'
If IsNull(PHXSBusy) Then PHXSBusy = False
'
If CState Then
PhXSTalking = True
If fIsLoaded("frmMain") And MyForm.Name = "frmMain" Then
=====================================

So not only am I checking that the form is loaded but that it is also the active form. So far this seems to work well. And bygosh it is through here or experimenting; the on-line help (F1) stinks when it comes to advanced topics such as this. I wonders how power users figured out stuff like this?
 
Last edited:

ByteMyzer

AWF VIP
Local time
Today, 03:36
Joined
May 3, 2004
Messages
1,409
A simpler way would be to incorporate an error-trap, like the following:
Code:
[COLOR="Navy"]On Error Resume Next
For Each[/COLOR] ctl [COLOR="navy"]In[/COLOR] Forms!frmMain.Controls
    [COLOR="navy"]If[/COLOR] ctl.ControlType = acCommandButton [COLOR="navy"]Or[/COLOR] _
       ctl.ControlType = acTextBox [COLOR="navy"]Or[/COLOR] _
       ctl.ControlType = acOptionGroup [COLOR="navy"]Or[/COLOR] _
       ctl.ControlType = acListBox [COLOR="navy"]Or[/COLOR] _
       ctl.ControlType = acComboBox [COLOR="navy"]Or[/COLOR] _
       ctl.ControlType = acCheckBox [COLOR="navy"]Then[/COLOR]
        ctl.Enabled = [COLOR="navy"]False
    End If
Next[/COLOR] ctl
[COLOR="navy"]On Error GoTo 0[/COLOR]
 

Daveyk01

Registered User.
Local time
Today, 03:36
Joined
Jul 3, 2007
Messages
144
A simpler way would be to incorporate an error-trap, like the following:
Code:
[COLOR=navy]On Error Resume Next[/COLOR]
[COLOR=navy]For Each[/COLOR] ctl [COLOR=navy]In[/COLOR] Forms!frmMain.Controls
    [COLOR=navy]If[/COLOR] ctl.ControlType = acCommandButton [COLOR=navy]Or[/COLOR] _
       ctl.ControlType = acTextBox [COLOR=navy]Or[/COLOR] _
       ctl.ControlType = acOptionGroup [COLOR=navy]Or[/COLOR] _
       ctl.ControlType = acListBox [COLOR=navy]Or[/COLOR] _
       ctl.ControlType = acComboBox [COLOR=navy]Or[/COLOR] _
       ctl.ControlType = acCheckBox [COLOR=navy]Then[/COLOR]
        ctl.Enabled = [COLOR=navy]False[/COLOR]
[COLOR=navy]   End If[/COLOR]
[COLOR=navy]Next[/COLOR] ctl
[COLOR=navy]On Error GoTo 0[/COLOR]

Okay, thanks. I am now using that as a last-resort for one form that I have to do some communications in the form load event.

Typically, I hate resume next. I would rather see the errors and correct the code. So once I was sure the code was good, then I used the resume next for that one form. The others are running without an on error net, so to speak.

Happy New Year
 

Users who are viewing this thread

Top Bottom