ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Enter Key behavior with Userforms (https://www.excelbanter.com/excel-programming/301796-enter-key-behavior-userforms.html)

Lance

Enter Key behavior with Userforms
 
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

TroyW[_2_]

Enter Key behavior with Userforms
 
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" wrote in message
...
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

Enter Key behavior with Userforms
 
Troy,
Thanks. Works great. I was missing the "Throw Away the Enter Keystroke" piece. I really appreciate your help.

"TroyW" wrote:

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" wrote in message
...
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





TroyW[_2_]

Enter Key behavior with Userforms
 
Your welcome. -- Troy


"Lance" wrote in message
...
Troy,
Thanks. Works great. I was missing the "Throw Away the Enter Keystroke"

piece. I really appreciate your help.

"TroyW" wrote:

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" wrote in message
...
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








All times are GMT +1. The time now is 06:50 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com