Error Message bypass

G

Guest

I currently have a report that displays images using the following code:

Private Sub Detail_Format (Cancel As Integer, FormatCount As Integer)
Me![Image1].Picture = Me![FILE PATH]
End Sub

The code works fine, but i do not have images for all of the items on the
report. So when Access tries to format one of these non-existent images, it
produces the following error message:

Run-time error '2220':
Micorsoft Access can't open the file 'C:\123.jpg'.

I would like to have Access display only the images i have and
disregard/skip the ones i don't. i'm sure there some sort of "Error" code i
can use but i'm not really sure.

I would appreciate any help. Thank you.
 
D

Dirk Goldgar

big d said:
I currently have a report that displays images using the following
code:

Private Sub Detail_Format (Cancel As Integer, FormatCount As Integer)
Me![Image1].Picture = Me![FILE PATH]
End Sub

The code works fine, but i do not have images for all of the items on
the report. So when Access tries to format one of these non-existent
images, it produces the following error message:

Run-time error '2220':
Micorsoft Access can't open the file 'C:\123.jpg'.

I would like to have Access display only the images i have and
disregard/skip the ones i don't. i'm sure there some sort of "Error"
code i can use but i'm not really sure.

I would appreciate any help. Thank you.

Here's one solution:

'----- start of code -----
Private Sub Detail_Format (Cancel As Integer, FormatCount As Integer)

On Error Resume Next
Me![Image1].Picture = Me![FILE PATH]
If Err.Number = 2220 Then
' ignore it and clear the image control
Me![Image1].Picture = ""
Else
MsgBox "Error " & Err.Number & " - " & Err.Description, _
vbExclamation, "Error In Report"
End If

End Sub

'----- end of code -----

Here's another:

'----- start of code -----
Private Sub Detail_Format (Cancel As Integer, FormatCount As Integer)

If Len(Dir(Me![FILE PATH])) > 0 Then
Me![Image1].Picture = Me![FILE PATH]
Else
Me![Image1].Picture = ""
End If

End Sub

'----- end of code -----
 
V

Van T. Dinh

Try something like:

Private Sub Detail_Format (Cancel As Integer, FormatCount As Integer)
If Len(Dir(Me![FILE PATH])) > 0 Then
Me![Image1].Picture = Me![FILE PATH]
End If
End Sub

Alternatively, you can use the FileExists() function but in this case, you
need to add the Microsoft Scripting Runtime Library in the References of
your database.
 

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