witch control has the focus

  • how can i tell by code witch control has the focus?

    thanks

  • If you are talking about an Access form, you can use the Screen.ActiveControl object.

    Aunt Kathi Data Platform MVP
    Author of Expert T-SQL Window Functions
    Simple-Talk Editor

  • I have a similar need. I need to know, when a user is moving from one record to the next, which control has focus. I've tried using Screen.Activecontrol in the OnCurrent event of the form like so

    Private Sub Form_Current()

    If Screen.ActiveControl.Name = "Regulatory_Sector_DB_Key" Then

    Call Regulatory_Sector_GotFocus

    End If

    <more stuff here>

    End Sub

    but I get;

    "Run-time error '2474':

    The expression you entered requires the control to be in the active window"

    The code that's highlighted in the editor is the If statement. I've also tried Me in place of Screen.

    Regulatory_Sector_DB_Key is a ComboBox. Regulatory_Sector is a text box which I've positioned over top of the combo box. Regulatory_Sector always displays the current value of the record. Only when a user moves into the Regulatory_Sector text box the Combobox's rowsource is requeried and focus is moved to the ComboBox. If the user subsequently navigates to a new record while focus is on the ComboBox I would like to requery the Rowsource.

    Thanks in advance for any assistance.

  • A couple of questions to help narrow the scope of the problem: What version of Access are you using, and what format is the database - .mdb or .adp (or the equivalent .accdb format for Access 2007 and later versions)?

    Wendell
    Colorful Colorado
    You can't see the view if you don't climb the mountain!

  • I'm sorry I forgot to include this information. I'm using MS Access 2010. The file format is an .accdb. I'm using linked tables to a SQL Server 2008R2 server.

  • I think the issue you are having is related to the OnCurrent event - it gets triggered the first time the form becomes active (after the load and got focus events), but I don't think it becomes the active form until after the OnCurrent event completes. You could try putting a break point on the expression and see what Screen.ActiveControl.Name tells you. My suspicion is that it is empty until after the event completes. If that is the case you could try creating a public variable that tells you the form has been initiated with the first OnCurrent event. But it's a rather tricky thing to synchronize controls that way. Is the combox box actually bound to a value of a field in the data source for the form, or are you dealing with an unbound form?

    Wendell
    Colorful Colorado
    You can't see the view if you don't climb the mountain!

  • I have come to the same conclusion that you have regarding the OnCurrent event. I am using a bound form. I have found a workaround though that was probably the right way to go in the first place. The GotFocus event fires for the control that has focus when the user navigates between records. I have simply added my code to that event.

    Thanks again for taking the time to respond to my inquiry.

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply