How would I fire an event when an error occurs in a class module?
Printable View
How would I fire an event when an error occurs in a class module?
This should give you the idea. Create a new project. Add a class mod. Put a commandbutton onto Form1 and add the following code to the class and form.
VB Code:
' Class mod code Public Event BadMagic(strError As String) Public Sub Hey() Dim x As Long On Error GoTo MyError ' change this to x = 1 /1 to see that event is only called on error x = 1 / 0 MsgBox "Calculation a-ok" Exit Sub MyError: RaiseEvent BadMagic("Divide by zero error") End Sub ' form code ' need this to specify that class has events Private WithEvents c As class1 Private Sub c_BadMagic(strError As String) ' just popup error msg MsgBox strError End Sub Private Sub Command1_Click() ' creat new class Set c = New class1 ' call class func for processing ' if error in this func then c_BadMagic is called c.Hey End Sub
Thanks!