What does On Error Goto 0 do?

  • Thread starter Thread starter Lars Brownies
  • Start date Start date
L

Lars Brownies

In inherited the code beneath. I never use the GoTo option other than for
the err_handler.

What does the code
<<On Error GoTo 0>>
actually do?

Is it necessary or can it be written in a better way?

Thanks
Lars


Function pfCompactRepairDB(strSource As String, strDestination As String) As
Boolean
On Error GoTo Err_Handler
pfCompactRepairDB = Application.CompactRepair(LogFile:=True,
SourceFile:=strSource, DestinationFile:=strDestination)

' Reset the error trap and exit the function.
On Error GoTo 0
Exit Function

Err_Handler:
pfCompactRepairDB = False
Dim strError As String
strError = "Error: " & Err.Number & ": " & Err.Description
MsgBox strError: Call ErrorLog("Generic", "pfCompactRepairDB", strError)
End Function
 
On Sun, 30 Aug 2009 00:07:42 +0200, "Lars Brownies"

There is a comment line above it that explains it.
However, there is no benefit in resetting the error handler if you're
on your way out anyway. Delete both lines.

-Tom.
Microsoft Access MVP
 
On Error GoTo 0 turns off error handling for the current proc.

Er, it turns off the error handler declared for the procedure, and
returns error handling to the default VBA error handler.

The most common use for it is after:

On Error Resume Next

....which turns off both any local error handler and the default VBA
error handler. A typical usage (which I don't endorse):

On Error Resume Next
DoCmd.OpenReport "rptMyReport"
On Error GoTo 0

What this does is allow the report to run and if an error occurs
(such as it returning no data and automatically closing because of a
NoData event that closes it), completely ignores it.

I prefer to trap for the particular error you expect, and ignore
that one error, because you can never be sure that the error you're
ignoring is the one you expected and are aware you want to ignore.

In short, my code includes neither of those On Error statments. The
only On Error statements I use are:

On Error GoTo errHandler
On Error Resume exitRoutine

I think either of the other two statements are evidence of less than
rigorous programming practice.
 
Back
Top