Microsoft Access 2010 Bible

(Rick Simeone) #1

Part IV: Professional Database Development


972


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 prop-
erty 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 prop-
erties to contain the quantity per unit, the category ID, and other information relevant to the appli-
cation. 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 sup-
port 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.
Free download pdf