2
votes

I have some trouble with user-defined events in Access 2007:

The events are declared within a class module - let's name this class Controller - as follows (simplified code examples):

Public Event EventA()

[...]

Public Property Let PropertyA(RHS As String)
    mPropertyA = RHS
    RaiseEvent EventA
End Property

[...]

The class is instantiated in a module as a "self-healing" object as follows:

Public Property Get objController() As Controller
    Static c As Controller

    If c Is Nothing Then _
        Set c = New Controller

    Set objController = c
End Property

In a form the Controller class is declared and set within the sub Form_Load() as follows:

Private WithEvents mController  As Controller

[...]

Private Sub Form_Load()
    [...]
    Set mController = objController
    [...]
End Sub

Within the same form I implemented the event actions:

Private Sub mController_EventA()
    [...]
    Me!PropertyA = mController.PropertyA
    [...]
End Sub

After clicking a button on the form a dialog form with a treeview is opened. After clicking a node in the treeview PropertyA in the Controller object is changed:

Private Sub tvwRDS_NodeClick(ByVal node As Object)
    [...]
    objController.PropertyA = node.key
    [...]
End Sub

My intention was this:

  1. You click a node.
  2. PropertyA of instantiated class Controller is set, and event EventA() is raised.
  3. Main form is handling the event; controls in the form are updated.

The first time everything worked as intended. After using the Compact and Repair feature as well as after compiling and creating an ACCDE file the sub mController_EventA() seems to be lost in space: nothing happens after the event was fired. Why that?!

The WithEvents clause is only allowed in object modules. But I need to instantiate self-healing objects from a normal module.

Thanks a lot in advance!

D.C.

1
@Andre: Thanks for supervising and the hint! :-)D.C.

1 Answers

1
votes

It seems that the Property itself must be declared as Static:

Public Static Property Get objController() As Controller
    Static c As Controller

    If c Is Nothing Then _
        Set c = New Controller

    Set objController = c
End Property

Since then it works fine.