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 list and then select the event from the Procedure drop-down list, as shown in Figure 12-9.

Figure 12-9

The following example shows what the Initialize and Terminate events might look like our friend the Kennel class.

Option Compare Database Option Explicit

'Declare the Gate object Private mobjGate As clsGate

'Declare the Dog object Private mobjDog As clsDogs

'Declare the KennelID Private mlngKennelID As Long

Private Sub Class_Initialize() mlngKennelID = 4

Set mobjGate = New clsGate Set mobjDog = New clsDogs End Sub

Private Sub Class_Terminate() Set mobjGate = Nothing Set mobjDog = Nothing End Sub

0 0

Post a comment