Coding for Form if Error on loading Image

K

Ken Cobler

I have a movie database, with the movies autonumbered (MovieID). Plus I also
load an image of each movie. My code, below, allows me not to bother with a
field for image path, since the images will all be in one spot, and all match
the MovieID number plus a ".jpg" extension.

My question: I want to create a default "No Image" picture for those
records which don't yet have pictures. Currently the form is using whatever
the last picture was, even though the picture name does not match the MovieID.

I am new to coding.

Please advise. Thank you.



Option Compare Database
Option Explicit
Dim filename As String, pathname As String

Private Sub Form_Activate()
'find the path of the current database
'which is where the jpegs are stored
pathname = CurrentProject.Path
End Sub

Private Sub Form_Current()
On Error GoTo Err_cmdClose_Click

'set the picture path
Me.ImageFrame.Picture = pathname & "\Images\" & Me.MovieID & ".jpg"
Exit_cmdClose_Click:
Exit Sub

Err_cmdClose_Click:
If Err.Number = 2220 Then 'can't find the file
Resume Next
Else
MsgBox Err.Description
Resume Exit_cmdClose_Click
End If
End Sub
 
S

Steve Schapel

Ken,

I think this will do it...

filename = pathname & "\Images\" & Me.MovieID & ".jpg"
If Len(Dir(filename)) Then
Me.ImageFrame.Picture = filename
Else
Me.ImageFrame.Picture = pathname & "\Images\YourNoImage.jpg"
End If
 
K

Ken Cobler

That works just great. Thank you!

Steve Schapel said:
Ken,

I think this will do it...

filename = pathname & "\Images\" & Me.MovieID & ".jpg"
If Len(Dir(filename)) Then
Me.ImageFrame.Picture = filename
Else
Me.ImageFrame.Picture = pathname & "\Images\YourNoImage.jpg"
End If
 

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