Chapter 7
To program Microsoft Access
Class modules package code for easy reuse. The class module acts as a container that exposes the code and selected variables inside it in a way that is familiar to Microsoft Visual Basic for Applications (VBA) developers. Basically, you invoke class procedures and assign values to
This chapter first introduces standalone class modules and form and report class modules. Then it
The focus then shifts to the containers for class, form, and report modules as we look at the All collections that are new to Access 2000. Just as there are AllForms and AllReports collections, there is an AllModules collection. (In fact, there are ten All collections altogether.) The chapter wraps up by explaining how to combine the AllModules collection with the Modules collection to manage code in an application.
There are three basic kinds of modules:
Standalone class modules
First, standalone class modules do not have a built-in
Second, standalone class modules offer Initialize and Terminate events that enable operations that need to take place at the opening and closing of a class instance. Report and form modules do not have these events, but you can perform similar functions with the Load and Close events.
Third, you must use the
New
keyword to create instances of standalone class modules. Report and form class modules also let you create instances with the
DoCmd OpenForm
and
OpenReport
methods as well as by referencing the report or form class module's properties or
You can create a standalone class module from the Insert menu in VBE. (The same menu offers commands for building a standard module or a procedure.) After creating a class module shell, you can populate it with procedures and declarations, which equip it with custom properties and methods.
Special property functions make it easier to develop any combination of read-only, write-only, and read/write properties for your classes. If your application
Your applications can also build custom methods into classes. You can use subprocedures or function procedures to accomplish this. By selectively exposing
The public methods and procedures support programmatic access by procedures outside the class. You must first instantiate the class in a host procedure within another module, using the New keyword. (You use the same keyword to instantiate objects from other classes, such as ADO Connection and Recordset objects. In fact, your applications can instantiate multiple copies of a custom class at the same time—just like the ADO classes.) After instantiating a class, the code in your host procedure manipulates the instance of the class, not the class itself. You can change a property in one instance of a form, but when you instantiate a second instance of the form it appears with the default property setting.
Although VBA in Access lets you build custom classes with their own properties and methods, you cannot build custom events within those classes. You can, however, design a class that hooks onto a built-in class or type library that you attach. For example, you can build a class module that launches VBA code in response to the
ItemAdded
and
ItemRemoved
events of the
References
collection. This collection tracks links to external type libraries and ActiveX controls. After referencing a library, such as the Microsoft ActiveX Data Objects 2.1 Library, you can build custom events around the ADO events for the
Connection
and
Recordset
objects. These events can enable asynchronous data access that lets your application respond to users even while it remains ready to respond to a completed connection or the availability of a
You use the
WithEvents
keyword within a
Public
declaration to point to an object reference that