error handling - enough to put in on error event

giovi2002

Registered User.
Local time
Today, 12:01
Joined
Apr 12, 2005
Messages
83
Every form has an on error property.

Is it enough for error handling to code the on error property for each form?
With enough I mean error handling which lets you resume the program.

Ontherwise I have to code (or call a procedure) for each coded event which i wouldn't prefer

For instance now I'm putting error handling in each event but would consider it more efficient if it can be placed once in each form
Private Sub cmdReport_Click()
On Error GoTo Err_cmdReport_Click

Dim stDocName As String

stDocName = "rptOfme"
DoCmd.OpenReport stDocName, acPreview

Exit_cmdReport_Click:
Exit Sub

Err_cmdReport_Click:
MsgBox Err.Description
Resume Exit_cmdReport_Click

End Sub
 
The only way to properly handle errors is to have an error handler in every procedure. On occasion you may want to ignore certain errors in a procedure and you could do so (trap it) if the error handler is in that procedure but would not have a clue if you were using the Form OnError event.
Here's a free add-in that can assist in writing those procedures. And here's an example of a more generalized error handler.
 
Can anyone give me a pointer on how to catch Access or JET errors?

I already have a generic error handling that creates an automatic log whenever there is a VBA error, but it will not run if there is a RI error or JET error.

I played with form's On_Error event but had limited success in catching some errors. Since Access does not display Err Number for their message (e.g. "You cannot save a record because a related record is required in X") and this doesn't get logged into my error table at all because it is not a VBA error.

So, I'm having challenge catching those type of errors. Hopefully someone will give me pointer in how to catch those error?

If this is any help, here's the module holding my error handlings; all events refer this modules, which is helpful for me to pinpoint the error to a specific event and resolving it.

Code:
'The code was provided by www.access-programmers.co.uk/forums Code Repository
'Added an Error Log functionality, idea suggested by the same website.

Public Sub ShowError(strModule As String, strProcedure As String, _
lngErrorNumber As Long, strErrorDescription As String)

'* Purpose: To create a central error handling mechanism.
'* Accepts: strModule - name of the module, where the error was from (form, class, ect)
'* strprocedure - the name of the procedure where the error occured
'* lngErrorNumber - the numeric error identifier
'* strErrorDescription - the text description of the error

On Error GoTo PROC_ERR
Dim strMessage As String
Dim strCaption As String

'* Build the error message
strMessage = "Error: " & strErrorDescription & vbCrLf & vbCrLf & _
"Module: " & strModule & vbCrLf & _
"Procedure: " & strProcedure & vbCrLf & vbCrLf & _
"Please notify Administrator about this issue. " & _
"Please provide the information shown in this dialog box" & vbCrLf & _
"and explain what you were doing when this error occured."

'* Build the caption for the msgbox
strCaption = "Unexpected Error!"
MsgBox strMessage, vbCritical, strCaption

'Creates a log of the error
Dim i As Variant
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim qd As DAO.QueryDef
    
Set dbs = CurrentDb
    
Set rst = dbs.OpenRecordset("tblErrorLog")

rst.AddNew
rst!ErrModule = strModule
rst!ErrProcedure = strProcedure
rst!ErrNumber = lngErrorNumber
rst!ErrDescription = strErrorDescription
rst.Update
rst.Close

Set rst = Nothing
Set qd = Nothing

PROC_EXIT:
Exit Sub
PROC_ERR:
GoTo PROC_EXIT
End Sub
 
Banana,
Temporarily put this code as the first part of your Error Handling code in each procedure and you will see that RI and Jet *do* assign numbers to their errors!
Code:
   MsgBox "Error      : " & Err.Number & vbCrLf & _
          "Description: " & Err.Description
 

Users who are viewing this thread

Back
Top Bottom