Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error Trapping and Sub Procedures 2

Status
Not open for further replies.

sabloomer

Technical User
Aug 8, 2003
153
US
I have a procedure and basically organizes the sub-procedures for a nightly process. When there is an error in a sub-procedure the error trapping on that sub-procedure calls another sub procedure to write to the log file and e-mail me. I can use the "exit sub" to end the sub-procedure, but how do I let the procedure that called that sub procedure know that there was an error so it can do what needs to be done?

Thank You

sabloomer
 
You may test the value of Err.Number

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
OOps, sorry, hit submit too fast.
You may play with a global (public) variable set by the sub-procedure's error handler.
Another way is to convert your sub-procedures to functions returning a completion code.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV,

Thank you. I will try using the global variable first. It sounds like less of a pain then changing eveything to a function.

sabloomer
 
Leave error trapping in the main procedure and remove it from the 'sub-procedures'.

Or...maybe better...raise an error in the error handling routine of the sub-procedures...




This way, any error from the sub-procedure will be handled in the main procedure.

Set a local string variable in the main sub to know what 'subprocedure' is running and use the variable to write to the log file.

Code:
Sub MainProcedure()
    On Error GoTo err_handler
    Dim strSubName As String
    
    
    strSubName = "SubName1"
    Call SubName1

    strSubName = "SubName2"
    Call SubName2


exit_here:
   Exit Sub

err_handler:

    MsgBox "An error has occured in " & strSubName & ": " & Err.Number & Err.Description
    'Do what needs to be done
    Resume Next


End Sub

Sub SubName1()

    On Error GoTo err_handler
    
    MsgBox "Hello World"
    Err.Raise 10000, , "Just a test for Sub1"

exit_here:

    Exit Sub

err_handler:
    MsgBox "An error has occured"
    Err.Raise Err.Number, , Err.Description
End Sub

Sub SubName2()

On Error GoTo err_handler
    MsgBox "Hello Earth"
    Err.Raise 20000, , "Another test for Sub2"

exit_here:

    Exit Sub

err_handler:
    MsgBox "An error has occured"
    Err.Raise Err.Number, , Err.Description

End Sub

HTH

[pipe]
Daniel Vlas
Systems Consultant

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top