Results 1 to 4 of 4
  1. #1
    New Lounger
    Join Date
    Dec 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help with SetFocus on Excel 2003 VBA Form ...

    Having some trouble with the textbox.SetFocus method. I want the code to move to a specific textbox but it keeps moving to the NEXT textbox control in the tab order instead of the one I'm asking for. I'm testing a condition in the _EXIT method of a textbox and trying to reset the focus back to that textbox. Here's some example code showing my issue:

    Code:
    Private Sub txtFrontAngle_Exit(ByVal Cancel as MSForms.ReturnBoolean)
      'Verify entered angle is <= IMaxAngle (note that IMaxAngle is a form global variable).
    
      Dim iFrontAngle as Integer, _
            iBut as Integer
    
      'Error trap used to keep blank field from causing problems.
      On Error Resume Next
      iFrontAngle = CInt(txtFrontAngle.Text)
    
      If iFrontAngle > IMaxAngle Then
        iBut = MsgBox(("ERROR: Angle must be <= " & Str(IMaxAngle) & "."), vbOKOnly, "Entry Error")
        ' MORE CODE HERE ... DOESN'T AFFECT .SetFocus PROBLEM
        txtFrontAngle.SetFocus
      End If
    
    End Sub
    This appears to be consistent, whatever textbox I attempt to .SetFocus to causes the same result (next textbox gets focus). Must be doing something stupid. Appreciate any help.

  2. #2
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Sealyons,

    Welcome to the Lounge as a new poster!

    Try changing: txtFrontAngle.SetFocus
    To : Cancel = True

    If this doesn't work you might try using the After_Update event with the same code and change as above.

    HTH
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  3. #3
    New Lounger
    Join Date
    Dec 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks. That worked directly in the _EXIT code. Simple enough, but still not sure why I was seeing the wrong textbox get focus. Guess this was because the .SetFocus was being run within event code that was running on that same textbox.?. If I used the AFTER_UPDATE event, that should have helped ensure that VBA had closed processing on the textbox before I tried to run more code against it. Correct? Really appreciate the help.

  4. #4
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Sealyons,

    The reason it was moving to the next tab stop was because that is where the _Exit builtin code is designed to send it after the _Exit user code is processed. Setting cancel to true prevents this builtin code from running. HTH
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

Posting Permissions

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