| re: I'm goofed up in On Error
"Randy Harris" <randy@SpamFree.com> wrote in message
news:RoFTb.36255$P%1.28518287@newssvr28.news.prodi gy.com...[color=blue]
> I thought that I had a grasp of how VBA error handling functioned, but[/color]
have[color=blue]
> just become painfully aware that I don't.
>
> I thought that the "On Error GoTo 0" in the second sub below would turn[/color]
off[color=blue]
> error handling for that procedure. I single stepped the program, it gets[/color]
an[color=blue]
> error on the last line (the Name Workdir... line), the next command to
> execute is Resume SkipReport. I know why the error occurs, but I don't
> understand how it resumes in the error handler from the other procedure.
>
> Would someone kindly explain this for me.
>
> -----------------------------------------------------
> Public Sub BatchPrint()
>
> On Error GoTo BadReport
> ChangeName "File1", "File2"
> [other code in here removed]
> Exit Sub
>
> BadReport:
>
> '====== Continues here
> Resume SkipReport
> End Sub
> -------------------------------------------------------------
> Private Sub ChangeName(FromName As String, ToName As String)
>
> On Error Resume Next
> Kill ToName
>
> On Error GoTo 0
> ' ===== Errors here
> Name WorkDir & FromName As WorkDir & ToName
>
> End Sub
> -------------------------------------------------------[/color]
When you say that "On Error GoTo 0" will "turn off error handling for that
procedure", that is correct in the sense of you no longer have any control
over what happens when an error occurs. Access will handle that error for
you - normally simply by displaying an error message. However, in this case
ChangeName has been called from BatchPrint so Access tries to do something
with that un-handled error by going back to BatchPrint and looking for
error-handling code there.
My advice would be to re-write ChangeName from a sub to a function which
returns either true or false but does not display an error message - you can
deal with the function returning false in your BatchPrint. You could write
more detailed error-handling, but here I have used On Error Resume Next to
cover both lines, so either true or false is returned. If this was my code
I might do a bit more checking of the file names and report the error more
specifically (illegal file name, lack of permissions, etc)
Private Function ChangeName(FromName As String, ToName As String) As Boolean
On Error Resume Next
Kill ToName
Name WorkDir & FromName As WorkDir & ToName
If Err.Number = 0 Then ChangeName = True
End Sub
Public Sub BatchPrint()
On Error GoTo BadReport
If ChangeName("File1", "File2") = True Then
Msgbox "Good"
Else
Msgbox "Bad"
End If
....
.....
etc
Fletcher |