Solved

Passing an error from a class to a calling form.

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
LINQ return type for nested group query 6 74
Vb.net 2008 2 56
SSRS expression Issue finding a string 10 66
MS Access - Capture pressed key onclick 4 29
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now