Harness the power of properties in VBA

When you add a control to a form in VB and VBA, you can set certain properties for that control. Some properties are object specific and others apply to most, if not all, objects.

Here, we’ll look at ten properties that can be used to provide a better experience for you as you develop your code and for your audience as they put your solutions to use every day.

Handy VBA properties

The Caption property controls the text that is displayed on or alongside buttons, checkboxes and other similar objects.

If you set a control’s Accelerator property to a character used in its Caption text, the user can press Alt-Accelerator to select the control at runtime. For the TextBox control, you set its default text using its Value (or its Text) property.

To disable a control until it is needed, set its Enabled property to ‘False’ and the control will appear greyed out on the form and it won’t respond to any of its events. Similarly, the Visible property makes the control visible or invisible on the form.

Whether you use the Visible property or the Enable property to disable a control depends on the context. You would disable a control if you later intend to enable it when circumstances are appropriate for its use.

On the other hand, you would hide a control if its appearance makes no sense in the current context.

Navigation properties

To make a command button operate as a Cancel button, set its Cancel property to ‘True’. As only one button can have its Cancel property set to True, this automatically sets the Cancel property on all other controls to False.

To help the user navigate your form, use the TabIndex and TabStop properties. TabIndex sets the order in which controls are visited when the user presses the Tab key.

The first control visited has a TabIndex of ‘0’, the next is ‘1’ and so on. Use this property to move your user logically around a form.

If the TabStop property is set to ‘True’, the control will be visited when the user presses the tab key; if it is set to False, the control will be skipped.

Step 1

step1To protect a user from making accidental changes, set the Cancel button’s Default property and its Cancel property to ‘True’.

step2In this way, the Cancel button will be selected by default, so that, if the user launches a form and immediately presses Enter, they will exit immediately without making changes.

Step 2

The ControTipText property is used to display information to the user when they hover over the control.

Step 3

step1Every control has a (Name) property, which is how it is referred to it in your code.

By default, the name is provided by VBA but you can alter this.

So setting the (Name) property of CommandButton1 to TxtBoxVisibleButton lets you refer to it by this name in your code.