Results 1 to 15 of 15

Thread: On Error

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    230

    On Error

    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:
    1. On Error GoTo description:
    2.  
    3. description:
    4. WriteFile(Err.Description & Function/Sub Name)

  2. #2
    Addicted Member
    Join Date
    Nov 2006
    Posts
    132

    Re: On Error

    Quote Originally Posted by Vntalk
    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:
    1. On Error GoTo description:
    2.  
    3. description:
    4. 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?

    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.

  3. #3
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: On Error

    You can use something like this (write in Append mode so you don't loose previous entries):
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4. On Error GoTo ErrorHandeler
    5.     Debug.Print 1 / 0
    6.         Exit Sub
    7. ErrorHandeler:
    8.     writeLog Err.number, Err.description, "Form1 Form_Load"
    9. End Sub
    10.  
    11. 'in module
    12. Public Sub writeLog(number As Long, description As String, procedure As String)
    13.     Dim ff As Integer: ff = FreeFile
    14.         Open App.Path & "\error.log" For Append As #ff
    15.             Print #ff, Format(Date, "dd.mm.yy") & ", " & Format(Time, "hh:mm:ss") & " - error #" & number & " - " & description & " - " & "in " & procedure & " procedure"
    16.         Close #ff
    17. End Sub

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: On Error

    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

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    230

    Re: On Error

    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

  6. #6
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: On Error

    Quote Originally Posted by Vntalk
    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.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    230

    Re: On Error

    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

  8. #8

  9. #9
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: On Error

    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:

    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
    Fatal_Error is a routine that does something like this:

    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
    .
    .
    .

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  10. #10
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: On Error

    Quote Originally Posted by szlamany
    ...Once an error occurs and you are in the error trap then no further error trapping will occur...
    That depends on how it is designed. Try the following to see what I mean:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.  
    5. On Error GoTo ErrHandler1
    6.  
    7.     Debug.Print 1 / 0
    8.    
    9.     Exit Sub
    10.    
    11. ErrHandler1:
    12.  
    13.     Test
    14.     Err.Clear
    15.     MsgBox "Hello"
    16.  
    17. ErrHandler2:
    18.  
    19.     MsgBox Err.Description
    20.     Exit Sub
    21.  
    22. End Sub
    23.  
    24. Public Sub Test()
    25. On Local Error GoTo ErrHandler
    26.  
    27.     Debug.Print 1 / 0
    28.    
    29.     Exit Sub
    30.    
    31. ErrHandler:
    32.     MsgBox "Sub: Test; Err: " & Err.Description
    33.     Err.Clear
    34.     Resume Next
    35.  
    36. End Sub

  11. #11
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: On Error

    This is the point I was trying to make:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.  
    5. On Error GoTo ErrHandler1
    6.  
    7.     Debug.Print 1 / 0
    8.    
    9.     Exit Sub
    10.    
    11. ErrHandler1:
    12.  
    13.     [b]MsgBox "In handler1 - lets make an error and see what happens"
    14.    
    15.     Debug.Print 1 / 0[/b]
    16.  
    17.     Test
    18.     Err.Clear
    19.     MsgBox "Hello"
    20.  
    21. ErrHandler2:
    22.  
    23.     MsgBox Err.Description
    24.     Exit Sub
    25.  
    26. End Sub
    27.  
    28. Public Sub Test()
    29. On Local Error GoTo ErrHandler
    30.  
    31.     Debug.Print 1 / 0
    32.    
    33.     Exit Sub
    34.    
    35. ErrHandler:
    36.     MsgBox "Sub: Test; Err: " & Err.Description
    37.     Err.Clear
    38.     Resume Next
    39.  
    40. End Sub
    Last edited by szlamany; Dec 24th, 2006 at 03:06 PM. Reason: Keep rb happy :)

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  12. #12

  13. #13
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: On Error

    Quote Originally Posted by RhinoBull
    @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!

    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

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  14. #14

  15. #15
    Addicted Member
    Join Date
    Nov 2006
    Posts
    132

    Re: On Error

    Quote Originally Posted by Vntalk
    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
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width