MAKE no mistake about it—the addition of class modules is the most significant developer enhancement in Access 97 (and the rest of the Office applications). What makes class modules so useful? Several benefits come to mind. Class modules allow you to create your own object models that look and behave just like Microsoft’s. So, instead of using wrapper functions such as CopyToClipboard and PasteFromClipboard, you can use syntax such as Clipboard.SetText and Clipboard.GetText.
Another great thing about class modules is that you can reuse your code—your objects—without having to worry about re-entrancy or stepping on your variables. That’s because each instantiation of your class is completely separate from all other instantiations. For example, say you have a simple timer object that’s a wrapper around the Windows API timer calls. Using a standard module, you might place the starting time in a module-level variable. But what happens if you call that code a second time before the first process is done? Either you have chaos or you need to create a system, using an array or a collection, to track multiple clocks. If you use a class module, however, you don’t have to worry about any of this because each timer object is independent of all other timers and has its own data (that is, variables).
In this issue’s lead article, veteran Access and VB programmer Jim Ferguson walks you through the creation of a class that makes Access’ unbound list boxes behave like VB’s. This is a perfect example of how much more useful classes are than standard modules. Imagine if this were done with standard modules and you wanted to use the code for multiple list boxes on a form. With the class approach, this is all taken care of by VBA—automatically.
Access/Office 97’s class modules (as well as VB 4.0’s) are missing one significant feature: inheritance. Without inheritance, you can’t create subclasses that inherit the properties and methods of their parent class, thus nullifying one of the foundations of object-oriented programming. I hope this serious shortcoming of VBA classes will be fixed sometime soon (that is, by the turn of the century). Nonetheless, class modules are still a very significant addition to VBA and Access. As you upgrade your existing Access applications to Access 97, be sure to take advantage of them. Once you buy into class modules, you’ll wonder how you ever coded without them.
Other Articles that May Interest