Dynamic Image Display in a Form

R

Rob

The problem I have is rather easy to explain, but after a lot of
searching I've been unable to find a result. I apologise in advance if
this has been posted earlier, but perhaps I have just not been able to
find the proper wording to search with. Anyway, on to my problem.

I have a UserID field, and a Photograph field in the same table.

I would like this form to read the UserID field, and add ".jpg" to the
Photograph field to display the corresponding image for the UserID.

For example,

Pretend this is my form,

----
UserID : 101
Picture : [displays 101.jpg]

UserID : 102
Picture : [displays 102.jpg]
 
B

bob

Not sure why the 'Photograph' field is required, or what data it contains.


Private Sub Form_Current()

Dim strPath As String

strPath = "C:\path-to-images\" & [UserID] & ".jpg"

If Len(Dir(strPath) > 0) Then
ImageCtrl.Picture = strPath
Else
ImageCtrl.Picture = ""
End If

End Sub



'ImageCtrl' is the name of the Image Control.

You can hard-code the path to the images as illustrated, or use a path relative to the database file (e.g.
a subdirectory of where the database is located) - that way the database can be accessed either via a local
drive, mapped drive or UNC path, and if you move the database and images you won't need to update the code.
The following article contains ways to obtain the database path in various circumstances:

http://tinyurl.com/y3vzzp

Using one of these approaches you would modify the line which generates the path to:

strPath = GetDBPath & "images\" & [UserID] & ".jpg"

This assumes that the images are stored in a subdirectory 'images' of the folder that contains the database,
and that you include one of the sample implementations of GetDBPath.


You should also modify the registry to prevent the 'Loading Image' dialog from appearing, and the associated
crash when scrolling too quickly through records. Details are available here:

http://tinyurl.com/yb7lh7


Note that this won't work in continuous forms.
 
B

bob

Oops - spotted my mistake.

Replace:

If Len(Dir(strPath) > 0) Then

With the following:


If Len([UserID]) > 0 And Len(Dir(strPath)) > 0 Then





bob said:
Not sure why the 'Photograph' field is required, or what data it contains.


Private Sub Form_Current()

Dim strPath As String

strPath = "C:\path-to-images\" & [UserID] & ".jpg"

If Len(Dir(strPath) > 0) Then
ImageCtrl.Picture = strPath
Else
ImageCtrl.Picture = ""
End If

End Sub



'ImageCtrl' is the name of the Image Control.

You can hard-code the path to the images as illustrated, or use a path relative to the database file (e.g.
a subdirectory of where the database is located) - that way the database can be accessed either via a local
drive, mapped drive or UNC path, and if you move the database and images you won't need to update the code.
The following article contains ways to obtain the database path in various circumstances:

http://tinyurl.com/y3vzzp

Using one of these approaches you would modify the line which generates the path to:

strPath = GetDBPath & "images\" & [UserID] & ".jpg"

This assumes that the images are stored in a subdirectory 'images' of the folder that contains the database,
and that you include one of the sample implementations of GetDBPath.


You should also modify the registry to prevent the 'Loading Image' dialog from appearing, and the associated
crash when scrolling too quickly through records. Details are available here:

http://tinyurl.com/yb7lh7


Note that this won't work in continuous forms.



Rob said:
Are there any people who know a solution to this problem?
 
R

Rob

I've been trying to implement your suggestion in to my database, but I
keep running in to an issue with the code that you offered.

When the form loads, I get a runtime error saying :

---
Run-time error 2465

Microsoft Office can't find the field '|' referred to in your
expression.
---


It then opens up the code builder and highlights the path line.

---
strPath = GetDBPath & "images\" & [UserID] & ".jpg"
---

Now, since my abilities in programming for Access are extremely
limited, I'm not sure where to start with this.

Is there a possibility of giving me a few pointers at what to look at,
and what may be causing this problem?


Oops - spotted my mistake.

Replace:

If Len(Dir(strPath) > 0) Then

With the following:


If Len([UserID]) > 0 And Len(Dir(strPath)) > 0 Then





bob said:
Not sure why the 'Photograph' field is required, or what data it contains.


Private Sub Form_Current()

Dim strPath As String

strPath = "C:\path-to-images\" & [UserID] & ".jpg"

If Len(Dir(strPath) > 0) Then
ImageCtrl.Picture = strPath
Else
ImageCtrl.Picture = ""
End If

End Sub



'ImageCtrl' is the name of the Image Control.

You can hard-code the path to the images as illustrated, or use a path relative to the database file (e.g.
a subdirectory of where the database is located) - that way the database can be accessed either via a local
drive, mapped drive or UNC path, and if you move the database and images you won't need to update the code.
The following article contains ways to obtain the database path in various circumstances:

http://tinyurl.com/y3vzzp

Using one of these approaches you would modify the line which generates the path to:

strPath = GetDBPath & "images\" & [UserID] & ".jpg"

This assumes that the images are stored in a subdirectory 'images' of the folder that contains the database,
and that you include one of the sample implementations of GetDBPath.


You should also modify the registry to prevent the 'Loading Image' dialog from appearing, and the associated
crash when scrolling too quickly through records. Details are available here:

http://tinyurl.com/yb7lh7


Note that this won't work in continuous forms.



Rob said:
Are there any people who know a solution to this problem?
 

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