DI Management Home > Microsoft Access Classes > Simple example

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.

Hint: Before starting, make sure these boxes are ticked in the Coding Options area of the Tools > Options > Modules menu:
  • Auto List Member
  • Auto Quick Info
  • Auto Data Tips


  1. Select the Modules tab of the database window
  2. Use menu options Insert > Class Module
  3. Complete the following code:
    Option Compare Database
    Option Explicit
    Public OrderID As Long
    Public CustomerName As String
    Public Value As Currency
  4. File > Save and save as clsSimple and close the VBA window.
  5. Now create some test code: In the Modules tab, click on New then Insert > Procedure with name TestSimple
  6. 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
    End Function
  7. File > Save with name basTest
  8. Open the Debug Window by pressing Ctrl+G or by using the toolbar button
  9. 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:

    • Did you notice how Access offered you a list to choose from after you'd typed oSim followed by the dot?

      Auto List Member

      (This requires the Auto List Member feature to be turned on.)

    • Even when you were declaring the type with
      Dim oSim As ...

      Access already knew about your new class.

      Auto List Classes

    • If you use the menu option View > Object Browser (or just press F2 when in the Debug Window), you can search for clsSimple and see the Members of its Class listed out just like all the "real" classes that Access comes with.

    Back to the instructions...

  10. 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
    ? oSim.Value

    This should display 99, the value of the oSim.Value property you've just set.

  11. 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
os.Value = 199.99
? os.Value
? os.CustomerName
os.CustomerName = "Barney"
? os.CustomerName
? os.OrderID
os.OrderID = 33
? os.OrderID

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.

More questions

  1. What are the default values of our new object's properties when it's first created?

  2. What happens if you mis-spell the name and type, say, os.Val instead of os.Value?

  3. Does Access care about upper and lower case if you type os.Value or os.VALUE or OS.value ?

<< Back  |  Next >>

Contact us

To contact us or comment on this page, please send us a message.

This page first published circa 2000. Last updated 11 June 2014. Reformatted for HTML5 on 22 June 2020.