I'm trying to make a custom class that handles the events of all combo boxes, but I also want the combo boxes to run their own individual event handlers. To do this, I've created a new custom class, added the desired event handlers to the custom class, and initialised all of my combo boxes into this custom class. I then store all of these new custom objects in a collection. This works fine. Any time a combo box is updated, it runs its own OnUpdate event handler followed by the custom class OnUpdate event handler.
The issue is that as soon as an unhandled error occurs, the collection is deleted, which also means each combo box is no longer a member of the custom class. This means any time an unhandled error occurs and the user continues to use the application, only the combo box's handler runs.
Is there a way to store these object refences even after an unhandled error? To my understanding, I can't use tempvars because this only stores values not object references.
The code in my custom class:
Option Compare Database
Private WithEvents mCmbo As ComboBox
Public Property Get Combo() As ComboBox
Set Combo = mCmbo
End Property
Public Property Set Combo(ByVal TheCombo As ComboBox)
Set mCmbo = TheCombo
End Property
Public Sub Initialize(TheCombo As ComboBox)
Set Me.Combo = TheCombo
Me.Combo.OnEnter = "[Event Procedure]"
Me.Combo.BeforeUpdate = "[Event Procedure]"
Me.Combo.AfterUpdate = "[Event Procedure]"
End Sub
'------------------------------------------------------ Trap Events --------------------------------------
Private Sub mCmbo_BeforeUpdate(Cancel As Integer)
MsgBox "Before update Trapped in custom class "
End Sub
Private Sub mCmbo_AfterUpdate()
MsgBox "After update Trapped in custom class."
End Sub
The code used to initialise my combo box's into this custom class:
Private Sub Form_Load()
Dim ctrl As Control
'Assigning the custom class to the combo box
For Each ctrl In Forms!Form1.Controls
If ctrl.ControlType = acComboBox Then
Debug.Print ctrl.Name
Set CC = New TestClass
CC.Initialize ctrl
ccCollection.Add CC
End If
Next ctrl
End Sub
Private Sub Combo4_AfterUpdate()
MsgBox "Combo update event"
End Sub
Private Sub Combo4_BeforeUpdate(Cancel As Integer)
MsgBox "Combo BeforeUpdate event"
End Sub
I've looked into tempvars, database properties and a hidden form, but I believe none of these can store object variables.
CodePudding user response:
Nope.
An unhandled error means something went wrong, and the programmer didn't anticipate that ever being able to go wrong (else they should've added an error handler), so the entire application shouldn't be trusted. VBA is weird in that it allows users to continue on unhandled errors, but unhandled errors really shouldn't happen in an application distributed to users, and if they do, the application should ideally shut down or restart (which does happen for the runtime versions of Access or compiled databases).
What you should do, of course, is handle these errors. And if you don't anticipate an error, the proper handling part is most commonly to log the error then shut down the application.
A limitation of VBA is that we can't have global error handlers to handle any otherwise unhandled exception, unfortunately, so you will have to add error handlers to each function being called directly from the application. Functions that are only called by other functions can have their errors handled in the outer function.