Save an Error log File

By | 2019-10-01

The following code shows how one can record errors to a Errors log file. Create an Errors.log file first and place that file in your apps path. Place the error handling in whatever procedures you want. Place the errLogger procedure in a module as a public procedure or in a form as a private procedure.

Module

Public Sub errLogger(ByVal lNum As Long, ByVal sDesc As String, ByVal sFrom As String)
    Dim FileNum As Integer

    FileNum = FreeFile
    Open App.Path & "\Errors.log" For Append As FileNum
    Write #FileNum, lNum, sDesc, sFrom, Now()
    Close FileNum
End Sub

Usage

Private Sub butGenErr_Click()
    On Error GoTo Err_handler

    Exit Sub
Err_handler:
    If Err.Number <> 0 Then
        errLogger Err.Number, Err.Description, "butGenErr_Click"
        Err.Clear
        Resume Next
    End If
End Sub

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.