Enter Key behavior with Userforms

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How do I make the enter key not tab to the next control in the tab order? I want to be able to Tab from control to control on my userform using the TAB key. However, when the user presses the ENTER key, I want to run some code and stay at that control. Any suggestions? The enter key by default is tabbing to the next control and I have not found any property of the form to turn this off.
Thanks in advance
 
Lance,

Here is an example of trapping the "Enter" keystroke using the KeyDown event
of a TextBox control. The logic is to check each keystroke inputted by the
user in the textbox control and if it equals the Enter key, throw away the
keystroke (keeping it will cause a tab to occur), and then call your other
subroutine.

Troy

Private Sub TextBox1_KeyDown( _
ByVal KeyCode As MSForms.ReturnInteger, _
ByVal Shift As Integer)

'''Trap the Enter key.
If KeyCode = 13 Then
'''Throw away the Enter keystroke.
KeyCode = 0
'''Call your subroutine here...
'''
End If
End Sub


Lance said:
How do I make the enter key not tab to the next control in the tab order?
I want to be able to Tab from control to control on my userform using the
TAB key. However, when the user presses the ENTER key, I want to run some
code and stay at that control. Any suggestions? The enter key by default
is tabbing to the next control and I have not found any property of the form
to turn this off.
 
Troy,
Thanks. Works great. I was missing the "Throw Away the Enter Keystroke" piece. I really appreciate your help.
 
Back
Top