Solved

Passing an error from a class to a calling form.

Posted on 2000-04-25
4
145 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
Comment Utility
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
Comment Utility
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
Comment Utility
Comment accepted as answer
0
 

Author Comment

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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

771 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

16 Experts available now in Live!

Get 1:1 Help Now