Linking .JPG files using VBA

P

Peter Martin

I have a Table that includes the field "FileLocation" which contains the full
file name of various JPG files.

I wish to preview those files on an Access Form but I do not wish to
manually insert the protographs one-by-one: rather I wish the photo to be
displayed or, create an additional OLE Object field in the Table and, using
VBA, populate that field with the required linkages.
 
A

Allen Browne

What version of Access is this?

In Access 2007, you can bind an image control to a field. Dead simple. Even
works even on continuous view forms.

In older versions, use the Current event of the form to set the Picture
property of the image control. The example below assumes:

a) An image control named imgSign
b) A text field containing the name of the file
c) A text box (txtImageFolder) containing the name of the folder
d) A label (lblNoImage) to display if there is no image.

Private Function LoadTheImage(varFile As Variant) As Boolean
On Error GoTo Err_Handler
'Purpose: Show/hide the picture, loading from file.
'Return: True if image loaded.
Dim bShow As Boolean
Dim strFullPath As String

With Me.imgSign
If Not (IsNull(varFile) Or IsNull(Me.txtImageFolder)) Then
'Check the file is still here. Can't show if it is not.
strFullPath = Me.txtImageFolder & varFile
If FileExists(strFullPath) Then
bShow = True
If .Picture = strFullPath Then
'do nothing
Else
.Picture = strFullPath
End If
End If
End If
If .Visible <> bShow Then
.Visible = bShow
Me.lblNoImage.Visible = Not bShow
End If
End With

Exit_Handler:
Exit Function

Err_Handler:
MsgBox "Error " & Err.Number & ": " & Err.Description
Resume Exit_Handler
End Function

For FileExists(), see:
http://allenbrowne.com/func-11.html
 
L

llamadave

What version of Access is this?

In Access 2007, you can bind an image control to a field. Dead simple. Even
works even on continuous view forms.

Could you elaborate on this? I have small jpg thumbnails in an ole
field and have been displaying them in an image control with the
Lebans.com method but they don't work on continuous view forms. Now I
have upgraded to Access 2007 and can't find any documentation on how
to do this. Dead simple?

David Elmore
 
A

Allen Browne

Just create a Text field.
Insert the fully qualified file name, e.g.:
C:\MyFolder\MyPic.bmp

In form design view, add an Image control from the ribbon. If you have to
select an image to get this to work, clear the Picture property aftertwards.

Then set the Control Source of the image control to the name of the text
field.
 

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