Attribute VB_Name = "Errors" '*********************************************************************** '*********************************************************************** 'Everything that has to do with error handling '*********************************************************************** '*********************************************************************** '***** Custom Errors enum ***** 'This method of raising custom errors ensures that ' error numbering and descriptions are consistant ' and make sense, because one can see them all together Public Enum enumErrorNumber 'CatchAllForAnyError AAMiscError = 5000 'file errors FileAlreadyOpen = 5001 FileNotOpen = 5002 EndOfFile = 5003 DeleteOpenFile = 5004 RenameOpenFile = 5005 'databse and SQL errors GetRecordsetFailed = 5010 InvalidKeyForNewRecord = 5011 'printer errors OpenPrinterFailed = 5020 'misc errors EmptyStringPassed = 5030 InvalidEnumPassed = 5040 InvalidActionEnumPassed = 5041 DocNotFoundInCollection = 5050 INISaveFailed = 5060 INIDuplicateKey = 5061 INISettingNotFound = 5062 WaitForSpoolTimeOut = 5070 OperationCanceledByUser = 5100 End Enum 'These are the options chosen in the ini file for eventlog level Public Enum LogLevelOptions None = 0 Error = 1 Warning = 2 Information = 3 End Enum Option Explicit '*********************************************************************** ' Comments : Determins the description of a custom error and raises it ' This method of raising custom errors ensures that ' error numbering and descriptions are consistant ' and make sense, because one can see them all together ' Parameters : err number from global enum "enumErrorNumber" ' Returns : a string that is formatted for a sql statement ' Created : Enterprise Software Solutions [06/11/1999] ' Modified : '*********************************************************************** Public Sub RaiseCustomError(lErrorNumber As enumErrorNumber, _ Optional StrAdditionalText As String) On Error GoTo ErrorHandler Dim strErrorDescToRaise As String 'determine the error description Select Case lErrorNumber 'file errors Case enumErrorNumber.FileAlreadyOpen strErrorDescToRaise = "File already open" Case enumErrorNumber.FileNotOpen strErrorDescToRaise = "File not open" Case enumErrorNumber.EndOfFile strErrorDescToRaise = "End of file" Case enumErrorNumber.DeleteOpenFile strErrorDescToRaise = "Can not delete a file that is already open" Case enumErrorNumber.RenameOpenFile strErrorDescToRaise = "Can not rename a file that is already open" Case enumErrorNumber.DocNotFoundInCollection strErrorDescToRaise = "Specified document not found in collection" 'databse and SQL errors Case enumErrorNumber.GetRecordsetFailed strErrorDescToRaise = "Get Recordset failed when trying to run a SELECT query" Case enumErrorNumber.InvalidKeyForNewRecord strErrorDescToRaise = "The Primary Key of a new record can not be '0' or blank" 'Printer errors Case enumErrorNumber.OpenPrinterFailed strErrorDescToRaise = "Failed to open printer" 'INI errors Case enumErrorNumber.INISaveFailed strErrorDescToRaise = "Could not save INI setting" Case enumErrorNumber.INIDuplicateKey strErrorDescToRaise = "Key elready exists in INI file" Case enumErrorNumber.INISettingNotFound strErrorDescToRaise = "INI setting not found" 'misc errors Case enumErrorNumber.EmptyStringPassed strErrorDescToRaise = "An Empty String was passed" Case enumErrorNumber.InvalidEnumPassed strErrorDescToRaise = "Invalid Enum was passed" Case enumErrorNumber.InvalidActionEnumPassed strErrorDescToRaise = "Invalid Action Enum Passed. Must be between 1 and 3." Case enumErrorNumber.WaitForSpoolTimeOut strErrorDescToRaise = "Wait for Spooling to finish timed out" Case enumErrorNumber.OperationCanceledByUser strErrorDescToRaise = "Operation Canceled By User" 'undefined errors Case Else strErrorDescToRaise = "Misc Error" End Select 'raise the error On Error GoTo 0 Err.Raise lErrorNumber, "Custom Error", strErrorDescToRaise _ & vbCrLf & vbCrLf & StrAdditionalText Exit Sub ErrorHandler: ' MsgBox "Internal error in 'RaiseCustomError' function!" _ & vbCrLf & vbCrLf & "Click 'OK'" _ & " and the following error message will explain" RaiseSameError End Sub '*********************************************************************** 'Raises the current error '*********************************************************************** Public Sub RaiseSameError(Optional strErrorSource As String, _ Optional strErrorText As String) 'add return for "Source" (if exists) If strErrorSource <> "" Then strErrorSource = strErrorSource & " ==> " End If 'add return for "Description" (if exists) If strErrorText <> "" Then strErrorText = strErrorText & " ==> " End If 'raise error Err.Raise Err.Number, _ strErrorSource & Err.Source, _ strErrorText & Err.Description, _ Err.HelpFile, Err.HelpContext End Sub '*********************************************************************** ' Comments : Common Error Handler. Display error & log error to event log ' Parameters : err number, error description, source function, misc text ' parm "bNoMessageBox" is flagged true if no message box ' is wanted ' Returns : a string that is formatted for a sql statement ' Created : Enterprise Software Solutions [05/26/1999] ' Modified : '*********************************************************************** Public Sub Got_Error(strModule As String, strFunction As String, _ Optional strErrorText As String, Optional bNoMessageBox As Boolean) Dim strLogText As String Dim iErrorNumber As Long Dim strErrorDescription As String Dim strErrorSource As String 'store error into local veriable and clear the error object iErrorNumber = Err.Number strErrorDescription = Err.Description strErrorSource = Err.Source Err.Clear On Error GoTo ErrorHandler 'build string for audit log strLogText = "ERROR NUMBER: " & iErrorNumber _ & " ==> DESCRIPTION: " & strErrorDescription _ & " ==> SOURCE: " & strErrorSource _ & " ==> MODULE: " & strModule _ & " ==> FUNCTION: " & strFunction If strErrorText <> "" Then strLogText = strLogText & " ==> Text: " & strErrorText End If 'log the event to the event log LogEvent strLogText, vbLogEventTypeError 'display the error message, ' but don't diaply the message box if we are asked not to If bNoMessageBox = False Then ' MsgBox "* ERROR NUMBER: " & iErrorNumber _ & vbCrLf & "* DESCRIPTION: " & strErrorDescription _ & vbCrLf & "* SOURCE: " & strErrorSource _ & vbCrLf & "* MODULE: " & strModule _ & vbCrLf & "* FUNCTION: " & strFunction _ & vbCrLf & vbCrLf & strErrorText _ , vbCritical, App.Title End If Exit Sub ErrorHandler: ' MsgBox "Error: " & Err.Number _ & vbCrLf & "Description: " & Err.Description _ & vbCrLf & "Source: " & Err.Source _ & vbCrLf & "Module: Common" _ & vbCrLf & "Function: Got_Error" _ & vbCrLf & vbCrLf & "Log Event Failed!" Err.Clear End Sub '*********************************************************************** ' Comments : Keeps an event log ' INI file should contain LogLevelOptions: ' none = 0 ' Errors = 1 ' Warnings = 2 ' Information = 3 ' Parameters : text to log, level of the log being sent in ' Returns : nothing ' Created : Enterprise Software Solutions [05/26/1999] ' Modified : '*********************************************************************** Public Sub LogEvent(strLogText As String, _ Optional LogLevel As LogEventTypeConstants = vbLogEventTypeInformation) Dim objFileSystem As New FileSystemObject Dim objFileHandle As Object Dim LogOptionChosen As Integer Const ForReading = 1 Const ForAppending = 8 'this const Opens the file as ASCII Const TristateFalse = 0 Dim strLogFilePath As String On Error GoTo ErrorHandler '------------------------- ' MsgBox strLogText 'get event log options from ini file LogOptionChosen = CInt(GetINISetting("General", "EventLogLevel")) If LogOptionChosen <= LogLevelOptions.None _ Or LogOptionChosen > LogLevelOptions.Information Then 'if we are to log nothing, ' or if the ini file has invalid value, log nothing and leave now GoTo ExitCleanup End If 'determine if we should log this event or not ' (based on parm passed in and ini file setting) If LogLevel = vbLogEventTypeInformation _ And LogOptionChosen < LogLevelOptions.Information Then GoTo ExitCleanup End If If LogLevel = vbLogEventTypeWarning _ And LogOptionChosen < LogLevelOptions.Warning Then GoTo ExitCleanup End If 'get path of log file strLogFilePath = GetINISetting("General", "EventLogFile") If IsNothing(strLogFilePath) Then strLogFilePath = App.Path & "\" & App.Title & ".log" End If 'initialize NT event log (if we haven't already done so) If App.LogMode <> vbLogAuto Or _ App.LogPath <> strLogFilePath Then 'the log setting have not been initialized, so initialize them now App.StartLogging strLogFilePath, vbLogToNT 'vbLogAuto End If 'initialize event log file If objFileSystem.FileExists(strLogFilePath) = True Then Set objFileHandle = objFileSystem.OpenTextFile(strLogFilePath, _ ForAppending, TristateFalse) Else 'create new log file Set objFileHandle = objFileSystem.CreateTextFile(strLogFilePath) objFileHandle.WriteLine ("This is the Event Log file for " & App.Title _ & ". The format is : [TimeStamp] ==> [Log Text] ") 'use applicating logging also End If 'log the event objFileHandle.WriteLine (Format(Now, gblDateTimeDisplayFormat) _ & " ==> " & strLogText) 'log event to NT event log If CBool(GetINISetting("General", "LogToNT")) = True Then App.LogEvent strLogText, LogLevel End If ExitCleanup: On Error Resume Next 'close the event log and clean up objFileHandle.Close Set objFileHandle = Nothing Set objFileSystem = Nothing Exit Sub ErrorHandler: 'ignore errors Err.Clear Resume ExitCleanup End Sub