Choosing an object and event for the code

In the example shown in Figure 6-2, assume that you want your code to either enable or disable the various credit-card-related controls (CCType through CCAuthorization) depending on the current contents of the PaymentMethod control. The AfterUpdate event of PaymentMethod occurs whenever a user chooses a valid option from the PaymentMethod control, so you want to tie the code to the PaymentMethod control's AfterUpdate event.

In the form's Design view, click the PaymentMethod control to select it and then click AfterUpdate on the Event tab of the property sheet. The next step is to click the Build button, as shown in Figure 6-3. In the Choose Builder dialog box that opens, choose Code Builder and then click OK.

event property.

|On Got Foe On Lost Foe

¡OnDbl Click

On Mouse Down On_Mou5e Up On Mouse Move

□Pr event property.

|On Got Foe On Lost Foe

¡OnDbl Click

On Mouse Down On_Mou5e Up On Mouse Move

Selected control

Event property

Build button

The form's class module opens, displaying a new, empty Sub procedure named PaymentMethod_AfterUpdate(), based on the object and event names. In the Code window, the empty procedure appears this way:

Private Sub PaymentMethod_AfterUpdate() End Sub

Any VBA code that you place between those two lines is executed every time a user changes the contents of the PaymentMethod control.

Every time the PaymentMethod_AfterUpdate() procedure executes, its code needs to make a decision: Should it enable or disable the credit card controls? You can use the VBA lf...Else...End If keywords to make the decision.

See Chapter 4 for more information on the VBA lf...Then...End If keywords.

Within the lf...Else...End If statements, the code positions the cursor and enables or disables controls based on the current contents of the PaymentMethod control. The logic of the procedure (not written in VBA code yet) looks like this:

If "Credit Card" is selected in the PaymentMethod Field Then

Enable the various Credit Card Controls Move the cursor to Credit Card Type (CCType) control Else

Disable the various Credit Card Controls End If

Writing the code

For the procedure to work, that logic needs to be written in VBA language and syntax. Listing 6-1 shows the procedure; the sections that follow look at each step in the procedure more closely.

Listing 6-1: PaymentMethod_AfterUdate() Procedure

Private Sub PaymentMethod_AfterUpdate()

If [PaymentMethod] = "Credit Card" Then

'Enable controls for entering credit card info. CCType.Enabled = True CCNumber.Enabled = True CCExpireMonth.Enabled = True CCExpireYear.Enabled = True CCAuthorization.Enabled = True

(continued)

94 Part ll: VBA Tools and Techniques

Listing 6-1 (continued)

'Move the cursor to the CCType control. CCType.SetFocus Else

'Disable controls for entering credit card info. CCType.Enabled = False CCNumber.Enabled = False CCExpireMonth.Enabled = False CCExpireYear.Enabled = False CCAuthorization.Enabled = False End If

End Sub

The first line of code in the PaymentMethod_AfterUpdate procedure compares whatever is now stored in the control named Payment Method. That line, on its own, reads

If [PaymentMethod] = "Credit Card" Then

Translated to English, the line says, "If the control named PaymentMethod contains the words Credit Card, then do the following lines up to Else; otherwise (else), skip the lines under Else." The same statement also means, "If the PaymentMethod field does not contain the words Credit Card, then only do the lines between Else and End If." Thus, if the PaymentMethod control contains the words Credit Card, these lines of code execute:

'Enable controls for entering credit card info. CCType.Enabled = True CCNumber.Enabled = True CCExpireMonth.Enabled = True CCExpireYear.Enabled = True CCAuthorization.Enabled = True 'Move the cursor to the CCType control. CCType.SetFocus

Those lines ensure that all the credit card controls are enabled and then position the cursor to the CCType control (where the user is most likely to make her next selection).

If the PaymentMethod control doesn't contain the words Credit Card, only the following lines execute to disable the various credit card controls:

'Disable controls for entering credit card info. CCType.Enabled = False CCNumber.Enabled = False CCExpireMonth.Enabled = False CCExpireYear.Enabled = False CCAuthorization.Enabled = False

Saving the procedure

After you type your procedure, choose FileOSave and then choose FileO Close and Return to Microsoft Office Access. In the form's Design property sheet, the words [Event Procedure] appear as the property. To test the procedure, switch to Form view and choose a different option from the

Payment Method control.

Was this article helpful?

0 0

Post a comment