andrewpiconnect
asked on
Access 2010 vba error handling
Hi,
Just a quick question regarding error handling in vba.
Should you implement error handling for each and every vba procedure? For example:
If opening a form to a specific record:
========================== ========== ========== =========
If Not Nz(Me.txtSubsIFAID, "") = "" Then
Dim strLinkCriteria As String
strLinkCriteria = "[SubsIFA_ID]=" & Me.txtSubsIFAID & " And [Prop_ID]=" & Me.txtPropID & ""
DoCmd.OpenForm "frmAddSubsIFA", acNormal, , strLinkCriteria
End If
========================== ========== ========== =========
or:
DoCmd.OpenForm "frmPolicyPII_AddPolicyNot e", acNormal, , acFormAdd
or:
If Me.Dirty Then
If MsgBox("You have made changes to the 'Client Data' and not saved them." & vbCrLf & vbCrLf & "Are you sure you wish to Exit without saving your changes?", vbYesNo + vbExclamation, "Client Details: Close Screen Alert") = vbYes Then
Me.Undo
DoCmd.Close acForm, "frmClientDetails"
Else
Exit Sub ' stay on form do nothing
End If
Else
Forms![frmNavMenuForm].[Na vMenuSubfo rm].Source Object = ""
End If
Many thanks
Just a quick question regarding error handling in vba.
Should you implement error handling for each and every vba procedure? For example:
If opening a form to a specific record:
==========================
If Not Nz(Me.txtSubsIFAID, "") = "" Then
Dim strLinkCriteria As String
strLinkCriteria = "[SubsIFA_ID]=" & Me.txtSubsIFAID & " And [Prop_ID]=" & Me.txtPropID & ""
DoCmd.OpenForm "frmAddSubsIFA", acNormal, , strLinkCriteria
End If
==========================
or:
DoCmd.OpenForm "frmPolicyPII_AddPolicyNot
or:
If Me.Dirty Then
If MsgBox("You have made changes to the 'Client Data' and not saved them." & vbCrLf & vbCrLf & "Are you sure you wish to Exit without saving your changes?", vbYesNo + vbExclamation, "Client Details: Close Screen Alert") = vbYes Then
Me.Undo
DoCmd.Close acForm, "frmClientDetails"
Else
Exit Sub ' stay on form do nothing
End If
Else
Forms![frmNavMenuForm].[Na
End If
Many thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hey Jack ... welcome to Experts Exchange ...
Joe
Joe
What is sure is that every code that runs absolutely should have an error handler somewhere in the call stack, thus preventing any unhandled errors from ever being raised.
If an error is raised and is not handled in the current procedure, it will look to the procedure that called it, and use the error handler there. If there's none, it will go the procedure before that one, so on and so forth, until it finds a handler or runs to the bottom of the call stack. Under no circumstances should it hit the bottom of the call stack without being handled.
Error propagation (as the above is called) can be a helpful tool when understood and used correctly. As a general rule of thumb though, you're probably better off including basic error handling on every procedure unless you have some specific reason NOT to.
Tools like mz-tools can help greatly with that, in case you're not already aware (google the word, you'll find it).
Cheers,