PDA

View Full Version : Help with error trapping function


BukHix
05-07-2002, 05:05 PM
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

<pre id=code><font face=courier size=2 id=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
</font id=code></pre id=code>

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

<pre id=code><font face=courier size=2 id=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
</font id=code></pre id=code>

He who joyfully marches in rank and file has already earned my contempt. He has been given a large brain by mistake, since for him the spinal cord would suffice. - Albert Einstein & BukHix Liberty1st.org ( http://www.liberty1st.org/forum/ )

smozgur
05-07-2002, 07:45 PM
Here is what you need BukHix. Creating a public error sub routine and calling where you need by catching error by On Error Goto <ErrorHandlerLabel>.

Private Sub EmpID_Exit(Cancel As Integer)
Dim fname As String, lname As String, txtFullName As String
Below code makes process jumped to ErrorHandler label at the bottom
On Error Goto ErrorHandler
lname = DLookup( [LastName] , EmployeeList , [EmployeeNumber]= & Me!EmpID)
fname = DLookup( [FirstName] , EmployeeList , [EmployeeNumber]= & Me!EmpID)
Me.txtEmployeeName = (fname & & lname)
....
....OTHER CODES HERE
....

If there is an error then it mean codes will be broken anywhere above this line. But if it run here then we should exit from sub here because next code line will call the MyError sub
Exit Sub

ErrorHandler:
Call MyError(err)
End Sub

If you put the MyError sub into a module then you can use it anywhere you need
Public Sub MyError(genError as ErrObject)
Dim msgError As String
msgError = Error # & Str(genError.Number) & was generated by & genError.Source & vbCrLf & genError.Description & vbCrLf & vbCrLf & An error message has been sent to Buck
MsgBox msgError, , Erro , genError.HelpFile, genError.HelpContext
DoCmd.SendObject , , , hicksb@ritsema.com , , , Database Problem , msgError, False
End Sub

Hope it works for you.
Regards

Oz

BukHix
05-10-2002, 02:56 PM
Thanks Oz for the help. I haven t had the chance to try it yet but I wanted to make sure that I thanked you so that you knew it was appreciated. I just finished another portion of the DB that I went on to until I found a solution for this but I am back on this. I will post back if I have more problems.

Thanks


He who joyfully marches in rank and file has already earned my contempt. He has been given a large brain by mistake, since for him the spinal cord would suffice. - Albert Einstein & BukHix Liberty1st.org ( http://www.liberty1st.org/forum/ )