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

Running code with the enter key

Status
Not open for further replies.
Jul 5, 2002
28
US
I have a search form, with a text box for the item to search for, a search button and a quit button. If the user presses the enter key after entering the item to search for (in the text box) I want the search to start. Currently, depressing the enter key just moves the focus to the search button. Also, I want the search to commence if the search button is clicked (as it currently does, now). In addition, there is a requery button on the results form that takes the user back to the search form, and I need to have the focus, when the user returns to the search form, be on the text box. Any suggestions?
Thanks in advance,
-jamie
 
Jamie,
Have not messed with forms for a while, but I think if you set the "Default" property of the search button to "True", it will receive the enter keypress. (Only one control on a form can be the default control).

As to having the focus on the textbox when you return to it, I think you can just set the taborder of the textbox to 0 (or 1), whatever is the lowest number possible. By default, taborder is set to the order in which you added the controls to the form.

Tranman
 
I have a similar situation but I can't set a button to the default as there are multiple buttons on the page.

_Keydown and _Keypress seem to be too early as the field isn't updated by the time they fire.

_Exit would work, but I don't want the search to start if the user just pressed Tab as there are other possible parameters and _Exit doesn't get the keycode passed as a parameter.

Only thing I can think of is
- Create a variable local to the form (call it EnterHit).
- Set EnterHit to 1 if KeyDown is the Enter key
- In OnExit, if EnterHit=1 then EnterHit =0 and start search.

Seems like there ought to be a simpler solution.

Anyone?

Thanks.

- MSK
 
I can't set a button to the default as there are multiple buttons on the page

The purpose of setting one button as the default is to allow it to accept the Enter keypress from anywhere on the form unless another command button has the focus.

Setting the search button as the default would solve the problem in the original post.
 
Create a lost focus event on your search field text box calling your search button sub. when you tab out or press enter it will call your search button sub.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top