Problem with error 53

D

Doug Sanders

I've got a small loop that looks for data in 1 of 4 places. If it doesn't
find a valid path it should create an error 53 which should then be
processed.

In most cases, the path to the desired file will be invalid and Error 53
will occur.

The code is this:

PictureNum = 0

Check1:
PictureNum = PictureNum + 1
If PictureNum = 5 Then Exit Sub

On Error GoTo Error53Check

If PictureNum = 1 Then FilePath = ("N:\Casefiles\" & [CASENO] & "\" &
[CASENO] & "-dp0.jpg")
If PictureNum = 2 Then FilePath = ("N:\Casefiles\" & [CASENO] & "\" &
[CASENO] & "-dp00.jpg")
If PictureNum = 3 Then FilePath = ("N:\Casefiles\" & [CASENO] &
"-dp0.jpg")
If PictureNum = 4 Then FilePath = ("N:\Casefiles\" & [CASENO] &
"-dp00.jpg")
Open FilePath For Input As #10
Close #10
Me![Image151].Picture = FilePath
GoTo Check1

Error53Check:
ErrNum = err
If err <> 53 Then GoTo MsgBoxNot53
If PictureNum = 4 Then Exit Sub
GoTo Check1

MsgBoxNot53:
Call Error_Handler(ErrNum) 'MsgBox "Some other error occured.
Error Number " & err


The first time through, it works fine when it has a bad path, it errors and
loops back. But when it loops back to 'Check1', increments PictureNum and
doesn't find the the path, instead going to Error53Check again, I get the
standard Access error box.

Hope this makes sense.

Thanks,
Doug Sanders
 
D

Dirk Goldgar

In
Doug Sanders said:
I've got a small loop that looks for data in 1 of 4 places. If it
doesn't find a valid path it should create an error 53 which should
then be processed.

In most cases, the path to the desired file will be invalid and Error
53 will occur.

The code is this:

PictureNum = 0

Check1:
PictureNum = PictureNum + 1
If PictureNum = 5 Then Exit Sub

On Error GoTo Error53Check

