Learn Access 2003 VBA with The Smart Method
100
www.LearnAccessVBA.com
Lesson 5-6: Set and retrieve
form control values from
within VBA
Appreciate that all form controls are objects with Properties, Methods
and Events. We are going to refine frmTest so that the user can enter the
numbers to be added into text boxes on the form. We can then pick up
the values by inspecting the Value property of each Text Box object and
will then display the result in a Text Box control on the same form.
1
Add three text box controls and three label controls to the
form so that it looks like this:
2
Name the three Text Box controls txtFirstNumber,
txtSecondNumber and txtResult.
3
Change the appearance of the txtResult text box by setting
the following properties:
Make sure you are in Design View before changing the properties
otherwise you won’t see any effect until the form is closed, saved
and re-opened. Note also that, due to a bug in Access, the Border
Color property must be set first (see sidebar).
Property
Value
Border Color 8421504
(Gray)
This color works well for the
sunken border.
Border Style Solid
The sunken effect needs a border.
Locked
Yes
Stops the user typing into the
control.
Enabled
No
Stops the control having focus.
Tab stop No
Takes the control out of the tab
order.
Back Style Transparent Makes the inside color of the
control the same as the form.
Special Effect Sunken Makes the control appear sunken.
Important
Text Box controls have been
named using the generally
accepted prefix of txt.
In keeping with the cradle to the
grave naming convention the
remainder of the control name
should always be exactly the
same as the relevant caption on
the form.
These naming conventions and
rules are included in Appendix
A: The Rules.
Important
Note that there’s a bug in the
Border Color property as it sets
the Special Effect property to
Flat whenever it is changed.
For this reason it is important
that you set the Border Color
property first.
Session5c