Error Handler Form-OnOpen

  • Thread starter Thread starter Junior
  • Start date Start date
J

Junior

I have placed code in a Form's Open event
but not correctly writing error handler
What is the correct syntax for the error handler
in a form open event?
Thanks
Such as the below for a click event
On Error GoTo Err_cmdfrmReview_Click
 
The "On Error GoTo <label>" refers to a label elsewhere in the procedure to go to. In the
example you gave, there would be a line in the procedure

Err_cmdfrmReview_Click:

This is the point that the error handler would jump to if there is an error. Here is an
example of a common, basic error handler.

Example:
Private Sub SomeSubroutine()
On Error GoTo HandleError
'Some code here that may create an error

CleanUp:
On Error Resume Next
rst.Close
Set rst = Nothing
Set db = Nothing
Exit Sub

HandleError:
MsgBox Err.Number & vbCrLf & Err.Description, , "Error"
Resume CleanUp
End Sub

This will go to the error handler marked by HandleError: if there is an error. When the
error handler is done it take you to the routine to clean up any object variables that may
have been set. The clean up of these is before the error handler, because if the code
finishes normally, I want it to run the clean up routine anyway. You then put an Exit Sub
or Exit Function (depending on whether you're in a sub or function) to keep the code from
getting to the error handler during normal processing.
 
Thanks Wayne - i was trying to duplicate the error handling code produced
say when a macro is converted to VBA - i'll tyr this out
 
Back
Top