Revision History
Site Map

Graph: DBPix image storage vs OLE Embedding and Linking in Microsoft Access

The Access Image FAQ < Previous ArticleNext Article >

Use Relative Paths for Linked Images

Sample Newsgroup Questions 
Relative path vs. absolute path possible in storing pictures?
db using photos - where to keep the photos
Relative Path does not work for displaying JPEG Images
Paths for Graphics
Picture on a form: Using a relative path

Storing full image paths in the database (e.g. "c:\db-images\img1234.jpg") can cause a maintenance headache when (for example):

  • The images need to be moved to a different disk (or server), e.g. to free disk space.
  • Users need to access the database or images over the network, via a UNC path or mapped drive.
  • Different users access the database & images by various methods.
  • The installation path of the database & images is not known at design-time.
In such cases, updating the paths would require writing parsing code to write the new path to every record. Additionally, the data is not well 'normalized', since there is much repetition of common data between records (the common part of the path).

The solution is to store only the filename or relative path, and to generate the full path in code at runtime (e.g. when the image needs to be displayed). Here's a simple example.

Private Sub Form_Current()
    Image1.Picture = "c:\db-images\" & Me!Filename
End Sub

In the code above, 'Image1' is the name of the image control.
'Filename' is the name of the field that contains the filename (e.g. 'img1234.jpg').

This gives us a single item to change if the images need to be moved, but it doesn't solve all of the problems (files accessed by different methods, path not known at design-time, missing files or an empty filename field).

A solution is to store the images in a fixed subdirectory of the location of the database mdb file. For example, if the database is in the folder "C:\mydb\" store the images in "C:\mydb\images\". Then, if we can obtain the path to the database file at runtime, we can find the images (without needing to know the path at design-time). It also works whether the database is opened via a local drive, UNC path or mapped drive, and if the database is moved the images will still be found, without needing any changes to the code or data (as long as the images are moved with the database) .

There are various ways to obtain the path to the database, depending on the version of Access in use and whether the database is a split (front-end/back-end) design or not. For details see the article Get the Path of the Database (.mdb) File. In the samples below we will use the simplest scenario, which is for Access 2000 or later, and assumes that the database is not a split (FE/BE) design. The code to obtain the paths to the database and images are localized in their own functions, so it's easy to substitute one of the other techniques from the article above. Since these functions are likely to be used in various forms and reports they could be shared by placing them in a common module.

Public Sub Form_Current()
    Image1.Picture = GetImagePath & Me!Filename
End Sub

Public Function GetImagePath() As String
    GetImagePath = GetDBPath & "images\"
End Function

Public Function GetDBPath() As String
    GetDBPath = CurrentProject.Path & "\"
End Function

This code now meets our requirements, but gives errors if the filename field is empty, or if the specificied file does not exist. The following code adds checks to prevent these errors:

Private Sub Form_Current()
    Dim ImagePath As String
    ImagePath = GetImagePath & Me!Filename

    If Len([Filename]) > 0 And Len(Dir(ImagePath)) > 0 Then
        Image1.Picture = ImagePath
        Image1.Picture = ""
    End If
End Sub

Public Function GetImagePath() As String
    GetImagePath = GetDBPath & "images\"
End Function

Public Function GetDBPath() As String
    GetDBPath = CurrentProject.Path & "\"
End Function

Related Articles

Use autonumber fields for image file names
Parse the Folder or Filename from a Full Path
Get the Path to the Database (.mdb) File

Imaging for Access that's Easy, Efficient & Fast
  • NO OLE Bloat
  • NO App Dependencies
  • NO Complex Coding
  • NO Performance Penalty
  •  DBPix logo
    Read More

    Microsoft and the Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries.