Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
215 views
in Technique[技术] by (71.8m points)

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
question from:https://stackoverflow.com/questions/65951446/access-vba-wrapper-class-handling-events

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...