Microsoft Access 2010 Bible

(Rick Simeone) #1

Chapter 28: Object-Oriented Programming with VBA


973


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 chapter, the mechanism for implementing proper-
ties 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 chapter) to success-
fully 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 dis-
posal as you write the methods of your custom classes.

Looking at a simple class module
Most often, the classes in your applications will model some real-world object, such as customers,
contacts, employees, and products. Your knowledge and understanding of the physical object
translate directly into Access VBA code and become the properties and methods of the Access
objects you create from the class module’s code.

This chapter’s database (Chapter28.accdb) implements a Product class similar to the one we
describe in the previous sections. The product class module — clsProduct1 — in
Chapter28.accdb includes the properties in Table 28.1 and the methods in Table 28.2.

TABLE 28.1


Example Properties in the Chapter28.accdb Database


Name Data Type Description
ProductID Long Integer The product’s ID
Name String Name of the product
Supplier String Name of the company supplying the product
UnitPrice Currency Customary selling price of the product
UnitsInStock Integer Current stocking level of the product
ReorderLevel Integer Minimum stocking level before reordering
Discontinued Boolean True if the product has been discontinued
Free download pdf