Results 1 to 6 of 6
  1. #1
    3 Star Lounger
    Join Date
    May 2002
    Location
    Florence, Oregon, USA
    Posts
    209
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Access2000 Form VBA Focus

    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.

  2. #2
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Access2000 Form VBA Focus

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

    Microsoft MVP - Excel

  3. #3
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Access2000 Form VBA Focus

    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:
    <pre>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
    </pre>

    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  4. #4
    3 Star Lounger
    Join Date
    May 2002
    Location
    Florence, Oregon, USA
    Posts
    209
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Access2000 Form VBA Focus

    Thanks, that was a good push.

  5. #5
    3 Star Lounger
    Join Date
    May 2002
    Location
    Florence, Oregon, USA
    Posts
    209
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Access2000 Form VBA Focus

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

    John

  6. #6
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Access2000 Form VBA Focus

    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.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •