Option Explicit
'Error Handler- standardized error processing across an application
'==================================================
'Your comments are appreciated; you can reach the author at:
'tconlon3@yahoo.com April 16, 2001
'This methodology & source code are provided to you "FREE" subject
'to the Terms & Conditions of Use listed below.
'This procedure provides a methodology for the processing of errors
'in an application. As you build a subroutine simply include the error
'handling structure provided and all of your errors are handled in a
'standard way. The subroutine 'on error' code section invokes the
'subroutine "sub ErrorHandler()"; it in turn calls "sub MsgLogHandler()"
'to post various messages in a log file. All source code required for this
'methodology is included in this write-up.
'Sub ErrorHandler()
'Before You 'call' the error handler you set 3 variables which it needs
'to do its work:
'errorSeverity$ "WARNING" or "FATAL" (you decide whether the
' or not the condition is "FATAL" to continued
' processing.
'errorUserInstruct$ free form user instructions or message to be
' displayed by the error handler.
' For example:
' "Exit this function & start over"
' "unanticipated error- Stop Program"
' "Invalid DataBase Operation"
'errorFromProcedure$ Name of procedure or subroutine in which
' the error occurred.
'Note: within the error handler you can add code which intercepts specific
'error codes. Please refer to the ErrorHandler subroutine included below.
'Sub MsgLogHandler()
'The MsgLogHandler outputs messages to the log file. It requires 1
'variable:
'MsgLogRequest 1 set by you during program housekeeping or
' start-up. This causes the message log
' to be opened.
' 2 set by you at program closedown or set by
' the ErrorHandler when it has processed a
' "FATAL" in variable errorSeverity$. This causes
' the message log to be closed.
' 3 Records the message into the message log.
'PROGRAM TRACE 4 Records a Trace message into the message log.
' Up to 4 messages are allowed using variables:
' msglogString1$, msglogString2$, msglogString3$,
' msglogString4$ Only msglogString1$ is required.
'Note: the TRACE function is described below.
'Program Trace (for debugging your application).
'An additional function is provided in this methodology..it's program
'tracing. At program startup (or really anywhere in your program) you
'set variable msglogTraceProcedure = True. In each routine you describe
'the subroutine name, etc. in variable msglogString1$, set variable
'MsgLogRequest = 4 (Trace requested) and call MsgLogHandler where it will
'record the message in the message log file for you. Note: 3 additional
'strings are available for your use; msglogString2$, msglogString3$ &
'msglogString4$.
'Miscellaneous Variables
'You will also have to initialize the following variables:
'userID$
'userWorkingDir$
'Code Segments
'Declarations:
'==========================
Public errorFromProcedure$
Public errorSeverity$
Public errorChoice$
Public errorUserInstruct$
Public errorContinueInstruct$
Public errorHoldError As Long
Public errorHoldProcedure$
Public userID$ 'unique prefix for file systems (to avoid dup names)
Public userWorkingDir$
Public msglogTraceProcedure ' initialized as "0" (False), activated if "1" (True)
Public msglogMsgHeader$
Public msglogString1$
Public msglogString2$
Public msglogString3$
Public msglogRequest ' 1- open file, 2- close file, 3- write msg,
' 4- Procedure Trace FILE is #1
Public msglogFileName$
Public msglogSessionDateTime$
Public dbMaxLockedRetries 'maximum Allowable Locked DB Retry Count
Public dbLockedRetry ' (note set this to zero as you begin each module where
'update/add activity occurs)
'main:
'==========================
Sub main()
' Housekeeping/ Initialization
' this is standard code ----------------------------------------
msglogSessionDateTime$ = Date$ & "_" & Time$
msglogFileName$ = "xxxx_msglog.txt" ' Environment
userID$ = "TC" ' Environment
userWorkingDir$ = "c:\" ' Environment
msglogTraceProcedure = 1 ' Environment Trace of all Procedures executed is requested
msglogRequest = 1
Call MsgLogHandler
dbLockedRetry = 0 'Set to zero as you enter a module that adds/ updates DBs
dbMaxLockedRetries = 5 ' Environment
On Error GoTo MainRoutineErrors
errorFromProcedure$ = "Main Routine"
errorContinueInstruct$ = "RESUME NEXT"
errorSeverity$ = "FATAL"
errorUserInstruct$ = ""
errorChoice = ""
msglogRequest = 3
' FOR EXAMPLE FOR EXAMPLE
msglogString1$ = "Login: User: " & Login.LogonID.Text & " ID Used: " & Login.Password.Text
Call MsgLogHandler
If msglogTraceProcedure Then
msglogRequest = 4
Call MsgLogHandler
End If
' ===================================================
'your code here
'====================================================
Exit Sub
MainRoutineErrors:
If msglogTraceProcedure Then
errorHoldError = Err
errorFromProcedure$ = "ErrorHandler"
msglogRequest = 4
Call MsgLogHandler
Err = errorHoldError
End If
errorFromProcedure$ = "Main Routine"
Call ErrorHandler
If errorContinueInstruct$ = "RESUME NEXT" Then
Resume Next
Else
Resume
End If
End Sub 'MAIN
'TrialBalanceRoutine: this is a sample subroutine
'==================================================
Sub TrialBalanceRoutine()
On Error GoTo TrialBalanceRoutineErrors
errorFromProcedure$ = "TrialBalanceRoutine"
If msglogTraceProcedure Then
msglogRequest = 4
Call MsgLogHandler
End If
errorContinueInstruct$ = "RESUME NEXT"
errorSeverity$ = "FATAL"
errorUserInstruct$ = ""
errorChoice = ""
'this is application specific code...blah....blah
ReDim TrialBalanceDetail(1) As TBDetail
ReDim TrialBalanceComment(1) As TBComment
ReDim TrialBalanceRecap(1) As TBRecap
Exit Sub
TrialBalanceRoutineErrors:
If msglogTraceProcedure Then
errorHoldError = Err
errorFromProcedure$ = "ErrorHandler"
msglogRequest = 4
Call MsgLogHandler
Err = errorHoldError
End If
errorFromProcedure$ = "TrialBalanceRoutine"
Call ErrorHandler
If errorContinueInstruct$ = "RESUME NEXT" Then
Resume Next
Else
Resume
End If
End Sub 'Trial Balance Routine
'Error Handler:
'========================
Sub ErrorHandler()
' Determine Severity of Error
'errorFromProcedure$ = "ErrorHandler"
If msglogTraceProcedure = True Then
msglogRequest = 4
Call MsgLogHandler
End If
'errorContinueInstruct$ = "RESUME NEXT"
'errorSeverity$ = "FATAL"
'errorUserInstruct$ = ""
'errorChoice$ = ""
Select Case Err
Case 52, 76
errorContinueInstruct$ = "RESUME"
errorSeverity$ = "WARNING"
errorUserInstruct$ = " File Not Found "
Case 3021
errorContinueInstruct$ = "RESUME NEXT"
errorSeverity$ = "WARNING"
errorUserInstruct$ = " No Records Found "
Case 3186, 3260
errorSeverity$ = "WARNING"
errorUserInstruct$ = " Database Has Locked this Record; Approve Data for Update Again'"
dbLockedRetry = dbLockedRetry + 1
If dbLockedRetry > dbMaxLockedRetries Then
errorSeverity$ = "FATAL"
errorUserInstruct$ = " DB remains LOCKED .. Error STOP PROGRAM "
End If
Case 3197
errorSeverity$ = "WARNING"
errorUserInstruct$ = " Exit this Menu & Start this Function Again"
Case 3420
errorSeverity$ = "WARNING"
errorUserInstruct$ = " Invalid db 'CLOSE' Requested"
Case Else
errorSeverity$ = "FATAL"
errorUserInstruct$ = " Unanticipated Error STOP PROGRAM"
End Select
MsgBox Error$(Err) & " " & Err & " Procedure:: " & errorFromProcedure _
& " Severity: " & errorSeverity$, 64, errorUserInstruct$
msglogString1$ = Error$(Err) & " " & Err & " Procedure: " & errorFromProcedure$ _
& " Severity: " & errorSeverity$ & " " & errorUserInstruct$
msglogRequest = 3
If errorFromProcedure$ <> "MsgLogHandler" Then
Call MsgLogHandler
End If
If errorSeverity$ = "FATAL" Then
msglogRequest = 2
Call MsgLogHandler
End
End If
End Sub 'ErrorHandler
'MsgLogHandler
'================================================
Sub MsgLogHandler()
' Perform Requested Action 1- open file, 2- close file, 3- write msg,
' 4- Procedure Trace FILE is #1
On Error GoTo MsgLogErrors
msglogMsgHeader$ = Date$ & " " & Time$ & ".."
Select Case msglogRequest
Case 1
errorSeverity$ = "FATAL"
Open userWorkingDir$ & userID$ & "_" & msglogFileName$ For Output As #1
Print #1, msglogMsgHeader$ & " " & userID$ & "_" & msglogFileName$ & " " & "OPENED"
Case 2
errorSeverity$ = "FATAL"
Print #1, msglogMsgHeader$ & " " & userID$ & "_" & msglogFileName$ & " " & "CLOSED"
Close #1
Case 3
' errorSeverity$ = "FATAL"
Print #1, msglogMsgHeader$ & " " & msglogString1$
If msglogString2$ <> "" Then
Print #1, msglogMsgHeader$ & " " & msglogString2$
End If
If msglogString3$ <> "" Then
Print #1, msglogMsgHeader$ & " " & msglogString3$
End If
Case 4
' errorSeverity$ = "FATAL"
msglogString1$ = " Procedure: " & errorFromProcedure$
Print #1, msglogMsgHeader$ & " " & msglogString1$
Case Else
errorSeverity$ = "FATAL"
msglogString1$ = " Procedure: " & errorFromProcedure$ & " Bad msglogREQUEST"
Print #1, msglogMsgHeader$ & " " & errorFromProcedure$
End Select
msglogString1$ = ""
msglogString2$ = ""
msglogString3$ = ""
msglogRequest = 0
Exit Sub
MsgLogErrors:
errorFromProcedure$ = "MsgLogHandler"
errorSeverity$ = "FATAL"
Call ErrorHandler
End
End Sub 'MsgLogHandler
'===================
'TERMS & CONDITIONS
'===================
'Terms & Conditions for use: this methodology & source code are provided
'to to you "FREE" without any restriction. Author makes no specific or
'implied claim to this program's usefulness or merchantability.
'User of this software holds the author & TEC Associates 'harmless' in
'any litigation related to the software. Related litigation expenses
'incurred by the author or TEC Associates will be paid for by the party
'initiating the litigation. Use of this software constitutes
'acceptance of the statements & terms/conditions listed above.
|