enter key action

A

Andy G

I want to call a function everytime the 'Enter' key is hit on the keyboard.
How can I intercept the 'Enter' key press and have it call a funciton?

Thanks.
 
A

Allen Browne

You can trap the keystroke if you set your form's KeyPreview property to
Yes, and use the KeyDown event.

The underlying question has to be why you would want to do that, regardless
of whether the user is merely adding a new line to a text box (see
EnterKeyBehavior property), moving to the next control in the Tab Order,
executing the default command button, or if you are even trying to trap
Enter for users who use the keyboard to navigate their menus. If you are
trying to trap moving controls, the user has other way to move, such as the
mouse and a tab key.
 
A

Andy G

Allen, thank you for your post.

I have a very good reason for trying to trap the enter key. I have a search
form that is a subform in my main form. When someone searches in the search
form the records show up in the main form after clicking the find button.
After someone searches using that form I want them to be able to press the
enter key to fire the button action instead of having to click it with their
mouse.

Andy
 
A

Allen Browne

Ah: you could set the button's Default property to Yes to achieve that goal.
 
A

Andy G

'Trap enter key entry to fire search button click
If KeyCode = 13 Then
btnSearch_Click
End If

I had the above code but the button's default property would be much easier!

Thanks!
 
Top