Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

How to display an image from a folder in a form or in a report in Access 2000


For a Microsoft Access 97 and earlier version of this article, see
148463 .

For a Microsoft Access 2002 and Office Access 2003 version of this article, see
285820 .
Advanced: Requires expert coding, interoperability, and multiuser skills.

This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).


↑ Back to the top


Summary

Sometimes it is not practical to store images in a Microsoft Access table. If you have a large number of images, or if each of your image files is large, the size of the Microsoft Access database file can rapidly increase.

This article describes how you can display images on a form or on a report by only specifying the path and the file name. The file name is stored in a table. The path to the file can also be stored in the table, or the path can be a relative path. This depends on the location of the database.

↑ Back to the top


More Information

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. The following example demonstrates how to display Windows bitmap images on an Access form or an Access report without storing the images in an Access table. Although this example uses bitmap images (.bmp), you can also use other image types, such as .jpg, .pcx, and .gif.

Creating the Table to Store the Image Paths

  1. Open the sample Access database file, Northwind.mdb, or the sample Access Project file, NorthwindCS.adp.
  2. Create the following table in either Northwind.mdb or NorthwindCS.adp. In Northwind.mdb:

    Table: tblImage
    ----------------------------
    Field Name: ImageID
    Data Type: AutoNumber
    Indexed: Yes (No Duplicates)

    Field Name: txtImageName
    Data Type: Text

    Table Properties: tblImage
    --------------------------
    PrimaryKey: ImageID
    In NorthwindCS.adp:

    Table: tblImage
    -----------------------
    Column Name: ImageID
    Datatype: Int
    Allow Nulls: Unchecked
    Identity: Checked

    Column Name: txtImageName
    Datatype: varchar

    Table Properties: ImageTable
    -------------------------------
    Primary Key Constraint: ImageID
  3. In Northwind.mdb: Create a standard image to display when the picture in the table cannot be found, or there is not a picture for a record.

    For this example, use a small image called "NoPicture.BMP". You can use something that exists, such as a company logo. Or, you may create a new picture by using Paint. Store the file in the following location:

    C:\Windows\NoPicture.bmp
  4. In Northwind.mdb: Open the tblImage table in Datasheet view, and then add the path and the name of a bitmap file to each record. The following examples show how the records might look:

    C:\Windows\Circles.bmp
    C:\Windows\Waves.bmp
    C:\Windows\Tiles.bmp
    C:\Windows\Bubbles.bmp

Displaying the Images on a Form

  1. Create the following new form based on the tblImage table:

    Form: frmImage
    ----------------------
    Caption: Image Form
    RecordSource: tblImage

    Image Control
    ---------------------------------
    Name: ImageFrame
    Picture: "C:\Windows\Circles.bmp"

    Text box
    ----------------------
    Name: txtImageID
    ControlSource: ImageID

    Text box
    ---------------------------
    Name: txtImageName
    ControlSource: txtImageName
    NOTE: The Picture property of the Image control has been set to the path name for the first image. The Image control must have a valid Picture property in Design view. Also, if you do not want the path to appear in the form, you can set the Visible property of the txtImageName control to False.

  2. : The On the View menu, click Code, and then paste or type the following code:
    Function setImagePath()
    Dim strImagePath As String
    On Error Goto PictureNotAvailable
    strImagePath = Me.txtImageName
    Me.ImageFrame.Picture = strImagePath
    Exit Function
    PictureNotAvailable:
    strImagePath = "C:\Windows\NoPicture.BMP"
    Me.ImageFrame.Picture = strImagePath
    End Function
  3. : The Set the OnCurrent event and the AfterUpdate event to
    =setImagePath().
  4. : The Open the Imageform form in Form view. Notice that the form displays the corresponding bitmap for each record.

Displaying the Images on a Report

  1. Create a new report in Design view that is based on the tblImage.
  2. Add the following controls to the "Details" section of the report:

    Image Control
    ---------------------------------
    Name: ImageFrame
    Picture: "C:\Windows\Circles.bmp"

    Text box
    ----------------------
    Name: txtImageID
    ControlSource: ImageID

    Text box
    ---------------------------
    Name: txtImageName
    ControlSource: txtImageName
  3. On the View menu, click Code, and then paste or type the following code:
    Function setImagePath()
    Dim strImagePath As String
    On Error Goto PictureNotAvailable
    strImagePath = Me.txtImageName
    Me.ImageFrame.Picture = strImagePath
    Exit Function

    PictureNotAvailable:
    strImagePath = "C:\Windows\NoPicture.BMP"
    Me.ImageFrame.Picture = strImagePath

    End Function
  4. Set the OnFormat event of the "Details" section of the report to =setImagePath() and then save the report as RptImage.
  5. Open the RptImage report in Print Preview. Notice that the report displays the corresponding bitmap for each record.

Storing the Image in a Relative Path

The previous example expects the complete path of the file to be in the txtImageName field. However, you may want the table to contain only the name of the image, while the code determines the path depending on what folder the current database is in. The images, in this case, are all in the same folder where the database file is located. This relative path technique is especially useful with distributed applications when you are not sure of which path the user is going to install the database to. In the following code, the path to the current database is determined with the FullName property. The path is then concatenated to the name of the image.

To demonstrate this, make the following changes to the earlier example:
  1. Remove the paths from tblImage table, leaving only the names of the bitmap files.
  2. Put the bitmap files in the same folder where the database is located. This includes the standard bitmap that appears when there is not a picture for a particular record.
  3. Substitute the function in the previous example with the following function:

    NOTE: CurrentProject.FullName, which refers to the full path of the current project, works for both Access Databases and Access Projects.
    Function setImagePath()
    Dim strImagePath As String
    Dim strMDBPath As String
    Dim intSlashLoc As String

    On Error Goto PictureNotAvailable
    'Obtain the full path of the current database or Access Project
    strMDBPath = CurrentProject.FullName

    'Find the location of the last backslash
    intSlashLoc = InStrRev(strMDBPath, "\", Len(strMDBPath))

    'Trim off the database name, leaving the path
    'and append the name of the image file
    strImagePath = Left(strMDBPath, intSlashLoc) & _
    Me.txtImageName

    'Set ImageFrame to the path of the image file
    Me.ImageFrame.Picture = strImagePath
    Exit Function
    PictureNotAvailable:
    strImagePath = "NoPicture.BMP"
    Me.ImageFrame.Picture = strImagePath

    End Function

↑ Back to the top


References

For more information about setting event properties, click Microsoft Access Help on the Help menu, type event in the Office Assistant or in the Answer Wizard, and then click Search to view "Events: Making your database objects work together".

For more information about the OleTypeAllowed property, click Microsoft Access Help on the Help menu, type oletypeallowed in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

For more information about the SourceDoc property, click Microsoft Access Help on the Help menu, type sourcedoc in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

↑ Back to the top


Keywords: kb, kbvba, kbprogramming, kbole, kbofficeprog, kbinterop, kbdta, kbarchive, kb3rdpartynetclient, kb32bitonly, kbgraphic, kbsweptsoltax, kbdisplay, kbhowto

↑ Back to the top

Article Info
Article ID : 210100
Revision : 3
Created on : 4/17/2018
Published on : 4/19/2018
Exists online : False
Views : 427