Microsoft KB Archive/285820

= How to display images from a folder in a form, a report, or a data access page =

Article ID: 285820

Article Last Modified on 11/2/2005

-

APPLIES TO


 * Microsoft Office Access 2003
 * Microsoft Access 2002 Standard Edition

-



This article was previously published under Q285820





For a Microsoft Access 2000 version of this article, see 210100.

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 many images, or if each of your image files is large, the size of the Microsoft Access database file can quickly increase.

This article demonstrates a custom function that you can use to:
 * Store file paths and names of images in a table.
 * Display images by using an image control.
 * Hide the image control if no image is available.
 * Provide feedback on the display status of the image.

This article also contains sample Visual Basic script that you can use to display the images in a data access page.

NOTE: Although this example use bitmap images (.bmp), you can also use other image types, such as .jpg, .pcx, and .gif.



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.

Creating the table to store file and path data
 Open the sample database, Northwind.mdb, or the sample project, NorthwindCS.adp.  Create the following table either in Northwind.mdb or in 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: Yes

Column Name: txtImageName Datatype: varchar

Table Properties: ImageTable ---  Primary Key Constraint: ImageID   Open the tblImage table in Datasheet view, and then add the path and name of a bitmap file to each record. The following table of examples shows how the records might look: -- -- --                   
 * Type                | Example                             |
 * Absolute (Local)    | C:\Windows\Zapotec.bmp              |
 * Absolute (UNC Path) | \\Servername\sharename\Zapotec.bmp  |
 * Relative            | Zapotec.bmp                         |

Creating the custom function
  Create a new module, and then paste or type the following code: 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 = &quot;No image name specified.&quot; Else If InStr(1, strImagePath, &quot;\&quot;) = 0 Then ' Path is relative strDatabasePath = CurrentProject.FullName intSlashLocation = InStrRev(strDatabasePath, &quot;\&quot;, Len(strDatabasePath)) strDatabasePath = Left(strDatabasePath, intSlashLocation) strImagePath = strDatabasePath & strImagePath End If       .Visible = True .Picture = strImagePath strResult = &quot;Image found and displayed.&quot; 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 = &quot;Can't find image in the specified name.&quot; Resume Exit_DisplayImage: Case Else      ' Some other error. MsgBox Err.Number & &quot; &quot; & Err.Description strResult = &quot;An error occurred displaying image.&quot; Resume Exit_DisplayImage: End Select End Function </li> Save the module as Module1 .</li></ol>

Using the custom function in a form
  Create the following new form that is based on the tblImage table. <pre class="fixed_text">  Form: frmImage --  Caption: Image Form RecordSource: tblImage

Image Control -  Name: ImageFrame Picture: &quot;C:\Windows\Zapotec.bmp&quot;

Text box --  Name: txtImageID ControlSource: ImageID

Text box ---  Name: txtImageName ControlSource: txtImageName

Text box ---  Name: txtImageNote ControlSource: <Blank> NOTE: 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>  On the View menu, click Code, and then paste or type the following code: Option Compare Database Option Explicit

Private Sub Form_AfterUpdate CallDisplayImage End Sub

Private Sub Form_Current CallDisplayImage End Sub

Private Sub txtImageName_AfterUpdate CallDisplayImage End Sub

Private Sub CallDisplayImage Me!txtImageNote = DisplayImage(Me!ImageFrame, Me!txtImageName) End Sub </li> Open the frmImage form in Form view. Note that the form displays the corresponding bitmap for each record. If the txtImageName field is blank or if the image cannot be found, you receive appropriate messages instead of the image frame.</li></ol>

Using the custom function in a report
  Create the following new report that is based on the ImageTable table. <pre class="fixed_text">  Report: rptImage --  Caption: Image Report RecordSource: tblImage

Image Control -  Name: ImageFrame Picture: &quot;C:\Windows\Zapotec.bmp&quot;

Text box --  Name: txtImageID ControlSource: ImageID

Text box ---  Name: txtImageName ControlSource: txtImageName

Text box ---  Name: txtImageNote ControlSource: <Blank> NOTE: If you do not want the path to appear in the report, you can set the Visible property of the txtImageName control to False. </li>  On the View menu, click Code, and then paste or type the following code: Option Compare Database Option Explicit

Private Sub Detail_Print(Cancel As Integer, PrintCount As Integer) Me!txtImageNote = DisplayImage(Me!ImageFrame, Me!txtImageName) End Sub </li> Open the rptImage report in print preview. Note that the report displays the corresponding bitmap for each record. If the txtImageName field is blank or if the image cannot be found, you receive appropriate messages instead of the image frame.</li></ol>

Duplicating the custom function in a data access page
  Create the following new data access page that is based on the tblImage table. <pre class="fixed_text">  Data Access Page: dapImage -  Title: Image Data Access Page

Image Control -  ID: ImageFrame Text box --  ID: txtImageID ControlSource: ImageID

Text box ---  ID: txtImageName ControlSource: txtImageName NOTE: If you do not want the path to appear in the page, you can set the Visibility property of the txtImageName control to Hidden. </li> On the Tools menu, point to Macros, and then click Microsoft Script Editor.</li>  Add the following script to the Current event of the MSODSC in the HEAD tag portion of the HTML document.

NOTE: You must pass in a parameter in order for the event to be triggered. <SCRIPT language=vbscript event=Current(oEventInfo) for=MSODSC>

</SCRIPT> </li> Open the dapImage page in Page view. Note that the page displays the corresponding bitmap for each record. If the txtImageName field is blank, a control icon is displayed. If the image cannot be found, an X icon appears in the image control.</li></ol>

Use an http:// path in a form
To use an http:// path in a form, use the Web browser control (shdocvw.dll) as follows: <ol> <li>Add a Microsoft Web Browser control to the form and name it WebBrowser .</li> <li> Add the following code to a module: Public Function DisplayImageWeb(ctlBrowserControl As Control, _ strImagePath As Variant)

On Error GoTo Err_DisplayImage

Dim strDatabasePath As String Dim intSlashLocation As Integer

With ctlBrowserControl If IsNull(strImagePath) Then ElseIf Left(strImagePath, 4) = &quot;http&quot; Then .Navigate (strImagePath) Else If InStr(1, strImagePath, &quot;\&quot;) = 0 Then ' Path is relative strDatabasePath = CurrentProject.FullName intSlashLocation = InStrRev(strDatabasePath, &quot;\&quot;, Len(strDatabasePath)) strDatabasePath = Left(strDatabasePath, intSlashLocation) strImagePath = strDatabasePath & strImagePath End If       .Navigate (strImagePath) End If End With Exit_DisplayImage: Exit Function

Err_DisplayImage: Select Case Err.Number Case Else MsgBox Err.Number & &quot; &quot; & Err.Description Resume Exit_DisplayImage: End Select End Function </li> <li> Add the following code behind the form: Option Compare Database Option Explicit

Private Sub Form_AfterUpdate CallDisplayImage End Sub

Private Sub Form_Current CallDisplayImage End Sub

Private Sub txtImageName_AfterUpdate CallDisplayImage End Sub

Private Sub CallDisplayImage DisplayImageWeb Me.WebBrowser9, Me.txtImageName End Sub </li></ol>

Additional query words: link point inf image jpg bmp event graphic filter path picture tif tiff gif store table ACC2002 ACC2003

Keywords: kbhowto kbinterop kbdatabase kbprogramming kbreport kbdesign kbdisplay kbgraphic KB285820

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.