Link to home
Start Free TrialLog in
Avatar of holdsworthbros

asked on

Linking images to access database

Hello Experts,
I have what I hope is a simple problem to solve in Access 2000.

I have a database that I would like to display images in. (I recently deleted all the images out of the database becuase it was getting too big).

Each record has a record ID, and all the images I want to display are stored in C:\valuations\
Not every record has an image, but all the images follow a naming format of <record ID Number>.jpg

So record 1234 has an image 1234.jpg

Is there some way I can link the image to the record to display it on the form and in the report (one record per report).
There are two outcomes I would like...

1) To be able to set up a default path to the file for each record so I dont have to manually link each file to each record. If the image is present it will automatically load.

2) To only have the image location/path stored in the database and to have it load 'as required' for viewing and printing purposes (i.e. not stored in the database).

I am a bit of an Access know-nothing so some detail would be appreciated.
Avatar of cquinn
Flag of United Kingdom of Great Britain and Northern Ireland image

Add an image control called ImageFrame to the form, and bind a control called ImageID to the record ID (you can set it to not visible if required, then in the form's OnCurrent event add the following code

 Private Sub Form_Current()
Dim strImageName as String
On Error Resume Next
strImageName="C:\Imagefolder\" & Me.[ImageID] & ".jpg"
if Dir(strImageName)<>"" then strImageName= ""   'Clear the image if one doesn't exist

Me![ImageFrame].Picture = strImageName

 End Sub
Avatar of rockiroads
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Also - you can do it in this way: (Also shows how to do it in reports)
Avatar of holdsworthbros


Hello again,
Okay I have tried to follow rockiroads advice... (I warned you, Im not good at access!) But something has gone astray...

I have a Bound Object Frame (where I would expect the image to come up) called MyImg
(is this the image control? I dont know what an image control is?)

The record ID is displayed on the form
Ive added that code to the form.
Ive also created that module

But I get an error 'Run-time error '438', Object doesnt support this property or method.

Then the de-bugger points to this line
if Dir$(sFile) <> "" Then Me.myImg.Picture = sFile else Me.myImg.Picture = ""
Highlighting:  Me.myImg.Picture = sFile else

Clearly Ive made a motzza of the myImg image control thingy.
No, u dont want a bounded object frame

Its just a image control

On the toolbox toolbar, I think its the one after the command button

When u add the image control, and if it prompts u for a file, then select any image
then go to the field properties for that image control

Clear the setting in "Picture"  - its the file u just selected
Ensure the "picture type" is set to "Linked"
Depending on how big the pics are, u could also set the "Clip" property to be "Stretch"
You my friend are an absolute bloody legend.

Worked a treat.
Just one more thing...
Ive tried doing exactly the same thing in the report, but it didnt work.

The report doesnt like the
sFile = IMAGE_DIR & Me.ID & ".jpg"

Is this more complex?
Where in the report code did u add it? It should more or less work

Did u place it inside

Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)

End Sub

What u might need to do is setup a default image (can be just blank) which u set in your report


    Dim sFile As String
    sFile = IMAGE_DIR & Me.ID & ".jpg"
    If Dir$(sFile) <> "" Then
        Me.myImg.Picture = sFile
        Me.myImg.Picture = IMAGE_DIR & "dummy.jpg"
    End If

Actually scrub that. I think the visible property would suit you better

Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)

    Dim sFile As String
    sFile = IMAGE_DIR & Me.ID & ".jpg"
    If Dir$(sFile) <> "" Then
        Me.myImg.Picture = sFile
        Me.myImg.Visible = True
        Me.myImg.Picture = ""
        Me.myImg.Visible = False
    End If

End Sub

Clearly not, worked when I did put it in there though.