Solved

Passing an error from a class to a calling form.

Posted on 2000-04-25
4
153 Views
Last Modified: 2010-05-02
I have a form (MyForm) that instances a class module (MyClass) when an error occurs in the class I would like it to suppress that error and pass it onto MyForm, and let the form handle the error, because although the error will always be the same different forms need to handle the error in different ways.

Please give code examples.

Thanks.
0
Comment
Question by:BALAJI
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 6

Expert Comment

by:Marine
ID: 2748454
So if error occurs in class you want that error message to be send to your form and display it from there ? Try this. What it does if you call this method and there will be an error in the class , class will pass teh error descriptin which you can show to user from your form


FORM CODE-------------
sRet = objLog.LogIn(UIP(0), UIP(1), sErrLog)
        If sRet Then
            frmBook.Show
            Set objLog = Nothing
            Unload Me
        Else
            MsgBox sErrLog & " " & "try # " & iCount + 1
            UIP(0).SetFocus
            UIP(0).SelStart = 0: UIP(0).SelLength = Len(UIP(0))
            UIP(1) = ""
            iCount = iCount + 1
            If iCount = 3 Then
                Unload frmLogin
                Set frmLogin = Nothing
            End If
        End If

end form code=====================

Class CODE
Public Function LogIn(Uid As String, Pws As String, sErrLog As String) As Boolean
On Error GoTo err_login
Dim sLog As String
sLog = "SELECT UID,PWS,[Level] FROM ACCOUNT WHERE UID='" & Uid & "' AND pws='" & Pws & "'"
rsAcct.Open sLog, cnAcct, adOpenStatic, 4
If rsAcct.RecordCount = 0 Then
    sErrLog = "Wrong User Name or Password"
    rsAcct.Close
    cnAcct.Close
    LogIn = False
    Exit Function
End If
LogIn = True
Exit Function
err_login:
    sErrLog = "Unexpected Error Occured "
End Function
0
 

Accepted Solution

by:
mbkurtz earned 50 total points
ID: 2750840
If you do not enable error handling in your class module's property or method, VB will automatically pass the error to the calling routine which in your case, will be on the form.

Example:

'The Form code, perhaps behind a button
Private Sub Command1_Click()
 On Error Goto ErrorTrap

   Dim MyClass   as TestClass

    Set MyClass = New TestClass
    'If DoSomething causes a run-time
    'error, it will be handled by
    'ErrorTrap
    Call MyClass.DoSomething

ExitMe:
    Exit Sub

ErrorTrap:
    Select Case Err.Number
       'Place code to handle different
       'error numbers here.  The Err
       'object will have complete
       'information (Source, Desc,
       'Number, etc.)
    End Select
    Resume ExitMe

End Sub


'Code found in TestClass
Public Sub DoSomething()
   
    'Add code here that causes a
    'run-time error.  Notice that
    'there is no error handling (no
    'On Error statement.)  The error
    'will be raised to the calling
    'routine.

End Sub


In addition, if you want to raise custom errors back to the calling routine (again, in your case, the form) you can trap the error in the class module and use the Raise method of the Err object to generate a custom error number, source, and description.

Example:

'The Form code, perhaps behind a button
Private Sub Command1_Click()
 On Error Goto ErrorTrap

   Dim MyClass   as TestClass

    Set MyClass = New TestClass
    'If DoSomething causes a run-time
    'error, it will be handled by
    'ErrorTrap
    Call MyClass.DoSomething

ExitMe:
    Exit Sub

ErrorTrap:
    Select Case Err.Number
       'Place code to handle different
       'error numbers here.  Test for
       'your custom error numbers.
    End Select
    Resume ExitMe

End Sub


'Code found in TestClass
Public Sub DoSomething()
 On error Goto ErrorTrap
   
    'Add code here that causes a
    'run-time error.  The error
    'will be handled by ErrorTrap.

ExitMe:
    Exit Sub

ErrorTrap:
    'Convert a VB error into a custom
    'error number, source, and desc.
    'Raise the new error to the calling
    'routine.
    Select Case Err.Number
       Case 'Whatever error number
          Call Err.Raise(VbObjectError + 1001, "TestClass.DoSomething()", "Custom Description")
    End Select

End Sub

0
 

Author Comment

by:BALAJI
ID: 2871740
Comment accepted as answer
0
 

Author Comment

by:BALAJI
ID: 2871743
Sorry I took so long to grade... Your suggestions are great
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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 process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month3 days, 21 hours left to enroll

630 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