Choosing an object and event for the code

Getting back to 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 CCExpireYear) 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 forms Design view, click the PaymentMethod control to select it and then click AfterUpdate on the Events tab of the Properties 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.

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 as below:

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 If...Else...End If keywords to make the decision.

Selected control

Figure 6-3:

The Payment-Method control's AfterUpdate event.

Selected control

Figure 6-3:

The Payment-Method control's AfterUpdate event.

Event Build button

Event Build button

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

Within the If...Else...End If statements, the code will position the cursor and enable or disable controls based on the current contents of the PaymentMethod control. The logic of the procedure (not written in actual 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

Move the cursor to the ShippingMethod control Disable the various Credit Card Controls End If

Writing the code

For the procedure to actually work, that logic needs to be written in VBA language and syntax. Listing 6-1 shows the procedure The sections that follow will 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 CCNameOnCard.Enabled = True CCNumber.Enabled = True CCExpi reMonth.Enabled — True CCExpireYear.Enabled — True

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

'Move the cursor to ShippingMethod control. ShippingMethod.SetFocus

'Disable controls for entering credit card info. CCType.Enabled — False CCNameOnCard.Enabled — False CCNumber.Enabled — False CCExpireMonth.Enabled — False CCExpireYear.Enabled — False

End If

End Sub

The first line of code in the PaymentMethod_AfterUpdate procedure compares whatever is currently 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 lines below 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 CCNameOnCard.Enabled = True CCNumber.Enabled = True CCExpireMonth.Enabled = True CCExpireYear.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 his or her next selection).

If the PaymentMethod control does not contain the words Credit Card, only the following lines execute. Those lines first move the cursor to the ShippingMethod control and then disable the various credit card controls.

'Move the cursor to ShippingMethod control. ShippingMethod.SetFocus

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

The code moves the cursor to the ShippingMethod control first to ensure that the cursor is not in any control that's about to be disabled. The ShippingMethod control is also the next control that the user is most likely to work in if he or she didn't choose Credit Card as the payment method.

Saving the procedure

After you type your procedure, choose FileOClose and Return to Microsoft Access. In the form's Design Properties 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.

0 0

Post a comment