When you work in most programs and dialog boxes, Windows disables (dims) controls that aren't relevant at the moment. You can add that same capability to your Access databases by using some VBA code. For example, you might create a form that allows a user to choose from among different payment options. When the user chooses Credit Card, you want all the fields for entering credit card information to be enabled. When the user selects any other payment method, you want to disable those same controls, as illustrated in Figure 6-2.
For the sake of example, assume that the controls in Figure 6-2 are named (from top to bottom) PaymentMethod, PONumber, CCType, CCNumber, CCExpireMonth, CCExpireYear, and CCAuthorization. We refer to those control names in the sections that follow.
Every control on a form has an Enabled property. When that property is True (or Yes), the control looks normal. When the Enabled property is False, the control is disabled and therefore appears dimmed on the form.
To enable or disable a control on a form through VBA, use the general syntax controlName.Enabled = True|False where controlName is the name of the control and TruejFalse means that you can use True to enable the control and conversely use False to disable the control.
For example, the following VBA statement enables a control named CCType:
[CCType].Enabled = True
The following VBA statement disables a control named CCType:
[CCType].Enabled = False
In a class module, any field names without identifiers refer to the current form. For example, it's sufficient to use a field name like [PaymentMethod] rather than Forms![formName]![PaymentMethod] because the current form is assumed.
Note this one catch to enabling and disabling controls from VBA: You can't disable a control if the cursor is in that control. So, in addition to knowing how to enable and disable controls, you need to know how to position the cursor with VBA, as we explain in the following section.
Was this article helpful?