Introducing Object-Oriented Programming
The world is filled with objects. The car you drive, the computer you use, and the radio you listen to are all examples of objects. Some objects, such as a desk lamp, are relatively simple, while other objects, such as a stealth bomber, are considerably more complex.
In addition to physical objects, the world is filled with objects you can't feel or touch. Electricity, sound, and light are all examples of objects people can produce, measure, and use, but you can't sense them as physical entities. An object's visible characteristics have little to do with its value to people. The electricity coursing through your computer's circuitry can be as valuable as the car you drive, under the right conditions.
You'll find any number of visible and invisible objects in most Access databases. And, just as with the objects that make up our environment, the invisible objects in an Access database can be as valuable as the forms, menus, and ribbons the user sees.
Getting to know objects
An Access object is a programmable entity of one sort or another. The Err object is an example of an invisible, but valuable, object built into Access. You use the Err object's properties (Number, Description, and so on) to determine which error has occurred. The Clear method resets the Err object, preparing it for the next error to occur. Even though the Err object never appears on an Access form or report, it has an important role in every professional Access application.
Understanding what objects are
Although there's an endless variety of objects, all objects have features in common:
- An object is a programmable entity. Most objects contain a number of properties you can read or set at runtime.
- Most objects include methods you can execute to perform tasks. An object's properties and methods define the object's interface to the rest of the program.
You can write custom objects to adapt to changing environments and user requirements. Most often, you can exploit an object's programmable nature by changing its properties and invoking its methods. But you can engineer a custom object in such a way that the object automatically adapts to differing conditions by running different internal routines.
You can create most object types multiple times in an application. Each time you create the object, Access assigns it a unique name to distinguish it from other instances of the object. In other words, a single Access program can host more than one instance of the object, with each object operating independently of the others (possibly even cooperating with the other objects) and maintaining its own set of properties and other data.
For example, say the Northwind Traders database (included with Microsoft Access) contains a Product object. The class module supporting the Product object defines the Name, Supplier, UnitPrice, and other properties of the product. There are any number of Product objects in the Northwind Traders database, each with its own name, price, and supplier.
To carry the analogy further, another class module might define a ProductInventory collection object that contains a number of Product objects. The ProductInventory class would feature a Count property that tells you how many Product objects are in the collection. The Product Inventory class module might contain a Sell method that deducts a certain Product item from the ProductInventory.
Using objects in applications
Every time you've written code setting a label's Caption property or returning the contents of a text box's Value, you've worked with objects. Although a label or text-box control is a simple type of object, the principles behind these objects are the same as using more complex and intelligent objects that you create yourself.
The following Access VBA code shows a series of statements that are typical of how you'd use objects in Access applications:
Dim MyObject As ObjectClass Set MyObject = New ObjectClass 'Setting a property of the object: MyObject.SomeProperty = SomeValue 'Invoking a method of the object: MyObject.SomeMethod
Some of this code might seem a little strange, especially the statement where MyObject is assigned to a New ObjectClass. As you'll see later in this tutorial, all this statement does is create a new object named MyObject that's based on the ObjectClass class.
In this code, the name of the object is ObjectName and its object class (described in the next section) is ObjectClass. You declare the object in the Dim statement and the New keyword instantiates (creates) it. SomeProperty is a property of the object, and SomeMethod is a method of the object.
In this tutorial:
- Object-Oriented Programming with VBA
- Introducing Object-Oriented Programming
- Defining objects with class modules
- Adding a class module to a database
- Using the product object
- Creating bulletproof property procedures
- Recognizing the Benefits of Object-Oriented Programming
- Managing a class's interface
- Using Property Procedures
- Exploring property-value persistence
- Modifying the Product Class
- Adding a new property to provide extra information
- Class Events
- The Class_Terminate event procedure
- Adding Events to Class Modules
- Creating custom events
- Trapping custom events
- Exploiting Access class module events