• Access2000 Form VBA Focus

    Author
    Topic
    #354926

    I am running Access2000 under Windows Me. I have defined an Access form for data input to a simple table and am writing VBA code for execution at various event conditions (e.g. Form_BeforeUpdate, etc.). As is normal, the movement of focus through the input fields (Text Box controls) is controlled by their Tab Index and Tab Stop properties. Elsewhere on the form are buttons that can be clicked at any time, that is, while any of the input fields is in focus. What the buttons do has no affect on the input process and so is irrelevant. My Problem: when one of the buttons is clicked, focus in all input fields is lost. To continue input I have to manually click the desired field. I want, at the end of the clicked button’s routine, to automatically return focus to either 1) the input field that was in focus or 2) the next field in the Tab Index sequence. (How to determine which is my problem.) What VBA command(s) can I use to return focus? Thanks for help.

    Viewing 1 reply thread
    Author
    Replies
    • #522538

      Hi,
      Screen.PreviousControl.SetFocus should do what you want.
      Hope that helps.

    • #522545

      In the On Enter event for each textbox control set the tag property of the form to the control’s tab index plus 1 (the next control you want to have focus).

      On Enter event of each txtbox:
      me.tag = txtBox1.tabindex +1

      In the Clicked event of each cmdbutton:

      Private Sub cmdClose_Click()
      Dim aTxtBox As Control
          'On Error Resume Next
          MsgBox "The next current control is: " & Me.Tag
          For Each aTxtBox In Me.Controls
              If TypeOf aTxtBox Is TextBox Then
                  Debug.Print aTxtBox.Properties.Item("Name")
                  If aTxtBox.Properties.Item("TabIndex") = Me.Tag Then
                      aTxtBox.SetFocus
                      Exit For
                  End If
              End If
          Next
      End Sub
      
      • #522820

        Thanks, Kevin, for the thought and effort you put into your solution.

        John

        • #523006

          You’re welcome. I couldn’t find a

          Screen.NEXTControl.SetFocus

          But if there’s a simpler way then the looping, I’d like to see it. Your situation is probably quite common.

    Viewing 1 reply thread
    Reply To: Access2000 Form VBA Focus

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: