Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I trap errors and write them to a log file?

Error Trapping

How can I trap errors and write them to a log file?

by  Michael42  Posted    (Edited  )
Global Variable:
mNL$ = Chr$(13) & Chr$(10)


Sub mySub1()
On Error Goto err_mySub1
'Your Code for current sub or function

Exit Sub


err_mySub1:
Screen.MousePointer = DEFAULT
sLogError Error, Err

Select Case Err
Case 3021 'No Current Record
Resume Next
Case Else
mRetVal% = MsgBox("SubName999" & mNL & mNL & Error & mNL$ & mNL$ & "Error number: " & Str$(Err), MB_ICONINFORMATION, "Internal Error")
Resume Next
End Select
Return


Sub sLogError (pErrMsg As String, pErrNumber As Long)
On Error Resume Next

'Write Error To Error Log
Dim mLogFile As Integer
mLogFile% = FreeFile
Open app.Path & "\Error.log" For Append As #mLogFile%
Print #mLogFile%, "<<<<<<<<<< " & Date$ & " - " & Time$ & " >>>>>>>>>>" & mNL
Print #mLogFile%, pErrMsg$ & mNL$ & mNL$ & "Error number: " & Str$(pErrNumber)
Close #mLogFile%

End Sub
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top