3

I've been searching around on Google to find a better way to show images in Access without actually inserting the image into the database.

I found this article 'http://support.microsoft.com/kb/285820' via this thread 'Is there a way to get ms-access to display images from external files' which goes into great detail on how to set paths to pictures through folders/files, and it works great, for a 'set' picture. However, I want a different picture to display when I switch to a different record.

Here is the code from the article:

Option Compare Database
Option Explicit

Public Function DisplayImage(ctlImageControl As Control, strImagePath As Variant) As String
On Error GoTo Err_DisplayImage

Dim strResult As String
Dim strDatabasePath As String
Dim intSlashLocation As Integer

With ctlImageControl
    If IsNull(strImagePath) Then
        .Visible = False
        strResult = "No image name specified."
    Else
        If InStr(1, strImagePath, "\") = 0 Then
            ' Path is relative
            strDatabasePath = CurrentProject.FullName
            intSlashLocation = InStrRev(strDatabasePath, "\", Len(strDatabasePath))
            strDatabasePath = Left(strDatabasePath, intSlashLocation)
            strImagePath = strDatabasePath & strImagePath
        End If
    .Visible = True
    .Picture = strImagePath
    strResult = "Image found and displayed."
    End If
End With

Exit_DisplayImage:
    DisplayImage = strResult
    Exit Function

Err_DisplayImage:
    Select Case Err.Number
        Case 2220       ' Can't find the picture.
            ctlImageControl.Visible = False
            strResult = "Can't find image in the specified name."
            Resume Exit_DisplayImage:
        Case Else       ' Some other error.
            MsgBox Err.Number & " " & Err.Description
            strResult = "An error occurred displaying image."
            Resume Exit_DisplayImage:
    End Select
End Function

I have a feeling that I need to place a line of code that states something like, show image where Image.ID = "ID", but I can't figure out where to put it without getting errors. Am I over-looking something perhaps, or am I approaching this the wrong way? I just don't want to clutter my database, memory-wise, with .bmp images, but I feel like I am going to have to.

SOLVED: A much easier solution is as Gord Thompson has described below in the comments. And from my own experience, using this method for .bmp images leaves the picture distorted and out of contrast. I tested the image for .jpg and it worked perfectly! I hope this helps others who are having trouble with similar problems finds this post helpful.

braX
  • 11,506
  • 5
  • 20
  • 33
Dylan
  • 81
  • 1
  • 2
  • 10
  • Have you tried debugging this code? Are you passing a valid path to an image? – ron tornambe Sep 15 '14 at 21:41
  • @rontornambe When I last ran the code, there were no syntax errors, so I must have a logic error somewhere. The path has to be valid because it's literally a folder on my desktop, and I made sure to right-click the image and copy the entire path from the properties. I have a feeling that my problems arise from the nested 'if' function within the 'with' function. I've been taking little stabs at the code and trying to get different things to work, but VBA is slapping me in the face at every turn. – Dylan Sep 15 '14 at 21:56
  • @Dylan, If you use the same app in your home computer and in your laptop be sure that you use a network path (reachable) from the 2 computers not local path. –  Jan 04 '16 at 12:41

1 Answers1

5

The Microsoft Support article you cited applies to Access 2003. In Access 2010 (and later) there is a much simpler way to do it. All you need to do is place an Image control on the form and bind it to the field in your table that contains the path to the image file.

For example, with an [Employees] table like this

EmployeeID  FirstName  LastName  PhotoPath                        
----------  ---------  --------  ---------------------------------
         1  Gord       Thompson  C:\Users\Public\Pictures\Gord.jpg
         2  Hank       Kingsley  C:\Users\Public\Pictures\Hank.png

you could use an Image control whose Control Source is the [PhotoPath] field ...

DesignView.png

... and the image will automatically be retrieved from the file specified

FormView.png

No VBA required.

Note also that the image files do not have to be .bmp files.

Added by an anonymous user:

For users of the Microsoft Office 365 version of MS Access (c. 2020), here is what you may need to know in order to get this terrific solution to work: The [PhotoPath] field in its data table needs to be "Long Text" data type, if using long paths or long file names. The [PhotoPath] field format "Is Hyperlink" may need to be set to "No." I was getting additional, unwanted coding from Access on my text inputs. The [Image3] control may need to specify "Linked" rather than "Embedded."

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
  • This is absolutely amazing! I would've been trying to manipulate that code for days to try to get it to do what I want. I'm going to have to put on the dunce cap for this one. Thanks Gord! – Dylan Sep 16 '14 at 04:56
  • 1
    just a quick question about the data type for the PhotoPath field, it has to be 'text' right? – Dylan Sep 16 '14 at 14:30
  • @Dylan I would think so, yes. – Gord Thompson Sep 16 '14 at 14:36
  • I seem to be having an issue with showing the image. It worked fine from my home computer, but on my laptop, it isn't showing the image. I am thinking it has something to do with my path, but I can't seem to figure out what. The laptop I am using is a work laptop where you need admin privilages to do anything. Could this be an issue when making paths for images? – Dylan Sep 16 '14 at 14:46
  • @Dylan I'd expect that if you can open an image file directly (by double-clicking it) then Access should be able to open it and display it in the Image control. – Gord Thompson Sep 16 '14 at 15:14
  • @Dylan ... or you could try this: Create a new form in Design View. Put an Image control on it, and when Access prompts you to "Insert Picture" you could browse to one of the pictures that is giving you trouble. If that fails, then perhaps your copy of Office at work does not have all of the [Graphics Filters](http://i.stack.imgur.com/dACL8.png) installed. – Gord Thompson Sep 16 '14 at 15:29
  • Sorry for the late reply, had to step out of the office. I'll try making a new form with an image control on it. And I agree, it should theoretically open up as the folder containing the images I want to use is easy to open (i.e: C:\Documents and Settings\siteworkshop\Desktop\Database\Converted Bmp Pictures\Sitework Yard\*name of image*). I might just have to mess around with it a bit more, I've probably overseen a step somewhere along the way... It's Murphy's Law. – Dylan Sep 16 '14 at 15:42