
ARCHIVE: TIPS & TRICKS
Log errors to a file
Marlex Ventura 08.22.2000
Rating: -4.56- (out of 5)




Log errors to a file
Reader, Marlex L. Ventura of Manila, Philippines, submitted this week's
tip. The following code encapsulates a Generic Error Handling routine
that logs errors to a file. In most cases, developers would like to
handle errors within their code. However a great deal of time is
required to really eliminate all these possible sources of errors.
Without an error handler routine, chances are your application may
freeze or simply bomb out on unexpected situations. Much worse, users
are not able to report back properly the conditions of the error and
for convenience the developer would need to trace these errors to
specific procedures or forms for a quick fix.
These lines of code are composed of 2 parts:
1. Error Handler used in procedure bodies.
2. Error Logger routine to log errors to an 'Error.log' file found in
the same directory as your application.
The Error Handler can simply be copied and pasted on all of your
existing procedures modifying the error messages, procedure names and
other parameters as required.
Logically the Error Logger routine must be placed in a module available
to all procedures and functions that would need to call the said
routine (eg. modErrors.bas).
Next time your users encounter an error, simply ask them to e-mail the
error log file and you will know where to start debugging your
programs.
Private Sub Form_Load()
Dim strSomeCommand As String
Dim rs As New ADODB.Recordset
On Error GoTo errorproc
'A query string is used as a command string
strSomeCommand = "SELECT * FROM Employees WHERE EmpNo = '00001'"
Set rs = MyConn.Execute(strSomeCommand)
If Not rs.EOF Then
txtEmpName = rs!EmpName
txtAddr = rs!Addr
End If
rs.Close
Set rs = Nothing
Exit Sub 'exit point of a successful operation
'Error Handler
errorproc:
Select Case Err.Number
'handle known errors here
'Case 999
'Add procedure to fix error here then
'Resume
Case Else 'Catch All for other errors
'Report error based on the general purpose of the procedure
Select Case MsgBox("Error loading employee information. " & _
Trim(Err.Description), vbExclamation + vbRetryCancel +
vbDefaultButton1, "Load Employee Error")
Case vbRetry
Resume
Case vbCancel
'procedure that logs the error
Call LogError(Now, "Form_Load", Err, Me, strSomeCommand)
End Select
End Select
End Sub
'Error Logger
Public Sub LogError(ByVal ErrDateTime As Date, ByVal ProcName As
String, ByRef ErrObj As ErrObject, Optional FormObject As Object,
Optional CommandString As String)
Dim intFileO As Integer
On Error Resume Next
intFileO = FreeFile(0)
Open App.Path & "Error.log" For Append As intFileO
If IsMissing(FormObject) And IsMissing(CommandString) Then
Write #intFileO, ErrDateTime, ProcName, ErrObj.Description,
ErrObj.Source
ElseIf Not IsMissing(FormObject) And Not IsMissing(CommandString) Then
Write #intFileO, ErrDateTime, ProcName, ErrObj.Description,
ErrObj.Source, FormObject.Caption, CommandString
'To enhance this procedure you could add a routine to dump all values
found in a form such as:
'Call DumpFormValues(intFileO, FormObject)
ElseIf Not IsMissing(CommandString) Then
Write #intFileO, ErrDateTime, ProcName, ErrObj.Description,
ErrObj.Source, CommandString
End If
Close intFileO
End Sub
Marlex, thanks for the great tip. To show our appreciation, we're
sending you a free searchVB.com denim shirt. Keep those tips coming in,
folks!
-----------------------------------------
Marlex L. Ventura
ventura@usinc.net
Marlex currently works as the Director for Research & Development of
Inobbar Technologies Corporation (http://www.inobbar.com), an
Information and Communication Technology Consultancy Services company
based in the Philippines.
His work in Research & Development has required Marlex to be constantly
in tune with the different ways IT is being used in the industry, and
constantly thinking of new ways of how IT can be leveraged to improve
everyone's way of life.
 |

|
|
 |
|
 |