IfThenElse
IfThenElse

Reputation: 509

Access vba wrapper class handling events

I'm try to write a wrapper class for getting events. This is a not working (EDIT: not show the message box) simple example: Where is the mistake?

' CLASSE1
Private WithEvents frm As Access.Form
Private Const Evented As String = "[Event Procedure]"
Public Sub Init(pFrm As Access.Form)
    Set frm = pFrm
    frm.OnLoad = Evented
End Sub
Private Sub frm_Load()
    MsgBox "OK!" 'NOT SHOW
End Sub

'Form1
Private SL As Classe1
Private Sub Form_Load()
    Set SL = New Classe1
    SL.Init Me
End Sub

Upvotes: 0

Views: 422

Answers (1)

Gustav
Gustav

Reputation: 55831

You have mixed up the form load. This works:

Class1:

Option Compare Database
Option Explicit

Private WithEvents frm As Access.Form
Private Const Evented As String = "[Event Procedure]"

Public Sub Initialize(pFrm As Access.Form)
    Set frm = pFrm
    frm.OnCurrent = Evented
End Sub

Public Sub Terminate()
    Set frm = Nothing
End Sub

Private Sub frm_Current()
    MsgBox "OK"
End Sub

Form1:

Option Compare Database
Option Explicit

Private FormCollection   As Collection

Private Sub Form_Load()

    Dim EventProcedure  As Class1
    
    Set EventProcedure = New Class1
    Set FormCollection = New Collection

    EventProcedure.Initialize Me.Form
    FormCollection.Add EventProcedure, Me.Name

    Set EventProcedure = Nothing
    
End Sub

Private Sub Form_Unload(Cancel As Integer)

    ' Unload events for all colour value textboxes.
    
    Dim EventProcedure  As Class1
    
    For Each EventProcedure In FormCollection
        EventProcedure.Terminate
    Next
    
    Set EventProcedure = Nothing
    Set FormCollection = Nothing

End Sub

Upvotes: 1

Related Questions