Solved

Passing an error from a class to a calling form.

Posted on 2000-04-25
4
148 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 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…

856 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