Is There AnyWay To Catch An Error On A Form Or A Module, And Write To A Text File Each Time It Occur
Like This:
VB Code:
On Error GoTo description: description: WriteFile(Err.Description & Function/Sub Name)
Printable View
Is There AnyWay To Catch An Error On A Form Or A Module, And Write To A Text File Each Time It Occur
Like This:
VB Code:
On Error GoTo description: description: WriteFile(Err.Description & Function/Sub Name)
Yes you can do this. This is a commonly used tactic. What part of this do you have questions about?Quote:
Originally Posted by Vntalk
An important concern is what to do with the error besides logging it. Once the error is logged you have to deal with the cause of the error. Also, you have to be very careful to preserve the information about the error so it is not lost while you are logging. Another thing is to make sure you are handling errors in your logging code, while at the same time not interferring with the operation of your applications main line of logic.
You can use something like this (write in Append mode so you don't loose previous entries):VB Code:
Option Explicit Private Sub Form_Load() On Error GoTo ErrorHandeler Debug.Print 1 / 0 Exit Sub ErrorHandeler: writeLog Err.number, Err.description, "Form1 Form_Load" End Sub 'in module Public Sub writeLog(number As Long, description As String, procedure As String) Dim ff As Integer: ff = FreeFile Open App.Path & "\error.log" For Append As #ff Print #ff, Format(Date, "dd.mm.yy") & ", " & Format(Time, "hh:mm:ss") & " - error #" & number & " - " & description & " - " & "in " & procedure & " procedure" Close #ff End Sub
You must remember the most important point of an error trap.
Once an error occurs and you are in the error trap then no further error trapping will occur.
That means you don't write files and open files and do anything that can even remotely error out.
Proper method is:
Code:On Error Goto Error_Routine
.
.
.
Exit Sub (or Exit Function)
DealWithError:
Write file - or whatever
Exit Sub
OtherDealWithError:
Do something else...
Exit Sub
Error_Routine:
If Err=.... Then Resume DealWithError
Resume OtherDealWithError
End Sub
sorry if i'm not clear enought, but i want to know if we can catch a global error, not to trap an error on each sub or function, since there are many sub/function in a module or a form.
Global error trap to know which sub/function an error occur
Not sure but i don't believe it's possible.Quote:
Originally Posted by Vntalk
Also, i want to know everytime an error occur, my app just close without any error warning. A week a go , when an error occur it does pop up an error message
Can you post your error trapping method (if there's any)?
An error that occurs in a sub or func that is not trapped is simply passed along to the calling routine. If that calling routine has no error trap then it goes up the ladder to the next routine.
You should have an error trap in each and every routine - that's proper programming practice - actually have a standard template for each and every routine.
Here's what ours looks like:
Fatal_Error is a routine that does something like this:Code:'=====================================
'Module :basMain.bas
'Application :AMC
'Author :Author Name
'Creation Date :November 18, 2002
'=====================================
'Company Name...
'Address...
'City, State, Zip...
'=====================================
Public Sub RemoveTab(f As Form)
'Remove the Tab for a form when the Form closes
Dim i As Long, j As Long, k As Long, x As Long, y As Long, z As Long
Dim s1 As String, s2 As String, s3 As String, s4 As String, s5 As String
Debug.Print " RemoveTab"
On Error GoTo Error_Handler
Begin:
With gfrmPrimary.tabForms
For x = 1 To .Tabs.Count
If CInt(.Tabs(x).Tag) = f.mintTabNo Then
.Tabs.Remove (x)
Exit For
End If
Next x
End With
Rtn_Caller:
Exit Sub
Error_Handler:
Call Fatal_Error(Err.Number, Err.Source, Err.Description, "RemoveTab")
Resume Rtn_Caller
End Sub
Code:Public Function Fatal_Error(ByVal errNum As Double _
, ByVal errSource As String _
, Optional ByVal ErrDesc As String _
, Optional ByVal ErrRoutine As String _
, Optional ByVal booNoEnd As Boolean)
Dim i As Long, j As Long, k As Long, x As Long, y As Long, z As Long
Dim s1 As String, s2 As String, s3 As String, s4 As String, s5 As String
Debug.Print "***Fatal_Error"
On Error GoTo Error_Handler
Begin:
s1 = ""
If Not booNoEnd Then s1 = " The AMC program will now close."
Select Case errNum
Case 52
gModal = True
MsgBox "Bad file name or number." & s1, vbCritical, "Bad File Name!"
gModal = False
Case 53
gModal = True
MsgBox "Can not find the INI file. Please make sure the INI file is at the same path as the executable and named correctly! The AMC program will now close", vbCritical, "INI File Not Found!"
gModal = False
.
.
.
That depends on how it is designed. Try the following to see what I mean:Quote:
Originally Posted by szlamany
VB Code:
Option Explicit Private Sub Command1_Click() On Error GoTo ErrHandler1 Debug.Print 1 / 0 Exit Sub ErrHandler1: Test Err.Clear MsgBox "Hello" ErrHandler2: MsgBox Err.Description Exit Sub End Sub Public Sub Test() On Local Error GoTo ErrHandler Debug.Print 1 / 0 Exit Sub ErrHandler: MsgBox "Sub: Test; Err: " & Err.Description Err.Clear Resume Next End Sub
This is the point I was trying to make:
VB Code:
Option Explicit Private Sub Command1_Click() On Error GoTo ErrHandler1 Debug.Print 1 / 0 Exit Sub ErrHandler1: [b]MsgBox "In handler1 - lets make an error and see what happens" Debug.Print 1 / 0[/b] Test Err.Clear MsgBox "Hello" ErrHandler2: MsgBox Err.Description Exit Sub End Sub Public Sub Test() On Local Error GoTo ErrHandler Debug.Print 1 / 0 Exit Sub ErrHandler: MsgBox "Sub: Test; Err: " & Err.Description Err.Clear Resume Next End Sub
@szlamany: do you have problem with VBCODE tags - you're using CODE (reason why?).
It's a silly habit from the DB section for the forum. If I put T-SQL in VBCODE tags sometimes the posters I'm working with think it's VB syntax. If they had SQLCODE tags I would be really happy!Quote:
Originally Posted by RhinoBull
At any rate, since I hand type the [code] and [/code] tags it's just a typing habit.
I'll try to keep on top of it ;)
Still VBCODE tags are better - they don't create annoying scrollable textbox.
You cannot trap a "global" error - errors that are not trapped inside a sub, function, or property get or set are raised to the calling method.Quote:
Originally Posted by Vntalk