Help with error trapping function

BukHix

Registered User.
Local time
Today, 14:11
Joined
Feb 21, 2002
Messages
379
I have been very lax in building error trapping into my DB applications and am now working on one that requires it.

Can I put this into a function and call it whenever I have an error anywhere in the form? If so what is the best way to do it? I have a basic idea of how to call functions am looking for "best practices" when it comes to error trapping for an entire form

Code:
If err <> 0 Then
    Dim msgError As String
    
    msgError = "Error # " & Str(err.Number) & " was generated by " _
        & err.Source & vbCrLf & err.Description & vbCrLf & vbCrLf & _
            "An error message has been sent to Buck"
    MsgBox msgError, , "Erro", err.HelpFile, err.HelpContext
    
    DoCmd.SendObject , , , "hicksb@ritsema.com", , , _
        "Database Problem", msgError, False
    Exit Sub
End If

As I have it now it is inside the sub like this:

Code:
Private Sub EmpID_Exit(Cancel As Integer)

Dim fname As String, lname As String, txtFullName As String

On Error Resume Next

lname = DLookup("[LastName]", "EmployeeList", "[EmployeeNumber]=" & Me!EmpID)
fname = DLookup("[FirstName]", "EmployeeList", "[EmployeeNumber]=" & Me!EmpID)

Me.txtEmployeeName = (fname & " " & lname)

If err <> 0 Then
    Dim msgError As String
    
    msgError = "Error # " & Str(err.Number) & " was generated by " _
        & err.Source & vbCrLf & err.Description & vbCrLf & vbCrLf & _
            "An error message has been sent to Buck"
    MsgBox msgError, , "Erro", err.HelpFile, err.HelpContext
    
    DoCmd.SendObject , , , "hicksb@ritsema.com", , , _
        "Database Problem", msgError, False
    Exit Sub
End If

End Sub
 
Wouldn't you just call that function in the subs? Something like

On Error Goto Err_Sub

(Procedure...)

Err_Sub:
DoCmd.OpenFunction ErrorTrap
 
You mean like this?

Code:
Private Sub EmpID_Exit(Cancel As Integer)

Dim fname As String, lname As String, txtFullName As String

On Error GoTo err_Sub

lname = DLookup("[LastName]", "EmployeeList", "[EmployeeNumber]=" & Me!EmpID)
fname = DLookup("[FirstName]", "EmployeeList", "[EmployeeNumber]=" & Me!EmpID)
Me.txtEmployeeName = (fname & " " & lname)

err_Sub
    DoCmd.Open ErrorTrap

End Sub

And then my function like this?
Code:
Public Function ErrorTrap()
If err <> 0 Then
    Dim msgError As String
    
    msgError = "Error # " & Str(err.Number) & " was generated by " _
        & err.Source & vbCrLf & err.Description & vbCrLf & vbCrLf & _
            "An error message has been sent to Buck"
    MsgBox msgError, , "Erro", err.HelpFile, err.HelpContext
    
    DoCmd.SendObject , , , "hicksb@ritsema.com", , , _
        "Database Problem", msgError, False
    Exit Sub
End If

End Function
 
Seems to me that would work. The only issue is if you need to reference "offending controls",i.e. being able to differentiate between user data errors and actual program issues, you would need to pass control names into the function as variables. It looks like you're sending errors via email, so that may not be a bad idea...?

Oh yeah, and the Err_Sub line needs a : at the end

[This message has been edited by jatfill (edited 05-07-2002).]
 
Thanks I also had to remove the Exit Sub in the function. I am now firing the event and sending the email but just as soon as it completes I get this error:

runtime.gif


This happens after the email is sent so I think the problem is cuased by the return to the form field. Any ideas? This bold text is what gets highlighted by the debugger

err_Sub:
DoCmd.Open ErrorTrap



[This message has been edited by BukHix (edited 05-07-2002).]
 
actually, I told you incorrectly the first time, sorry. It should be:
Call ErrorTrap

[This message has been edited by jatfill (edited 05-08-2002).]
 

Users who are viewing this thread

Back
Top Bottom