Microsoft Access 2010 Bible

(Rick Simeone) #1

Part IV: Professional Database Development


974


TABLE 28.2

Product Class Methods Used in the Example Database


Name Purpose
Sell Sells a quantity of the product
Discount Reduces the selling price of the product instance

The Product class object in Chapter28.accdb doesn’t completely model a real product. You
can add many other properties and methods to this class to more effectively model a real product,
but this simple class does show you how to approach modeling a physical object in Access.

You can describe the product you’d create from the class like this: A product Name and
ProductID identify the product. A certain manufacturer (the Supplier) produces the product,
and the product is intended to be sold for a certain unit price (UnitPrice). Northwind Traders
keeps track of the number of units in stock (UnitsInStock) and has determined the minimum
number of units to keep in stock (ReorderLevel). The manufacturer may discontinue a prod-
uct, in which case its ReorderLevel is set to zero and the UnitsInStock is allowed to
decrease to zero as items are sold. Periodically, a product may be sold (the Sell method) and may
also be discounted through the Discount method.

Adding a class module to a database
You can open a new class module in one of two ways:

l (^) In the editor window, choose Insert ➪ Class Module.
l In the Other group of the Access Create ribbon tab, select the Macro drop-down list, and
choose Class Module.
Tip
It’s a good idea to click on the Save button on the Code Editor toolbar and assign a name to the class module
early in its development cycle. The class name should be descriptive but not excessively long. Finally, the name
should be meaningful to you — users never see the name of the class, so use a name that means something to
you or another developer.
The name you provide for the class module becomes the name of the object’s class when creating
objects from the class module (see Figure 28.1). The name of the class module is similar to the
names you’ve given other objects in your databases.
The class module is in the code editor window in Figure 28.1. Notice that the class module looks
just like any other module in the editor window. Your only indication that it isn’t a normal module
is the tiny icon in the left corner of the module as it appears in the code editor. It’s a little box icon,
instead of the tinkertoy icon you see in standard modules.

Free download pdf