How to exit field and form in one key stroke

BrighterLater

Board Regular
Joined
Sep 16, 2005
Messages
114
I'd be grateful for help on simplifying the use of certain of my VBA dialogues.

In windows' dialogue boxes, having entered text into a field, hitting Return not only exits the field but may also behave as if one had hit the OK button.

In the simple way I code VBA forms this takes two key strokes. The first Return exits the field and then, through tab order, focuses the OK button. It takes a second Return to fire the OK button. Is there a conventional way to make this behave more like a windows dialogue form. Presumably you need to recognise whether you exited the field with a Return or some other keystroke like an arrow or tab.

Thank you
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Sorry I was being lazy - worked it out.

Supposing the text field is Textbox1, then Textbox1_KeyDown event returns the ascci value of each key stroke. So all I have to do is check for a value of 13 and then treat it as if the OK button had been pressed.
Sorry for wasting your time
 
Upvote 0
If you have a command button on the form that does what you want you can set it's Default property to True/Yes.

Then when entered is pressed the command button's code will be triggered.

If you want the same but with the Esc key set the Cancel property to True/Yes.
 
Upvote 0

Forum statistics

Threads
1,224,598
Messages
6,179,822
Members
452,946
Latest member
JoseDavid

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top