Microsoft Office Tutorials and References
In Depth Information
Changing Form Controls with VBA
Looks can be deceiving
When you create a lookup field, what you
see in that field may not match what Access
actually stored in the field. You may have a
ContactID field that shows a customer
name in the format Jones, Hank , but Access
actually stored that person’s ContactID as a
number (perhaps 39 or whatever).
VBA sees what Access sees — the
ContactID number in the preceding
example, not the name. Any code you write needs
to take that fact into consideration. To create
an If statement that makes a decision based
on the contents of the ContactID field, use
something like this:
If Me.ContactID.Value = 39
If you use the following statement instead, the
code either generates an error message or
perhaps doesn’t give you the result that you
think it should:
If Me.ContactID.Value =
“Jones. Hank”
You can add a Debug.Print statement
to your code and run it from the Immediate
window to see what type of data is stored in
a control. The following example displays the
content of the ContactID control:
Debug.Print Me.ContactID.
Value
If that content is a number, you know that the
ContactID field in every record contains a
number.
The Select Case Me.[Payment Method].Value statement uses
the value (contents) of the Payment Method control to make a decision
about which controls to enable and make visible. When the Cash option is
selected, only this code is executed, filling the Paid check box with a check:
Case “Cash”
Me.Paid.Value = True
When the Check option is selected, the following lines execute to enable the
CheckNo control, place a check in the Paid check box, and move the cursor
to the CheckNo control:
Case “Check”
Me.CheckNo.Enabled = True
Me.Paid.Value = True
‘Move cursor to CheckNo control
DoCmd.GoToControl “CheckNo”
Search JabSto ::




Custom Search