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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Automatically detect Enter Key

Status
Not open for further replies.

DebbieCoates

Programmer
Oct 2, 2007
23
GB
I have a form with a text box in and a command button.

At the moment a user types something in the text box and then clicks the button.

is there a way that rather than having to click the button I can automatically detect if the enter key has been pressed, and then do the command button ?
 
Please read point #16 in faq222-2244 as this is important now that you have asked over a dozen questions.

To answer your question, set the button's "Default" property to "True
 
Thank you for your reply

I've managed to achieve what I wanted by useing

Private Sub txtURN_KeyPress(KeyAscii As Integer)
If (KeyAscii = 13) Then
Call cmdURNSearch_Click
End If
End Sub

I
 
That will work but would only be needed if the text box is Multi-Line or there is another command button which has it's "Default" setting to "True", or if the command button should react only when the focus is on the text box, otherwise not.
 
To give a little more information to SB's post, the "Default" property may be set for one and only one command button, and pressing the Enter key will be equivalent to pressing that button. The "Cancel" property may be set also to one and only one Command button, and pressing the Escape key will be equivalent to pressing that button.

If you set the default property to true for a command button, the Keypress event has no effect where the enter key is concerned, for the Default property overrides it.

If I wanted each text box to treat the enter key differently for some reason, I would organize the text boxes into an array. I would then call a procedure from the click event of the command button, passing the index property of the current text box as an argument.

HTH

Bob
 
Is it possible that you are looking for the AfterUpdate event?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top