This is an example of how to use the intrinsic Visual basic App object to log errors to a file, which may later be helpful in debugging an application. The log file has the error number, error description, form name, sub or function name, and a date-time stamp when the error occurred. This also limits the error so that multiple loggings of the same error are not recorded in iterations.
Inputs
The public sub in the moderror.bas takes 4 parameters
LogErrors err.number, err.description, me.name , "MySubNameHere"
Assumes
The app.StartLogging will not work in the development enviroment. The code must be in a compiled executable for it to work.
Returns
This writes a .log file which may be opened with any text editor for examination as to the general area of an error that could crash your program.
API Declarations'* this code goes in a module for public use
'* Timothy A. Vanover
'* [email protected]
Public Sub LogErrors(lngErrNumber As Long, strErrDescription As String, _
strErrModule As String, strErrProcedure As String)
'*use to prevent recording multiple errors in loop
Static LastErrorRecorded As String
If CStr(Err.Number) & Err.Description & strErrModule & strErrProcedure = LastErrorRecorded Then
Exit Sub
Else
App.StartLogging App.Path & "\" & App.EXEName & ".log", vbLogToFile
App.LogEvent vbCrLf & _
"Error Description: " & strErrDescription & vbCrLf & _
"Error Number: " & lngErrNumber & vbCrLf & _
"Module Name: " & strErrModule & vbCrLf & _
"Procedure Name: " & strErrProcedure & vbCrLf & _
"Version Number: " & App.Major & "." & App.Minor & "." & App.Revision & vbCrLf & _
"Date: " & Format$(Now, "Short Date") & vbCrLf & _
"Time: " & Format$(Now, "Long Time") & _
vbCrLf & vbCrLf
LastErrorRecorded = CStr(Err.Number) & Err.Description & strErrModule & strErrProcedure
End If
End Sub