The ErrGeneral Function

Public Function ErrGeneral( _
    Optional objErr As ErrObject, _
    Optional strModule As String, _
    Optional strProcedure As String, _
    Optional strLogFile As String, _
    Optional strMsgPrompt As String, _
    Optional intButtons As Integer, _
    Optional strMsgTitle As String, _
    Optional blnLogError As Boolean = True, _
    Optional blnDisplayMsg As Boolean = True _
    ) As Integer

    'Copyright (c) Brendan Reynolds/Timarco Ltd, 1999.
    'All rights reserved.

    'e-mail [email protected]
    
    'This function optionally logs to a CSV file and/or
    'displays error information. This function is designed
    'to handle VBA run-time errors. Use the function ErrData
    'to handle Access or DAO errors trapped in the Error
    'event of a form.
    
    'Arguments
    
    'objErr: The error, as returned by the Err object.
    
    'strModule: The name of the module in which the error
    'occured.
    
    'strProcedure: The name of the procedure in which the
    'error occured.
    
    'strLogFile: The pathname of the file in which error
    'information is to be logged. If no file is specified,
    'the pathname returned in CurrentDb.Name, but with an
    'extension of '.log', will be used.
    
    'strMsgPrompt: The text to be used as the prompt if the
    'error information is displayed in a message box.
    
    'intButtons: The buttons/icon to be displayed on the
    'messagebox.
    
    'strMsgTitle: The title of the messagebox.
    
    'blnLogError: If True, the error information will be
    'logged.
    
    'blnDisplayMsg: If True, the error information will be
    'displayed.
    
    'Return values
    
    'If the error information is displayed, returns the
    'value of the messagebox button clicked. Otherwise
    'returns zero.
    
    'Use
    
    'intResponse = ErrGeneral( _
    '   objErr:=Err, _
    '   strModule:= "SomeModule", _
    '   strProcedure:="SomeProcedure
    
    'Error information is both logged and displayed. The
    'default log file and message text are used.
    
    'To log the error but not display the error message,
    'set blnLogError to False. To display the error message
    'but not log it, set blnDisplayMsg to False. To
    'substitute a custom error message, pass the appropriate
    'values in the strMsgPrompt, intButtons, and strMsgTitle
    'arguments. To specify the name of the file in which
    'errors will be logged, pass the full path and name in
    'the strLogFile argument.
            
    Dim intFileNum As Integer
    Dim lngErrNum As Long
        
    lngErrNum = objErr.Number
    
On Error GoTo Err_Routine

    ErrGeneral = 0
    
    If blnLogError = True Then
    
        'Log the error information.
        If strLogFile = vbNullString Then
        
            'No file specified, so use the default.
            strLogFile = CurrentDb.Name
            strLogFile = Left$(strLogFile, _
                Len(strLogFile) - 3) & "log"
        End If
        intFileNum = FreeFile
        Open strLogFile For Append As intFileNum
        Write #intFileNum, "Run-Time Error", _
            lngErrNum, AccessError(lngErrNum), _
            strModule, strProcedure, Now(), CurrentUser()
        Close intFileNum
    End If
    If blnDisplayMsg = True Then
    
        'Display the error information.
        If strMsgPrompt = vbNullString Then
        
            'No prompt specified, so use the default.
            strMsgPrompt = "Run-Time Error " _
                & lngErrNum & ": " _
                & AccessError(lngErrNum)
        End If
        If intButtons = 0 Then
        
            'No buttons/icon specified, so use the default.
            intButtons = vbOKOnly + vbInformation
        End If
        If strMsgTitle = vbNullString Then
        
            'No title specified, so use the default.
            strMsgTitle = "Unexpected Run-Time Error"
        End If
        ErrGeneral = MsgBox(strMsgPrompt, _
            intButtons, _
            strMsgTitle)
    End If
    
Exit_Routine:
    On Error Resume Next
    Close intFileNum
    Exit Function
    
Err_Routine:
    lngErrNum = Err.Number
    Select Case Err
        '
    Case Else
        Err.Raise lngErrNum
        Resume Exit_Routine
    End Select
    
End Function

Download this function in plain text format, ready for import into your own Microsoft Access 97 or Microsoft Access 2000 application, here.

home