Every Class module comes with two predefined events: Initialize and Terminate.
You can find the event procedure stubs for these two events in the class module's
code window by clicking the Objects drop-down list (left-hand list box at the top of the code
window) and choosing the Class object.
The Procedures drop-down list (right-hand list box at the top of the code window) then displays
the Initialize and Terminate event procedure stubs, which you may navigate to with the mouse (see
FIGURE 12.4 Navigating to a class module's Initialize and Terminate event procedures.
The Initialize Event
The Initialize event happens whenever a routine in another part of the application or
in an ActiveX client uses your class module to instantiate a new copy of an object.
You therefore want to put code in the Initialize event procedure that is appropriate
You might set default initial values of properties from the Windows Registry or Private
constants, for instance. If your class accesses data, you might open database files here. The
example in Listing 12.6 initializes a classwide Private variable that is used as the storage for
a pair of Property Let/Property Get procedures.
INITIALIZING A PROPERTY'S VALUE IN THE CLASS MODULE'S INITIALIZE EVENT PROCEDURE
You should remember that the Initialize event takes place before your object is completely
instantiated. Therefore, it is not a good place to do things such as instantiating another object
variable of the same class: This would cause an infinite recursive loop, as successive copies
of the object would try to instantiate each other.
The Terminate Event
The Terminate event happens whenever a routine in another part of the application or
in an ActiveX client destroys the object instance of your class module. An object gets destroyed
in one of two ways:
You might use the Terminate event procedure to close data files or write information
to the Windows Registry or INI files, or just to give a farewell message to your