Learn Access 2003 VBA with The Smart Method
60
www.LearnAccessVBA.com
Lesson 3-2: Understand
methods
What are methods.
As well as having properties to describe an object some objects have
certain functionality too. Let’s consider a Car object. We may have
precisely described it by setting properties such as Color, NumberOfSeats
and FuelType but what is the car for. Is it a film prop, a paperweight, or
does it actually do something.
The things objects can do are called methods.
All cars have several methods such as StartEngine, MoveForward, Stop,
SteerLeft and SteerRight. Some cars have methods that are not common
to all cars such as StartAirConditioner or EnableCruiseControl.
Methods may be modified by arguments
Access objects have methods too. For example a Combo Box object has an
AddItem method.
Simply invoking the MoveForward method of the Car object wouldn’t
really be enough. To more precisely tell the car what to do we might
want to say:
Move forward three metres.
In order to do this we’d need to invoke the MoveForward method along
with some instructions telling the car how to move forwards. These
instructions are called parameters or arguments. The actual
MoveForward() method call would more likely be something like this:
MoveForward( 3 )
note
Some programmers prefer the
term Parameter instead of
Argument. Both are correct.
Session3
pg_0002
Session Three: The Object-Orientated Paradigm
© 2007 The Smart Method Ltd
61
Where the argument 3 represents the distance to move forwards in
metres.
Examples of methods available in Access
objects
A method with no parameters
Methods do not have to have parameters. A good example of a very
useful method is the Form object’s Requery method. Invoking the
Requery method will execute the SQL query that underpins the form and
will then update all of the controls on the form to reflect the current state
of the data.
This could be very useful in a multi-user environment when the user
wanted to be sure that the data displayed on the screen was completely
up to date.
A method with parameters
The Combo Box object has a method that allows items to be added to it.
This is intuitively named the AddItem method.
Clearly the AddItem method must have some parameters in order to tell
the Combo Box object something about the item to be added.
The method actually has two parameters. The first is the text that will be
displayed in the combo box and the second is an optional parameter to
indicate the position in the list.
For Example:
AddItem( “London")
AddItem( “Glasgow")
Would result in a combo box with two entries, the first would be London
and the second Glasgow.
AddItem( “London", 1 )
AddItem( “Glasgow", 0)
Would result in a combo box with two entries, the first would be
Glasgow and the second London.