Microsoft Office Tutorials and References
In Depth Information
Changing Form Controls with VBA
of a form
The payment-method control in the example is named Payment Method.
It’s a combo box that allows the user to choose one of four possible payment
methods: Cash, Check, Credit Card, and Money Order. As soon as the user
makes a selection from that combo box, you want some VBA code to change
some other controls. In particular, you want it to disable controls that aren’t
relevant to the selected payment method. You can also have it mark the
Paid field as False when Purchase Order is selected. Just as an example,
for this exercise, have it hide the ExpireLabel control when the user
selects anything except Credit Card.
So the first question is when to execute this custom VBA code. The After
Update event is the best event for this situation, because it occurs after a
new value is selected and any validation criteria for the field have already
been met. In this case, you click the Payment Method control (in Design
view) to select it. If the property sheet isn’t already open, right-click that
control and choose Properties from the contextual menu. Click the Event tab
on the property sheet. Click the After Update event; click its Build button;
and then choose the Code Builder in the dialog box that appears, as illustrated
in Figure 4-5.