The ErrData Function

Public Function ErrData( _
    Optional intDataErr As Integer, _
    Optional strModule 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 Access or DAO errors trapped in the Error
    'event of a form. Use the function ErrGeneral to handle
    'VBA run-time errors.
    
    'Arguments
    
    'intDataErr: The error number as returned by the DataErr
    'argument of the Error event.
    
    'strModule: The name of the form module 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 fo 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 (from the Error event of a form)
    
    'intResponse = ErrData ( _
    '   intDataErr:=DataErr, _
    '   strModule:=Me.Name
    ')
    
    '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.

On Error GoTo Err_Routine

    Dim intFileNum As Integer
    Dim lngErrNum As Long
    
    ErrData = 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, "Data Error", intDataErr, _
            AccessError(intDataErr), _
            strModule, "Form_Error", 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 = "Data Error " _
                & intDataErr & ": " _
                & AccessError(intDataErr)
        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 Data Error"
        End If
        ErrData = 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