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 strongm 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 file?

Status
Not open for further replies.

cavery

Technical User
Oct 29, 2002
129
US
HI,
I'm using Access 97 and I want to trap errors (any standard error message) and write those error messages to a file (.ext or notepad in a specific shared drive). This would help me track and record the number of errors that occur in our business enviornment. In addition, read errors that are ocurring from other users.

I have code that was given to me over a year ago, but I am struggling to understand how to decipher and use. I will paste it below. Feel free to provide original and more easier code to use!



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%, &quot;<<<<<<<<<< &quot; & Date$ & &quot; - &quot; & Time$ & &quot; >>>>>>>>>>&quot; & mNL
Print #mLogFile%, pErrMsg$ & mNL$ & mNL$ & &quot;Error number: &quot; & Str$(pErrNumber)
Close #mLogFile%

End Sub



Thanks,
Clark

~Clark
 
Clark,

You can take a look at the code that's in my error trapping routine, on my website.

Here's the basics (from some other code):
Dim strFile As String

strFile = FreeFile
Open &quot;W:\DataSetBuildLog.txt&quot; For Append Lock Write As #strFile
Write #strFile, Now, &quot;Starting the Build Process.&quot;
Close #strFile

FreeFile returns a string of the next available file address. The quotes around the text are not needed--they actually show up in the text file.

I'd play with something like this for a little bit, away from your error trapping, so that you can get a feel for how it works. Then try to get your error trapping going.

And, of course, come on back with any other questions you have.

Jeremy

PS: Clark, it's always nice to see the same faces a year down the road, and see that they have moved on in their knowledge and what they're tackling. Cheers to you for really taking this on.

==
Jeremy Wallace
AlphaBet City Dataworks
Access Databases for Non-Profit Organizations

Please post in the appropriate forum with a descriptive subject; code and SQL, if referenced; and expected results. See thread181-473997 for more pointers.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top