A simple example
<< Back to Classes index page
In this first example, we will create a very basic class module which we'll call
clsSimple. We'll give it some
properties that we can set and change using normal VBA code.
We'll also show how to use the Stop command in test code
to leave us "live" in memory.
Before starting, make sure these boxes are ticked in the
Coding Options area of the Tools > Options > Modules
- Auto List Member
- Auto Quick Info
- Auto Data Tips
- Select the Modules tab of the database window
- Use menu options Insert > Class Module
- Complete the following code:
Option Compare Database
Public OrderID As Long
Public CustomerName As String
Public Value As Currency
- File > Save and save as clsSimple and close the VBA window.
- Now create some test code: In the Modules tab, click on New
then Insert > Procedure with name TestSimple
- Complete the code:
Public Function TestSimple()
Dim oSim As clsSimple
Set oSim = New clsSimple
oSim.OrderID = 1
oSim.CustomerName = "Fred"
oSim.Value = 100.2
Debug.Print oSim.OrderID, oSim.CustomerName, oSim.Value
- File > Save with name basTest
- Open the Debug Window by pressing Ctrl+G or by using the toolbar button
- Type TestSimple and press the Enter key
This will print out the values of the properties you've set in your program.
The system should be highlighting the word Stop in yellow.
The Debug Window should have shown the results like this:
1 Fred 100.2
OK, it's nothing amazing, but it's a start. Let's make a few observations so far:
Back to the instructions...
The Stop command in your procedure will have halted the program
and left it "live" with your object oSim still in memory.
You can use the Debug Window to make changes to the object's properties.
For example, type these commands in the Debug Window:
oSim.Value = 99.00
This should display 99, the value of the oSim.Value property you've just set.
- To finish the program, press F5 or Run > Go/Continue
What we've learned
This very simple example has shown us how to create a custom object
clsSimple with properties
OrderID, CustomerName and Value,
and then set those properties and do something with them -
even if it's only printing them out again.
Creating a property is as simple as declaring a public variable
in the Class Module code.
It's also shown us how to set up a test program to leave us 'live"
in the Debug Window to do test and other mischief with our objects.
As a further example, just open the Debug Window again (Ctrl+G)
and type the following commands:
Set os = New clsSimple
os.Value = 199.99
os.CustomerName = "Barney"
os.OrderID = 33
You have just created a new object called os on the fly.
The command that did this was
Set os = New clsSimple
Because you aren't "live" in a procedure,
you won't get prompted with the auto list this time,
but you are still able to assign
and retrieve its properties.
What are the default values of our new object's properties
when it's first created?
What happens if you mis-spell the name and type, say, os.Val
instead of os.Value?
Does Access care about upper and lower case if you type
os.Value or os.VALUE or OS.value ?
Feedback or questions: Contact
Return to Tips and Tutorials Page.