MS-Access / Getting Started

Defining objects with class modules

You define an object by the code in a class module. You add that class module to your Access application, and then add the property and method code to a module before using the object that the class module defines. The name of the class module is the name of the object's class.

A class module is a special type of code module. Access recognizes the module as the definition of an object and lets you create new instances of the object from the code in the module. Any of the object's special features - including properties, methods, and events - are exposed as procedures tagged with the Public keyword in the class module. You should declare any code in the class module you intend for only the object to use, and that you won't expose to the outside world, with the Private keyword.

Each object you create from the class module is an instance of an object class. For example, the Nissan Sentra is a particular class of automobile. The Nissan Sentra that your Uncle Joe owns is a particular instance of the Nissan Sentra class of automobile. Even though Uncle Joe's car looks pretty much like every other Nissan Sentra, certain attributes of his car set it apart from all the other Nissan Sentras on the road.

Carrying the car analogy a bit further, consider the properties and methods of the automobile object class. A car has a color property that defines the color of the car's exterior. It's likely that the color of any car matches the color applied to other cars produced by the car's manufacturer. A car also has a vehicle identification number (VIN) that isn't shared with any other car anywhere in the world.

An object's property values, therefore, are a combination of values shared with other objects of the same class and values unique to the particular instance of the class. In fact, there must be a property or some other attribute of the object that sets it apart from all other instances of the same type of object in the application. Otherwise, Access can't know which instance you're referring to in your code.

If you were to construct a Product class module, you'd include properties such as Name (a string), UnitPrice (a currency data type), UnitsInStock (an integer or long integer), ReorderLevel (also an integer or long integer), and Discontinued (a Boolean value). Depending on how you planned to use the product object in the application, you might add properties to contain the quantity per unit, the category ID, and other information relevant to the application. You'd also want to add the ProductID property to uniquely identify each instance of the product object.

You may have noticed that all the properties I mention in the preceding paragraph correspond to the fields in the Products table in the Northwind Traders database. In fact, often each instance of the object represents a record contained in a database table.

Because you're constructing the class in VBA code, you can add any properties necessary to support the application and the data you're constructing. When you build Access classes, you have access to all the power and utility available through the Access data types and features. Adding new public procedures to the class module extends the properties and methods available to the object.

You can, therefore, define new data types to accommodate whatever peculiarities your application requires.

In the class module, private variables handle property values. As you can see in the "Exploring property-value persistence" section, later in this tutorial, the mechanism for implementing properties is part of the special attributes of class modules. You have to follow certain rules and coding conventions (see the "Creating simple product properties" section, later in this tutorial) to successfully implement properties in Access class modules.

In addition to properties, most objects support a number of methods, which are the actions that the class performs. An airplane has a number of rather obvious methods: ascend, descend, and land, among others. The classes you construct in Access implement whatever functionality you want the class's objects to support. The Product object we describe earlier might have Sell or Discount methods not shared with a Customer object in the same database.

The methods of a custom object exist as public procedures (functions and subroutines) in the class module. And, just as with properties, you have the full power and flexibility of VBA at your disposal as you write the methods of your custom classes.

[Previous] [Contents] [Next]