Microsoft KB Archive/210100

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

Article ID: 210100

Article Last Modified on 10/11/2006

-

APPLIES TO


 * Microsoft Access 2000 Standard Edition

-



This article was previously published under Q210100



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).



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.



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
 Open the sample Access database file, Northwind.mdb, or the sample Access Project file, NorthwindCS.adp.  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  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  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 </li></ol>

Displaying the Images on a Form
<ol>  Create the following new form based on the tblImage table: <pre class="fixed_text">  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.

</li>  : 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 </li> : The Set the OnCurrent event and the AfterUpdate event to =setImagePath .</li> : The Open the Imageform form in Form view. Notice that the form displays the corresponding bitmap for each record.</li></ol>

Displaying the Images on a Report
<ol> Create a new report in Design view that is based on the tblImage.</li>  Add the following controls to the "Details" section of the report: <pre class="fixed_text">  Image Control -  Name: ImageFrame Picture: "C:\Windows\Circles.bmp"

Text box --  Name: txtImageID ControlSource: ImageID

Text box ---  Name: txtImageName ControlSource: txtImageName </li>  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 </li> Set the OnFormat event of the "Details" section of the report to =setImagePath and then save the report as RptImage.</li> Open the RptImage report in Print Preview. Notice that the report displays the corresponding bitmap for each record.</li></ol>

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: <ol> Remove the paths from tblImage table, leaving only the names of the bitmap files.</li> 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.</li>  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 </li></ol>

<div class="references_section">