Morphing Data?

A

AccessNewbie

Here is the scenario: DB contains an image object on a form. The image
that appears is a photo of a cub scout, with the name of the image set
to match the unique "ScoutID" of the individual. Everything works
fine, until the next time I open the form, when I get a runtime error
2220: "Microsoft Office Access can't open the file 'name.jpg.'

If I delete the image object on the form, replace it with exactly the
same object, named the same, it works fine. Then, next time I open the
form, again with the error.

The code behind the image object:


Private Sub Form_Current()

Dim sFileName As String

sFileName = "C:\Documents and Settings\Mr. Admin\My Documents\My
Pictures\Cub Scouts\" & Me.ScoutID & ".jpg"

' See if this photo exists
' Returns a zero length string if file does not exist
sFileName = Dir(sFileName)

If Len(sFileName & vbNullString) = 0 Then
' Load "Not Available" Photo
Me.ImagePhoto.Picture = "C:\Documents and Settings\Mr. Admin\My
Documents\My Pictures\Cub Scouts\NotAvailable.jpg"
Else
' Load the matching Photo for this ScoutID
Me.ImagePhoto.Picture = sFileName


End If

End Sub


Can anyone tell what's going on here? I actually have the same problem
in two databases, both using images as described.

Thanks for your suggestions,
David

PS: I know little about VB coding; I got the code above from an old
discussion thread somewhere.
 
A

Arvin Meyer [MVP]

Your code is fine. I've been using similar code without incident for at
least 7 or 8 years now. The dot after the "Mr" in the path may be the
problem. Try another file at another path and see if you still have the
problem.
--
Arvin Meyer, MCP, MVP
Microsoft Access
Free Access downloads:
http://www.datastrat.com
http://www.mvps.org/access
 
M

MacDermott

It might be instructive, when you get this error, to press ctl-Pause and see
what line in the code is causing the error.
You can also determine what the ScoutID is at that point.

HTH
 
A

AccessNewbie

Thanks for the suggestions; I'm sorry that it seems less than obvious
what the problem is. I moved the image folder, to simplify the path:

Private Sub Form_Current()

Dim sFileName As String

sFileName = "C:\Cub Scouts\" & Me.ScoutID & ".jpg"

' See if this photo exists
' Returns a zero length string if file does not exist
sFileName = Dir(sFileName)

If Len(sFileName & vbNullString) = 0 Then
' Load "Not Available" Photo
Me.ImagePhoto.Picture = "C:\Documents and Settings\Mr. Admin\My
Documents\My Pictures\Cub Scouts\NotAvailable.jpg"
Else
' Load the matching Photo for this ScoutID
Me.ImagePhoto.Picture = sFileName


End If

End Sub

I still get the same error, which occurs when the program attempts to
open the form. Ctrl-Pause does nothing. If I debug, I get highlight
on the line:

Me.ImagePhoto.Picture = sFileName

Any other ideas?

Thanks again for the help.

David
 
A

AccessNewbie

Oh, and one more thing: Access seems to have no problem loading the
defauld image, which it does after I cancel out of the debugger.
 
M

MacDermott

When you debug and get the highlight on this line:
Me.ImagePhoto.Picture = sFileName
what is the value of sFileName?
 
A

AccessNewbie

Aha, excellent question. For some reason--I don't see why, but it must
have to do with the line sFileName = Dir(sFileName)--it values
SFileName only "1.jpg", without the path. So, and I think this is
inelegant but effective, I repeated the statement that defines the
variable, inside the If Then Else. That has solved the problem.
(Maybe I could have just deleted "sFileName = Dir(sFileName)", but I
hate to monkey around with things now that it works. Maybe I'll try
that when I repair the second DB...)

Thanks for the help, and let me know if you see a problem with my
fix...

Regards,
David


Private Sub Form_Current()

Dim sFileName As String

sFileName = "C:\Documents and Settings\Mr. Admin\My Documents\My
Pictures\Cub Scouts\" & Me.ScoutID & ".jpg"

' See if this photo exists
' Returns a zero length string if file does not exist
sFileName = Dir(sFileName)

If Len(sFileName & vbNullString) = 0 Then
' Load "Not Available" Photo
Me.ImagePhoto.Picture = "C:\Documents and Settings\Mr. Admin\My
Documents\My Pictures\Cub Scouts\NotAvailable.jpg"
Else
' Repeated definition of variable, placed to resolve fileopen errors
sFileName = "C:\Documents and Settings\Mr. Admin\My Documents\My
Pictures\Cub Scouts\" & Me.ScoutID & ".jpg"
' Load the matching Photo for this ScoutID
Me.ImagePhoto.Picture = sFileName

End If

End Sub
 

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