Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trap Enter Key leaving Text Box 1

Status
Not open for further replies.

bbuk

Technical User
Apr 27, 2002
44
GB
I wnat to trap the enter keypress event for a text box on a user form in Excel/VBA. When Enter is pressed I want to check that the text of the text box is a valid date, and prevent the focus shifting to the next control if its not.
I can perform the validation, but I can't seem to stop the focus shifting to the next control.
Any idea's anybody please - I'm sure I'm missing something simple here.
 
Use the keydown event and, once the validation has proved false, reset keycode to 0. Otherwise, all the code will be processed, and then the keypress processed by vb, resulting in it moving on.

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 13 Then KeyCode = 0
End Sub

The above code just stops it moving on.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top