Solved

Error propagation between modules

Posted on 1998-08-18
3
355 Views
Last Modified: 2010-04-30
I have a form (a) that calls (in a different project) a class (b) that calls a form (c) that calls a class (d) ... When I raise an exception in class d it gets propogated back to form (c), but no further.  Books online hints that an error cannot be propagated further than a form.  Is this true? How can I get the error all the way back to form (a)?  I tried setting some sort of flag, but I can't seem to use the Unload command in an error handler, so I don't even know how to get out of the form.
0
Comment
Question by:gwilym
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
jsuper earned 100 total points
ID: 1429603
I have the same sort of thing going on with my classes and forms.  WHat I did was to create an Error event for the class, and when an error is trapped, I use the RaiseEvent method to return an error event to the calling class or method.  Also, I use the err.description, and the err.code as parms for the event to supply system information.  You could also just pass the error object itself as a parameter.  If you need some examples, just add a comment.
0
 

Author Comment

by:gwilym
ID: 1429604
Yup - examples sound good!
0
 
LVL 1

Expert Comment

by:jsuper
ID: 1429605
O.k.  Here is an example Imade up...   It works like a charm.  I'll show you the code, and then explain what is happening.  The project contains two forms and two classes.  The first form's code is below.  Think of it as a toybox containing buttons that when clicked, will create a toy class, and ask that a specific toy be removed from the toybox.

Private WithEvents vToy As Toys      'The toybox class defined
                                     ' with events!

Private Sub cmdBall_Click()          'Tells the toybox class to
    Set vToy = New Toys              ' get the ball out.
    vToy.Get_from_Toybox "Ball"
End Sub

Private Sub cmdTop_Click()            'Tells the toybox class to
    Set vToy = New Toys               'get the top!
    vToy.Get_from_Toybox "Top"
End Sub

                                      ' This is the only error
                                      ' message box in the
                                      ' whole project!

Private Sub vToy_Error(Code As Long, Description As String)
    MsgBox Code & " - " & Description, vbOKOnly, "Toy error!"
End Sub

Here is the code for the toybox class...  Note that it defines a variable to hold the form that is trying to return error information (frmBall)!

Public Event Error(Code As Long, Description As String)

Private WithEvents frmBall As frmBall       'Important!  This
                                            'is what allows the
                                            'form to return
                                            'events!

Public Sub Get_from_Toybox(Toy As String)
    If Toy = "Ball" Then
        Set frmBall = New frmBall            'Display the Ball
        frmBall.Show                         'Form.
    ElseIf Toy = "Top" Then
        '
    End If
End Sub

Private Sub Class_Initialize()
    Set frmBall = Nothing
End Sub

'  This event is fired when the frmBall Form raises the error
'  event!

Public Sub frmBall_Error(Code As Long, Description As String)
    RaiseEvent Error(Code, Description)
End Sub

Here is the code for the frmBall form.  It creates a class object of type Ball, and sets the appropriate properties and methods.  Notice the error event at the bottom which is fired when the ball class raises the error event...

Dim WithEvents vBall As Ball

Public Event Error(Code As Long, Description As String)

Private Sub cmdBounce_Click()
    Static force As Long
   
    force = force + 2
    vBall.Bounce force
End Sub

Private Sub Form_Load()
    Set vBall = New Ball
    vBall.Color = "Red"
    vBall.Size = 6
    txtColor = vBall.Color
    txtSize = vBall.Size
End Sub

Private Sub txtColor_Change()
    If txtColor.Text <> "" Then
        vBall.Color = txtColor.Text
    End If
End Sub

Private Sub txtSize_Change()
    If txtSize.Text <> "" Then
        vBall.Size = Val(txtSize.Text)
    End If
End Sub

Private Sub vBall_Error(Code As Long, Description As String)
    RaiseEvent Error(Code, Description)
End Sub

Last of all, here is the code for the Ball class.  Note that it traps for all kinds of errors and then uses the raiseevent function to propogate error events all the way back to the main form.  the main form then displays an error message MsgBox.  In fact, by putting error trapping in each subroutine, ALL of the classes and form errors could be routed back to one location.

Public Event Error(Code As Long, Description As String)
Private h_Color As String
Private h_Size As Long

Public Property Get Color() As String
    On Error GoTo Error
   
    Color = h_Color
    Exit Property
Error:
    RaiseEvent Error(Err.Number, Err.Description)
End Property

Public Property Let Color(ByVal vNewValue As String)
    On Error GoTo Error
   
    If vNewValue = "" Then
        RaiseEvent Error(1, "Color cannot be blank!")
    Else
        h_Color = vNewValue
    End If
    Exit Property
Error:
    RaiseEvent Error(Err.Number, Err.Description)
End Property

Public Property Get Size() As Long
    On Error GoTo Error
   
    Size = h_Size
    Exit Property
Error:
    RaiseEvent Error(Err.Number, Err.Description)
End Property

Public Property Let Size(ByVal vNewValue As Long)
    On Error GoTo Error
   
    If vNewValue < 1 Then
        RaiseEvent Error(1, "Size cannot be zeo or negative!")
    ElseIf vNewValue > 10 Then
        RaiseEvent Error(2, "Size cannot be larger than ten!")
    Else
        h_Size = vNewValue
    End If
    Exit Property
Error:
    RaiseEvent Error(Err.Number, Err.Description)
End Property

Public Sub Bounce(force As Long)
    On Error GoTo Error
   
    If force > 10 Then
        RaiseEvent Error(3, "You bounced it to hard!")
    End If
    Exit Sub
Error:
    RaiseEvent Error(Err.Number, Err.Description)
End Sub

Notice that in the error traps, I simply return the system error information back to the Main form.  For validation errors, I define my own error messages.  Good Luck, let me know if that all makes sense.
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question