Writing Code???

BillBee

Registered User.
Local time
Today, 12:59
Joined
Aug 31, 2008
Messages
137
I understand that both the below examples do exact;ly the same thing. Is it always safe to do what is described first or should the second example be what is required? Made be there is an even better way. Thanks in advance.


Private Sub Return_to_Exhibitor_Form_Btn_Click()
DoCmd.Close acForm, "frmPrintReports"
Exit Sub


Private Sub Return_to_Exhibitor_Form_Btn_Click()
On Error GoTo Err_Return_to_Exhibitor_Form_Btn_Click
DoCmd.Close acForm, "frmPrintReports"
Exit_Return_to_Exhibitor_Form_Btn_Click:
Exit Sub
Err_Return_to_Exhibitor_Form_Btn_Click:
MsgBox Err.Description
Resume Exit_Return_to_Exhibitor_Form_Btn_Click
 
If the form is open then there should not be any errors when a command is issued to close it. If the button is actually on that form then there can be no errors so the error code is completely redundant.

One of the prevalent myths about VBA is that every procedure should have an error handler. This is a good example of why it is often superfluous.

In any case, where a procedure has no handler the error is passed to the calling procedure whose error handler will be triggered. Otherwise it continues to be passed upwards until it finds one or bombs out. Consequently there definitely should be an error handler in every chain of procedures. Otherwise the app can error out with an unhandled exception.

More levels of error handling can report the location of any problem to the user more precisely but that needs to be traded off against the clutter of error handlers and the need for the developers to turn multiple handlers off to find the exact line where the error occurs when debugging.
 

Users who are viewing this thread

Back
Top Bottom