Part II: Programming Microsoft Access
440
Me.Recalc
If Not IsNull(cboBuyerID) Then
‘Verify that the DiscountPercent is valid:
If Not IsNull(cboBuyerID.Column(2)) Then
‘Get the DiscountPercent from Column 2:
txtDiscountRate = _
Format(cboBuyerID.Column(2),”Percent”)
‘Get the Tax Location from Column 4:
txtTaxLocation = cboBuyerID.Column(4)
‘Get the Tax Rate from Column 3:
txtTaxRate = cboBuyerID.Column(3)
End If
Else
‘Invalid data found in the combo box,
‘so set all the text boxes to Null:
txtDiscountRate = Null
txtTaxLocation = Null
txtTaxRate = Null
End If
- Select Compile Chapter11 from the Debug menu in the code editor to check your
syntax. - Close the VBA window and return to the frmSales form.
The code first performs a Recalc on the form to update any values that may be in an incomplete
state, like a buyer ID in the process of being selected or a line item that was in the process of being
selected when the combo box was used.
Tip
Anytime you’re doing data entry and you need code to run to perform some process, it’s a good idea to first
run the form’s Recalc command.
The Me. refers to the current form and substitutes in this example for Forms!frmSales!.
The first IF statement checks to make sure a buyer ID was selected by making sure the current
value of the combo box’s bound column — ContactID — is not null. If it isn’t null (meaning, a
valid value was selected in the combo box), a second IF statement checks to make sure not only
that the value of cboBuyerID is valid but also that the value of the third column —
DiscountPercent — is not null.
If the DiscountPercent column (column 2) is valid, the values from that and other combo-box
columns are used to fill controls on the form.
Figure 11.10 shows the procedure created in the code editor after entering the procedure
described earlier. After you finish entering these statements, press the Save button on the toolbar to
save your code before closing the VBA window.