Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Passing an error from a class to a calling form.

Posted on 2000-04-25
4
Medium Priority
?
155 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
  • 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 200 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

916 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