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 ...

Get Access™ 2007 VBA Programmer's Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.