On Error Goto

  • Thread starter Thread starter Newbie
  • Start date Start date
N

Newbie

Can somebody tell me why the On Error Goto statement has
to be like this?

Exit_Form_Open:
Exit Sub

Err_Form_Open:
MsgBox Err.Description
Resume Exit_Form_Open

And not like this?

Err_Form_Open:
MsgBox Err.Description
Exit Sub
 
Ok...so, if there is an error, goto Err_name...
Err_name tells to display the error message and to go to
Exit_name to exit the sub.

So, why not say it as on error goto Err_name...
Err_name tells to display the error message and exit the
sub?

Maybe I am thinking that there is an unneeded step here?
 
Using an Exit label gives you a single point of exit. Generally, that's a
good idea, unless your code is using a loop. Then it makes sense to exit the
loop once the condition as been satisfied, rather than completing the loop.

Also, the Exit label usually does more than simply exit the sub or function.
It provides a method to close and release object variables. Consider:

Exit_Here:
rst.Close
Set rst = Nothing
Set db = Nothing
Exit Sub

Err_Handler:
MsgBox Err.Number & ": " & Err.Description
Resume Exit_Here

The above makes sure that the recordset variable is closed and that both it
and the database variable are released (set to nothing). Simply exiting the
sub on the error handler wouldn't do that.
--
Arvin Meyer, MCP, MVP
Microsoft Access
Free Access Downloads
http://www.datastrat.com
http://www.mvps.org/access
 
Thank you.
-----Original Message-----
Using an Exit label gives you a single point of exit. Generally, that's a
good idea, unless your code is using a loop. Then it makes sense to exit the
loop once the condition as been satisfied, rather than completing the loop.

Also, the Exit label usually does more than simply exit the sub or function.
It provides a method to close and release object variables. Consider:

Exit_Here:
rst.Close
Set rst = Nothing
Set db = Nothing
Exit Sub

Err_Handler:
MsgBox Err.Number & ": " & Err.Description
Resume Exit_Here

The above makes sure that the recordset variable is closed and that both it
and the database variable are released (set to nothing). Simply exiting the
sub on the error handler wouldn't do that.
--
Arvin Meyer, MCP, MVP
Microsoft Access
Free Access Downloads
http://www.datastrat.com
http://www.mvps.org/access




.
 
Back
Top