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.5. Using Class Events

Unlike standard modules, class modules can raise their own events. This is a very powerful feature of VBA, because it not only gives your code the ability to know what's going on inside the class instance, it provides the opportunity to take whatever actions you deem necessary based on those events.

Another very important benefit of using class events is that that you can keep User Interface (UI) functionality separate from the class implementation, making the class truly independent and reusable. You can then use your class in many places without worrying about specific UI implementation. This section focuses on getting your class to talk to the rest of your application through events.

13.5.1. Initialize and Terminate Events

Every class module has two built-in events that fire automatically: Initialize and Terminate. The Initialize event fires when the class instance is first created. You can use the Initialize event to set default property values and create references to other objects. The Terminate event fires before the object is destroyed, and is normally used to clean up local object references.

To define code for these events, select Class from the Object drop-down and then select the event from the Procedure drop-down, as shown in Figure 13-9.

Figure 13.9. Figure 13-9

The following example shows what the Initialize and Terminate events might ...

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