Consulting

Results 1 to 5 of 5

Thread: Userform Enter key

  1. #1
    VBAX Regular
    Joined
    Nov 2018
    Posts
    17
    Location

    Userform Enter key

    Hi all,

    Again i come cap in hand after not being able to work something out.


    I have a userform which i am trying to use with a hand scanner input. As the scanner effectively presses the CR at the end of the string, im using the textbox1_exit event to trigger the code to deal with the string.

    I then have it setting the textbox.value to "" to clear the box.

    But i would like it to then become the focus again. Using the userform.TextBox1.setfocus it works, but only if i step through the code. As soon as the _exit event finishes, it then selects the next control.

    Im trying to find a control that acts on the enter key being pressed?

    Cheers All

  2. #2
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    You could use the Tag in this fashion:

    Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
        'Do your stuff
        '....
        'Stay here = yes
        Tag = 1
        'Stay in here = no
        Tag = 0
    End Sub
    
    
    'Then whichever control gets focus next use...
    
    Private Sub TextBox2_Enter()
        If Tag = 1 Then TextBox1.SetFocus
    End Sub
    
    'or...
    
    
    Private Sub CommandButton1_Enter()
        If Tag = 1 Then TextBox1.SetFocus
    End Sub
    Semper in excretia sumus; solum profundum variat.

  3. #3
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    If commandbutton1 is the next taborder, then:
    Private Sub CommandButton1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)    
         'If KeyCode = Asc(vbCr) Then
         If KeyCode = 13 Then
            TextBox1 = ""
            TextBox1.SetFocus
        End If
    End Sub

  4. #4
    VBAX Regular
    Joined
    Nov 2018
    Posts
    17
    Location
    Quote Originally Posted by Kenneth Hobs View Post
    If commandbutton1 is the next taborder, then:
    Private Sub CommandButton1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)    
         'If KeyCode = Asc(vbCr) Then
         If KeyCode = 13 Then
            TextBox1 = ""
            TextBox1.SetFocus
        End If
    End Sub
    Thanks Kennith, that works a treat.

  5. #5
    VBAX Mentor 大灰狼1976's Avatar
    Joined
    Dec 2018
    Location
    SuZhou China
    Posts
    479
    Location
    Hi nbrown6!
    Maybe this example might help you a little.

    Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If TextBox1 = "" Then Exit Sub
    Sheets(1).Cells(Rows.Count, 1).End(3).Offset(1) = TextBox1
    TextBox1 = ""
    Cancel = True
    End Sub
    Attached Files Attached Files

Posting Permissions

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