Initialize Event — Event Procedures

Description

To create the event procedure for the Initialize event, open the class module and click Class in the Object box. Then click Initialize in the Procedure box.

Syntax

Private Sub Class_Initialize( )

Remarks

You can use this event procedure to run code when an instance of the class is created, or to initialize any data used by the instance of the class.

The Initialize event occurs when you create a new instance of a class by using the New keyword. For example, suppose you have a class named CustomObject. You can add a declaration statement to a procedure in a standard module that creates a new instance of the class CustomObject, as shown in the following example:

Dim obj As New CustomObject
When you run the procedure that contains this declaration, Visual Basic creates a new instance of the CustomObject class, and the Initialize event occurs.

The Initialize event also occurs when you create an instance of a class by setting or returning a property or applying a method defined in the class module. For example, suppose you've defined a Function procedure named ListNames within the CustomObject class module. To run this function from another module, you must qualify it with the name of the class module, as shown in the following example:

CustomObject.ListNames
Qualifying the function with the name of the class module creates an instance of the class module, and the Initialize event occurs.

Example

The following example includes event procedures for the ItemAdded and ItemRemoved events. To try this example, first create a new class module by clicking Class Module on the Insert menu. Paste the following code into the class module and save the module as RefEvents:

' Declare object variable to represent References collection.
Public WithEvents evtReferences As References

' When instance of class is created, initialize evtReferences variable.
Private Sub Class_Initialize()
    Set evtReferences = Application.References
End Sub

' When instance is removed, set evtReferences to Nothing.
Private Sub Class_Terminate()
    Set evtReferences = Nothing
End Sub

' Display message when reference is added.
Private Sub evtReferences_ItemAdded(ByVal Reference As Access.Reference)
    MsgBox "Reference to " & Reference.Name & " added."
End Sub

' Display message when reference is removed.
Private Sub evtReferences_ItemRemoved(ByVal Reference As Access.Reference)
    MsgBox "Reference to " & Reference.Name & " removed."
End Sub
The following Function procedure adds a specified reference. When a reference is added, the ItemAdded event procedure defined in the RefEvents class runs.

For example, to set a reference to the calendar control, you could pass the string "C:\Windows\System\Mscal.ocx", if this is the correct location for the calendar control on your computer.

' Create new instance of RefEvents class.
Dim objRefEvents As New RefEvents

' Pass file name and path of type library to this procedure.
Function AddReference(strFileName As String) As Boolean
    Dim ref As Reference

    On Error GoTo Error_AddReference
    ' Create new reference on References object variable.
    Set ref = objRefEvents.evtReferences.AddFromFile(strFileName)
    AddReference = True

Exit_AddReference:
    Exit Function

Error_AddReference:
    MsgBox Err & ": " & Err.Description
    AddReference = False
    Resume Exit_AddReference
End Function
The next Function procedure removes a specified reference. When a reference is removed, the ItemRemoved event procedure defined in the RefEvents class runs.

For example, to remove a reference to the calendar control, you could pass the string "MSACAL," which is the name of the Reference object that represents the calendar control.

Function RemoveReference(strRefName As String) As Boolean
    Dim ref As Reference

    On Error GoTo Error_RemoveReference
    ' Return object representing existing reference.
    Set ref = objRefEvents.evtReferences(strRefName)
    ' Remove reference from collection.
    objRefEvents.evtReferences.Remove ref
    RemoveReference = True

Exit_RemoveReference:
    Exit Function

Error_RemoveReference:
    MsgBox Err & ": " & Err.Description
    RemoveReference = False
    Resume Exit_RemoveReference
End Function