O'Reilly logo

Access™ 2007 VBA Programmer's Reference by Armen Stein, Geoffrey Griffith, Rob Cooper, Teresa Hennig

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

13.2. Why Use Classes?

From a coding perspective, the only real difference between using the built-in Access or VBA objects and the ones you write yourself, is that you have to instantiate your custom objects. Other than that, there's no difference at all.

There is a learning curve associated with creating your own class objects, but once learned, the major benefit is much simpler and more manageable code. Let's say you are using API functions in your application. You can create your own interface that hides the complexity of the API functions with a class. By this same token, classes are also very useful if you are writing code that will be used by other developers.

Also, while you can instantiate the built-in objects, using the Dim construct, you don't always have to. For example, to expose the Name property of a Table object, either of the following examples will work.

MsgBox DBEngine(0)(0).TableDefs(1).Name

Set tdf = DBEngine(0)(0).TableDefs(1)

Admittedly, if you've never written classes before, using them requires a different way of thinking at first. Once you become familiar with the concepts, you'll find great benefit in their usage. Once written, classes provide increased reusability and a layer of abstraction that enables you to focus more on business logic or rules. The end result is code that is easier to use. The Recordset class in DAO is an example of a class that is used quite frequently. So why not write your own?

Now having just expounded the virtues of adopting ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required