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!

call an event after user hits the "ENTER" key

Status
Not open for further replies.
Jun 4, 2003
58
US
I have a textbox, and I want it so that after the user hits the "Enter" key it calls an event. Any VB code for something like that.

Thanks
 
Hi!

You'll probably need to use the controls keydown event for that (and the forms KeyPreview property set to yes):

[tt]if keycode = vbkeyreturn then
' the code
keycode = 0 ' <- cancels the enter key, if necessary
end if[/tt]

Roy-Vidar
 
once again, Thanks RoyVidar

now, do I put that in the textboxes After Update event?
 
well as a default when you hit enter in a text box it exits the box and moves to the next item in the tab order list, so could you use the "on exit" event of the text box


ah unless your text box is set to multiline
 
No. You put it in the Form_KeyDown Event ... assuming that you have the KeyPreview property set to TRUE for the Form.
 
sillysod, true...but if the user hits "Tab" or clicks out of the textbox, then the On Exit event occurs...I want to call the event on the Enter key only
 
Said it above: "controls keydown event" control is a general term for all the thingies on forms and reports, for instance a text box (or textbox control) ;-)

Form keydown hmmm - then it will invoke on every enter, perhaps that's the intention. Cancelling the keystroke (keycode=0) would avoid moving to next control.

Roy-Vidar
 
how about something along the lines of

Private Sub Text0_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
'do whatever you want to doEnd If
else
nextcontrol.setfocus

End Sub
 
Excepting the Multiline (as noted above), you could also work through the lost focus event. Although that would occur in other circumstances (Tab Key, Click outside of the text box, ... who knows how many more)




MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top