Results 1 to 13 of 13

Thread: Form wide error handling?

  1. #1

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Form wide error handling?

    Instead of in each sub adding error handling, is there a way to have a form wide error handling?

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Form wide error handling?

    The easy answer is No, but there may be a GLOBAL error handler, but each module has to RAISE and event and in turn call it. I've never done it, though.
    I use error control for each module that I anticipate may produce an error.

  3. #3

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Form wide error handling?

    hmm..not what i wanted to hear : /...thanks

  4. #4
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Form wide error handling?

    Even though there is an outdated On Local Error... statement which implies there might be a global error handler, there isn't. If you want to put the same error handling, or an error handling outline, in several subs you can use MZTools which will do it at the press of a button for each sub.

  5. #5

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Form wide error handling?

    yeah i know that, but i was hoping i could put it at the top of my form or something under option explicit "On Private Error goto ErrHndler" or something of the sort..That would be a neat feature

  6. #6
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Form wide error handling?

    Here is an example of what we use...

    VB Code:
    1. Private Sub PopulateBL()
    2.         '<EhHeader>
    3.         On Error GoTo PopulateBL_Err
    4.         '</EhHeader>
    5.         Dim rsRecords As New ADODB.Recordset
    6. 100     With rsRecords
    7. 102         .Open "SELECT HBL_BLNumber FROM HBL WHERE HBL_Status <> 3", cnn, adOpenDynamic, adLockOptimistic
    8. 104         Do While Not .EOF
    9. 106             cboBL.AddItem .Fields("HBL_BLNumber")
    10. 108             .MoveNext
    11.             Loop
    12. 110         cboBL.AddItem vbNullString
    13. 112         .Close
    14.         End With
    15. 114     Set rsRecords = Nothing
    16.       '<EhFooter>
    17.       Exit Sub
    18.  
    19. PopulateBL_Err:
    20.         Select Case ErrorGlobalHandler("AISIS.frmHBL.PopulateBL")
    21.         Case vbAbort
    22.              Exit Sub
    23.         Case vbRetry
    24.              Resume
    25.         Case vbIgnore
    26.              Resume Next
    27.         Case Else
    28.              MsgBox Err.Description, vbCritical, "Critical Error Encountered"
    29.              End
    30.         End Select
    31.       '</EhFooter>
    32. End Sub

    VB Code:
    1. Public Function ErrorGlobalHandler(ByVal strErrorSource As String) As Long
    2.     Dim strErrorMessage As String
    3.     strErrorMessage = "Error Occured In:  " & strErrorSource & vbCrLf & _
    4.                       "Error Date & Time: " & Now & vbCrLf & _
    5.                       "Error Description: " & Err.Description & vbCrLf & _
    6.                       "Error Number:      " & Err.Number & vbCrLf & _
    7.                       "Line Number:       " & Erl & vbCrLf
    8.     'Pass to other subroutine to trap any error that might occur in the ErrorGlobalHandler
    9.     ErrorGlobalHandler = PromptAndLogError(strErrorMessage, App.Path)
    10. End Function
    VB Code:
    1. Public Function PromptAndLogError(ByVal ErrorMessage As String, ByVal SystemPath As String, Optional ShowPrompt As Boolean = True) As Long
    2.     On Error Resume Next
    3.     Dim fso         As FileSystemObject
    4.     Dim tsm         As TextStream
    5.     Dim ErrorFile   As String
    6.    
    7.     Set fso = New FileSystemObject
    8.    
    9.     ErrorFile = SystemPath & "\ErrorLog" & Format$(Date, "mmddyyyy") & ".txt"
    10.    
    11.     If fso.FileExists(ErrorFile) = True Then
    12.         Set tsm = fso.OpenTextFile(ErrorFile, ForAppending)
    13.         tsm.Write String$(30, "-") & vbCrLf & ErrorMessage
    14.     Else
    15.         Set tsm = fso.CreateTextFile(ErrorFile, True)
    16.         tsm.Write ErrorMessage
    17.     End If
    18.    
    19.     tsm.Close
    20.     Set tsm = Nothing
    21.     Set fso = Nothing
    22.    
    23.     If ShowPrompt = True Then
    24.         PromptAndLogError = MsgBox(ErrorMessage & vbCrLf & "Please report to the programmer any persistent error.   ", vbCritical + vbAbortRetryIgnore, "Error Encountered")
    25.     End If
    26. End Function
    Last edited by dee-u; Oct 6th, 2005 at 08:42 PM.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  7. #7
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Form wide error handling?

    Dee-u, that's still not the same as a global error handler. You still need to put the actual error handling code in each sub. The fact that you then call a public function that shows a standard error message and logs the error doesn't really make it a global error handler.

  8. #8
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Form wide error handling?

    Yap, I know, but I hoped it might be of some help or sort....
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  9. #9

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Form wide error handling?

    thanks dee-u, but i was looking for a way around that : /

  10. #10
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Form wide error handling?

    that is what i was talking about in #2. I knew it existed.

  11. #11
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

    Re: Form wide error handling?

    If an error is not handled, it is passed back to the calling procedure.
    This means that only the first procedure in the call stack really needs to have an error handler. Basically this is every event handler.
    Frans

  12. #12
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Form wide error handling?

    That's correct Frans, but we don't write our own message pump in VB since we simply rely on VB to do that for us. That means we just write code in our event procedures and they are magically just raised... So we have no control over the call stack either. But letting an error traverse all the way up the call stack is also a bad programming practice, and I bet that if VB would have had some kind of Form wide error handling that Remix asked for there would be hundreds of books and articles that would warn us from using that bad bad practice.

    Errors should be delt with where the error is raised or by the immediate caller.

  13. #13
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

    Re: Form wide error handling?

    Yes, I agree.
    I prefer to handle errors at the place where they occur.
    If I need to pass it to the calling procedure, I raise another error.

    But I prefer to code in a way, that most situations that can cause an error are checked before the error occurs.

    eg.

    VB Code:
    1. If c <> 0 Then
    2.     a = b/c
    3. Else
    4.     MsgBox "Moron, you can't divide by zero"
    5. End If
    Frans

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