Where to capture error?

L

Lars Brownies

In the err_handler of my procedures I use:

Call ErrLog("frmMyForm", "Form_Current", "Error " & Err.Number & ": " &
Err.Description)

I noticed that I can also capture the particular Err.Number and
Err.Description in the ErrLog procedure, which would make the ErrLog call
shorter:

Call ErrLog("frmMyForm", "Form_Current")

Is it a good idea to implement it like this or can I run into trouble?

Thanks,

Lars
 
A

Arvin Meyer [MVP]

Since I wrote the ErrorLog routine,

http://www.datastrat.com/Code/Error.txt

I think I can answer that. The only place you may run into trouble is in
Vista or possibly Windows 7 (since I haven't tested it there yet) if you are
denied permissions to write to or create a file in the root of the C: drive.
In that case, simple change the path in the code to write somewhere where
the user does have permissions.
 
L

Lars Brownies

Thanks Arvin. Didn't know this code originated from you.

In my developers mdb I have made a link to the text file so that I can open
it as a table. For that to work I had to change the comma separators in your
code to |, as some error messages have comma's in them.

Lars
 
L

Lars Brownies

Strange thing, it seems that the err.number and err.description of the
function where the error occured, aren't available anymore in the errlog
function. I contstantly get the number 0 and description is empty. Maybe I
did something wrong with testing or should it work?

Thanks,

Lars
 
D

Dirk Goldgar

Lars Brownies said:
Strange thing, it seems that the err.number and err.description of the
function where the error occured, aren't available anymore in the errlog
function. I contstantly get the number 0 and description is empty. Maybe I
did something wrong with testing or should it work?


It should work, provided you don;t clear the error between the time it is
raised and the time it is displayed or printed. Post the exact code of your
ErrLog function.
 
L

Lars Brownies

In a module I have the following function:

Public Function ErrLog(strObj As String, strRoutine As String)
On Error GoTo Err_Handler

Dim strErr as String

strErr = "Error " & Err.Number & ": " & Err.Description

MsgBox strErr & cLine & "(Object: " & strObj & ". Procedure: " & strRoutine
& ".)", vbExclamation, cAppName
strErr = Replace(strErr, Chr(10), ", ")

Open strPathApp & cAppAbbr & "-FE\Log.txt" For Append As #1
Print #1, Format(Now, "dd/mm/yyyy hh:nn:ss") & "|Object: " & strObj &
"|Routine: " & strRoutine & "|" & strDepartment & "|" & strUser & "|" &
strErr
Close #1

Exit_Func: Exit Function
Err_Handler:
MsgBox Err.Description & " in ErrLog." & " " & strObj & " " &
strRoutine, vbExclamation, cAppName
Resume Exit_Func
End Function

Thanks,

Lars
 
D

Dirk Goldgar

Lars Brownies said:
In a module I have the following function:

Public Function ErrLog(strObj As String, strRoutine As String)
On Error GoTo Err_Handler


Bang! There's your smoking gun. Executing an "On Error" statement
automatically clears the Err object. Change the procedure to capture the
current error values *before* executing that statement:

'------ start of revised code ------
Public Function ErrLog(strObj As String, strRoutine As String)

Dim strErr as String
Dim intFileNo As Integer

strErr = "Error " & Err.Number & ": " & Err.Description

On Error GoTo Err_Handler

MsgBox _
strErr & cLine & "(Object: " & strObj & _
". Procedure: " & strRoutine & ".)", _
vbExclamation, _
cAppName

strErr = Replace(strErr, Chr(10), ", ")

intFileNo = FreeFile()
Open strPathApp & cAppAbbr & "-FE\Log.txt" For Append As #intFileNo

Print #intFileNo, _
Format(Now, "dd/mm/yyyy hh:nn:ss") & _
"|Object: " & strObj & _
"|Routine: " & strRoutine & "|" & strDepartment & _
"|" & strUser & "|" & strErr

Close intFileNo

Exit_Func:
Exit Function

Err_Handler:
MsgBox _
Err.Description & " in ErrLog." & " " & strObj & " " & strRoutine, _
vbExclamation, _
cAppName
Resume Exit_Func

End Function
'------ end of revised code ------

Note: I also removed your hard-coding of 1 for the file number, and used
FreeFile() to get the first free number. Hard-coding the file number is a
bad practice, because there could be circumstances in which the number 1 may
already be in use.
 
L

Lars Brownies

Wow! Thanks a lot Dirk for the lesson.

Lars

Dirk Goldgar said:
Bang! There's your smoking gun. Executing an "On Error" statement
automatically clears the Err object. Change the procedure to capture the
current error values *before* executing that statement:

'------ start of revised code ------
Public Function ErrLog(strObj As String, strRoutine As String)

Dim strErr as String
Dim intFileNo As Integer

strErr = "Error " & Err.Number & ": " & Err.Description

On Error GoTo Err_Handler

MsgBox _
strErr & cLine & "(Object: " & strObj & _
". Procedure: " & strRoutine & ".)", _
vbExclamation, _
cAppName

strErr = Replace(strErr, Chr(10), ", ")

intFileNo = FreeFile()
Open strPathApp & cAppAbbr & "-FE\Log.txt" For Append As #intFileNo

Print #intFileNo, _
Format(Now, "dd/mm/yyyy hh:nn:ss") & _
"|Object: " & strObj & _
"|Routine: " & strRoutine & "|" & strDepartment & _
"|" & strUser & "|" & strErr

Close intFileNo

Exit_Func:
Exit Function

Err_Handler:
MsgBox _
Err.Description & " in ErrLog." & " " & strObj & " " & strRoutine,
_
vbExclamation, _
cAppName
Resume Exit_Func

End Function
'------ end of revised code ------

Note: I also removed your hard-coding of 1 for the file number, and used
FreeFile() to get the first free number. Hard-coding the file number is a
bad practice, because there could be circumstances in which the number 1
may already be in use.

--
Dirk Goldgar, MS Access MVP
Access tips: www.datagnostics.com/tips.html

(please reply to the newsgroup)
 
Top