If PictureNum = 1 Then FilePath = ("N:\Casefiles\" & [CASENO] &
"\" & [CASENO] & "-dp0.jpg")
If PictureNum = 2 Then FilePath = ("N:\Casefiles\" & [CASENO] &
"\" & [CASENO] & "-dp00.jpg")
If PictureNum = 3 Then FilePath = ("N:\Casefiles\" & [CASENO] &
"-dp0.jpg")
If PictureNum = 4 Then FilePath = ("N:\Casefiles\" & [CASENO] &
"-dp00.jpg")
Open FilePath For Input As #10
Close #10
Me![Image151].Picture = FilePath
GoTo Check1

Error53Check:
ErrNum = err
If err <> 53 Then GoTo MsgBoxNot53
If PictureNum = 4 Then Exit Sub
GoTo Check1

MsgBoxNot53:
Call Error_Handler(ErrNum) 'MsgBox "Some other error occured.
Error Number " & err


The first time through, it works fine when it has a bad path, it
errors and loops back. But when it loops back to 'Check1',
increments PictureNum and doesn't find the the path, instead going to
Error53Check again, I get the standard Access error box.

Hope this makes sense.

Thanks,
Doug Sanders

You need to use the Resume statement to exit your error-handler, not a
Go To statement. If you just branch out with a Go To, you are still in
the error-handler as far as VBA is concerned, and so a new error will be
passed upward for handling.

It's going to be hard to get that sorted out without revising your whole
looping structure. All this "Going To" makes for convoluted code that
is hard to debug. How about something like this instead:

'----- start of revised code -----

' ... code before this ...

Dim astrFilePath As Variant
Dim FilePath As String
Dim blnGotPic As Boolean

astrFilePath = Array( _
"N:\Casefiles\" & [CASENO] & "\" & [CASENO] & "-dp0.jpg", _
"N:\Casefiles\" & [CASENO] & "\" & [CASENO] & "-dp00.jpg", _
"N:\Casefiles\" & [CASENO] & "-dp0.jpg", _
"N:\Casefiles\" & [CASENO] & "-dp00.jpg" _
)

On Error GoTo Error53Check

For PictureNum = LBound(FilePath) To UBound(FilePath)
FilePath = astrFilePath(PictureNum)
If Len(Dir(FilePath)) > 0 Then
Me![Image151].Picture = FilePath
Exit For
End If
NextPicture:
Next PictureNum

' If necessary, you could have code that checks whether
' you ever found a picture or not, and does something
' accordingly.

Exit_Point:
Exit Sub

Error53Check:
If Err.Number = 53 _
Or Err.Number = 52 _
Then
Resume NextPicture
Else
ErrNum = Err.Number
Call Error_Handler(ErrNum)
' or otherwise handle error
Resume Exit_Point
End If

End Sub
'----- end of revised code -----

Note that I replaced your code to check the file by opening it with code
a call to the Dir() function to see if the file exists. If you really
need to verify that you can open the file, not just that it exists, that
replacement will have to be undone. With this change in place, I don't
think you're going to get error 53, anyway, but you might get error 52
("Bad file name or number"), so I added that to the error-handling code
just in case.
 
D

Doug Sanders

Of course, 'Resume' is missing.

Thanks, and I'll review your other ideas too.

Thanks a lot.

Doug Sanders


Dirk Goldgar said:
In
Doug Sanders said:
I've got a small loop that looks for data in 1 of 4 places. If it
doesn't find a valid path it should create an error 53 which should
then be processed.

In most cases, the path to the desired file will be invalid and Error
53 will occur.

The code is this:

PictureNum = 0

Check1:
PictureNum = PictureNum + 1
If PictureNum = 5 Then Exit Sub

On Error GoTo Error53Check

If PictureNum = 1 Then FilePath = ("N:\Casefiles\" & [CASENO] &
"\" & [CASENO] & "-dp0.jpg")
If PictureNum = 2 Then FilePath = ("N:\Casefiles\" & [CASENO] &
"\" & [CASENO] & "-dp00.jpg")
If PictureNum = 3 Then FilePath = ("N:\Casefiles\" & [CASENO] &
"-dp0.jpg")
If PictureNum = 4 Then FilePath = ("N:\Casefiles\" & [CASENO] &
"-dp00.jpg")
Open FilePath For Input As #10
Close #10
Me![Image151].Picture = FilePath
GoTo Check1

Error53Check:
ErrNum = err
If err <> 53 Then GoTo MsgBoxNot53
If PictureNum = 4 Then Exit Sub
GoTo Check1

MsgBoxNot53:
Call Error_Handler(ErrNum) 'MsgBox "Some other error occured.
Error Number " & err


The first time through, it works fine when it has a bad path, it
errors and loops back. But when it loops back to 'Check1',
increments PictureNum and doesn't find the the path, instead going to
Error53Check again, I get the standard Access error box.

Hope this makes sense.

Thanks,
Doug Sanders

You need to use the Resume statement to exit your error-handler, not a
Go To statement. If you just branch out with a Go To, you are still in
the error-handler as far as VBA is concerned, and so a new error will be
passed upward for handling.

It's going to be hard to get that sorted out without revising your whole
looping structure. All this "Going To" makes for convoluted code that
is hard to debug. How about something like this instead:

'----- start of revised code -----

' ... code before this ...

Dim astrFilePath As Variant
Dim FilePath As String
Dim blnGotPic As Boolean

astrFilePath = Array( _
"N:\Casefiles\" & [CASENO] & "\" & [CASENO] & "-dp0.jpg", _
"N:\Casefiles\" & [CASENO] & "\" & [CASENO] & "-dp00.jpg", _
"N:\Casefiles\" & [CASENO] & "-dp0.jpg", _
"N:\Casefiles\" & [CASENO] & "-dp00.jpg" _
)

On Error GoTo Error53Check

For PictureNum = LBound(FilePath) To UBound(FilePath)
FilePath = astrFilePath(PictureNum)
If Len(Dir(FilePath)) > 0 Then
Me![Image151].Picture = FilePath
Exit For
End If
NextPicture:
Next PictureNum

' If necessary, you could have code that checks whether
' you ever found a picture or not, and does something
' accordingly.

Exit_Point:
Exit Sub

Error53Check:
If Err.Number = 53 _
Or Err.Number = 52 _
Then
Resume NextPicture
Else
ErrNum = Err.Number
Call Error_Handler(ErrNum)
' or otherwise handle error
Resume Exit_Point
End If

End Sub
'----- end of revised code -----

Note that I replaced your code to check the file by opening it with code
a call to the Dir() function to see if the file exists. If you really
need to verify that you can open the file, not just that it exists, that
replacement will have to be undone. With this change in place, I don't
think you're going to get error 53, anyway, but you might get error 52
("Bad file name or number"), so I added that to the error-handling code
just in case.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top