560 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owls only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Drawing UserForms in the VBA code editor
Part seven of a seven-part series of blogs
Learn how to create your own custom dialog boxes in VBA, using UserForms. This is the first part of a three-part series (the other parts are on Writing Code for UserForms and Creating Advanced Controls).
If you've been reading this far (or you've just dipped into this blog), you'll probably already know how to format a form. This page shows you how to make sure that short-cut keys, and the Enter, Tab and Cancel keys work as you would wish for a form.
What will happen on a form when you press the Tab key? The answer depends on the tab order of the form:
What will happen when you press Tab? You want the cursor to jump to the next control which can "receive focus" - that is, the Drink: combobox.
To set this tab order, either select the form or a Frame or MultiPage control on it. If you select the form, you'll see the form's tab order:
Either select the form itself, or a Frame or MultiPage control on it, then select to change the tab order as shown.
If you have a Frame or MultiPage selected, you'll only see some of the form's controls:
Because you have a frame selected, you can change the tab order of the controls within the frame only.
It may seem odd that the tab order includes labels, when these can't receive the focus (you can't click on them) and hence are nothing to do with tab order. Read on to find out how accelerator keys require labels to appear in the Tab Order dialog box.
In the form below, you can press ALT + D to jump to the Drink: combo box, since the label has the D underlined:
The underlined letter D is called an accelerator key.
You can only attach accelerator keys to labels, not textboxes or comboboxes. What will happen when you press ALT + D for the above example is that the cursor will jump to the next control which can receive the focus following the Drink combobox in the tab order.
The reason that you will go to the Drink: combobox above is that it is the first control after the lblDrink label which can receive focus (ie which you can click in).
As to how you attach accelerator keys - that's the easy bit:
Just type in a letter for the oddly-named Accelerator property .
When a form appears in Windows, usually one button has a slightly thicker border. When you press Enter, this is the one Windows will select:
Here the Order button has a slighly darker border, and is the one which will be chosen if you press Enter.
To make a button the default, assign its Default property to True:
Select the Order button for our example, and set the Default property to True.
Note that you can only have one default button on a form - so if you make another button the default, this will deselect this one.
Similar to the Default button above, the Cancel button is the one that Windows will choose when you press ESC.
Select the Cancel button, and set its Cancel property to True. When a user presses ESC, this will then run any code attached to this button. Just like for the Default button, you can only have one Cancel button per form.
That's the end of this marathon on-line training on formatting user forms. Next up, we'll learn:
These, however, are the subject of separate blogs.
|Parts of this blog|
25 Aytoun Street