|
-
Jun 16th, 2005, 01:07 PM
#1
Thread Starter
Admodistrator
Form wide error handling?
Instead of in each sub adding error handling, is there a way to have a form wide error handling?
-
Jun 16th, 2005, 01:23 PM
#2
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.
-
Jun 16th, 2005, 04:16 PM
#3
Thread Starter
Admodistrator
Re: Form wide error handling?
hmm..not what i wanted to hear : /...thanks
-
Jun 16th, 2005, 05:08 PM
#4
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.
-
Jun 16th, 2005, 11:00 PM
#5
Thread Starter
Admodistrator
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
-
Jun 16th, 2005, 11:03 PM
#6
Re: Form wide error handling?
Here is an example of what we use...
VB Code:
Private Sub PopulateBL()
'<EhHeader>
On Error GoTo PopulateBL_Err
'</EhHeader>
Dim rsRecords As New ADODB.Recordset
100 With rsRecords
102 .Open "SELECT HBL_BLNumber FROM HBL WHERE HBL_Status <> 3", cnn, adOpenDynamic, adLockOptimistic
104 Do While Not .EOF
106 cboBL.AddItem .Fields("HBL_BLNumber")
108 .MoveNext
Loop
110 cboBL.AddItem vbNullString
112 .Close
End With
114 Set rsRecords = Nothing
'<EhFooter>
Exit Sub
PopulateBL_Err:
Select Case ErrorGlobalHandler("AISIS.frmHBL.PopulateBL")
Case vbAbort
Exit Sub
Case vbRetry
Resume
Case vbIgnore
Resume Next
Case Else
MsgBox Err.Description, vbCritical, "Critical Error Encountered"
End
End Select
'</EhFooter>
End Sub
VB Code:
Public Function ErrorGlobalHandler(ByVal strErrorSource As String) As Long
Dim strErrorMessage As String
strErrorMessage = "Error Occured In: " & strErrorSource & vbCrLf & _
"Error Date & Time: " & Now & vbCrLf & _
"Error Description: " & Err.Description & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Line Number: " & Erl & vbCrLf
'Pass to other subroutine to trap any error that might occur in the ErrorGlobalHandler
ErrorGlobalHandler = PromptAndLogError(strErrorMessage, App.Path)
End Function
VB Code:
Public Function PromptAndLogError(ByVal ErrorMessage As String, ByVal SystemPath As String, Optional ShowPrompt As Boolean = True) As Long
On Error Resume Next
Dim fso As FileSystemObject
Dim tsm As TextStream
Dim ErrorFile As String
Set fso = New FileSystemObject
ErrorFile = SystemPath & "\ErrorLog" & Format$(Date, "mmddyyyy") & ".txt"
If fso.FileExists(ErrorFile) = True Then
Set tsm = fso.OpenTextFile(ErrorFile, ForAppending)
tsm.Write String$(30, "-") & vbCrLf & ErrorMessage
Else
Set tsm = fso.CreateTextFile(ErrorFile, True)
tsm.Write ErrorMessage
End If
tsm.Close
Set tsm = Nothing
Set fso = Nothing
If ShowPrompt = True Then
PromptAndLogError = MsgBox(ErrorMessage & vbCrLf & "Please report to the programmer any persistent error. ", vbCritical + vbAbortRetryIgnore, "Error Encountered")
End If
End Function
Last edited by dee-u; Oct 6th, 2005 at 08:42 PM.
-
Jun 16th, 2005, 11:50 PM
#7
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.
-
Jun 16th, 2005, 11:57 PM
#8
Re: Form wide error handling?
Yap, I know, but I hoped it might be of some help or sort....
-
Jun 17th, 2005, 12:39 AM
#9
Thread Starter
Admodistrator
Re: Form wide error handling?
thanks dee-u, but i was looking for a way around that : /
-
Jun 17th, 2005, 01:04 AM
#10
Re: Form wide error handling?
that is what i was talking about in #2. I knew it existed.
-
Jun 17th, 2005, 02:56 AM
#11
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.
-
Jun 17th, 2005, 03:48 AM
#12
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.
-
Jun 17th, 2005, 07:07 AM
#13
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:
If c <> 0 Then
a = b/c
Else
MsgBox "Moron, you can't divide by zero"
End If
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|