BXL's DspErrMsg()
This is my central error handler. It is called in every function and subroutine I write. I place it in my general purpose library: modGeneral.
At the top of modGeneral are these global constants and variables.
'Global Constants
Global Const Success As Boolean = False
Global Const Failure As Boolean = True
Global Const NoError As Long = 0 'No Error
Global Const LogError As Long = 997 'Log Error
Global Const RtnError As Long = 998 'Return Error
Global Const DspError As Long = 999 'Display Error
'Global Variables
Public bLogOnly As Boolean 'See DspErrMsg
Public bDebug As Boolean 'See DspErrMsg
This is the main function that basically displays a message box formatted based on what the Err object contains and if we want to put our project in debug mode. It returns the button the user clicks: vbAbort, vbCancel, vbIgnore, vbRetry
Public Function DspErrMsg(ByVal sRoutineName As String, _
Optional ByVal sAddText As String = "") as vbMsgBoxResult
' Description:Display an unanticipated error message
' Inputs: sRoutineName Function or Subroutine's name
' sAddText Additional Text
' Outputs: *None
' Requisites: Global bLogOnly
' bDebug
' Note! This cannot have an error handler
' Example: Select Case DspErrMsg(cModule & "." & cRoutine)
' Case Is = vbAbort: Stop: Resume 'Debug mode - Trace
' Case Is = vbRetry: Resume 'Try again
' Case Is = vbIgnore: 'End routine
' End Select
' Date Ini Modification
' 11/08/11 CWH Initial Development
' 02/20/19 CWH Externalized bDebug and bLogOnly
' Procedure
If bLogOnly Then
Debug.Print Now(), ThisWorkbook.Name & "!" & sRoutineName, Err.Description, sAddText
Else
DspErrMsg = MsgBox( _
Prompt:="Error#" & Err.Number & vbLf & Err.Description & vbLf & sAddText, _
Buttons:=IIf(bDebug, vbAbortRetryIgnore, vbCritical) + _
IIf(Err.Number < 1, 0, vbMsgBoxHelpButton), _
Title:=sRoutineName, _
HelpFile:=Err.HelpFile, _
Context:=Err.HelpContext)
End If
End Function