enter key action

  • Thread starter Thread starter Andy G
  • Start date Start date
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.
 
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.
 
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
 
Ah: you could set the button's Default property to Yes to achieve that goal.
 
'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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top