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.
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.
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 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:
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.
: 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
: The Set the OnCurrent event and the AfterUpdate event to
=setImagePath().
: The Open the Imageform form in Form view. Notice that the
form displays the corresponding bitmap for each record.
Create a new report in Design view that is based on the
tblImage.
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
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
Set the OnFormat event of the "Details" section of the
report to =setImagePath() and then save the report as
RptImage.
Open the RptImage report in Print Preview. Notice that the
report displays the corresponding bitmap for each record.
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:
Remove the paths from tblImage table, leaving only the
names of the bitmap files.
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.
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
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.