Class Modules in Microsoft Access
(aka.emulating Object-Oriented programming)
Making it do something
<< Back to Classes index
page
Introduction
Now we've seen how to create an object with properties, we can make the
object do something. A function in a class module that does something
is known as a method.
Again, we'll use a simple example to demonstrate the basics.
Instructions
- Make a copy of our first class module: in the Modules tab, click once
on the object clsSimple, select Edit > Copy
then Edit > Paste and name the new module clsSimpleMethod
- Double-click on clsSimpleMethod and edit the code so it reads:
Option Compare Database
Option Explicit
Public OrderID As Long
Public CustomerName As String
Public Value As Currency
Public Function Tax() As Currency
Tax = 0.1 * Me.Value
End Function
- Save and close this module then Edit the module basTest
and Insert a new Procedure with the name TestTax
- Edit the code:
Public Function TestTax()
Dim oSim As New clsSimpleMethod
Dim cValue As Currency
For cValue = 100 To 1000 Step 100
oSim.Value = cValue
Debug.Print "Value=" & oSim.Value, "Tax=" & oSim.Tax
Next
End Function
- Save, close and run from the Debug Window by typing the word
TestTax and pressing Enter.
This should produce the results:
testtax
Value=100 Tax=10
Value=200 Tax=20
Value=300 Tax=30
Value=400 Tax=40
Value=500 Tax=50
Value=600 Tax=60
Value=700 Tax=70
Value=800 Tax=80
Value=900 Tax=90
Value=1000 Tax=100
What we've learned
Just adding the code for a public function Tax to our simple object
has created a method that calculates the sales tax at a straight 10 per
cent of whatever its value is at the time.
Again, it's hardly testing our computer, but it could equally well be
as complicated as we like.
What this means
Even with this simple example, the programmer using the object clsSimpleMethod
in his VBA code does not need to know any of the messy details of how
the tax is calculated. He just uses the expression oSim.Tax in
his code.
Meanwhile, from a higher level of program maintenance point of view,
if we want to change the rate of tax to, say, 12%, we only have to alter
one number in the code in clsSimpleMethod and, then, every time
anyone has used the expression oSim.Tax anywhere in the program,
the answer will be automatically correct.
You're probably thinking that, so far, this is really no different from
using a public function in a normal code module, apart from the auto list
prompting. We'll see in later examples why using classes is much more
powerful.
A more complicated case
If the tax laws become more complicated so, say, the tax is 10% if the
value is $100 or less and 12% if more, we can change the code in our class
module clsSimpleMethod as follows.
Public Function Tax() As Currency
If Me.Value <= 100 Then
Tax = 0.1 * Me.Value
Else
Tax = 0.12 * Me.Value
End If
End Function
This would give you the results in the Debug Window
set os = New clsSimpleMethod
os.value = 100
? os.tax
10
os.value = 200
? os.tax
24
Question: What do you think the word Me means in the expression
Me.Value?
|