Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to display photo on the form and report?

Status
Not open for further replies.

hshaker

Technical User
Jun 29, 2007
66
CA
Hi there,

I would like to know how to display a picture that is a OLE linked object on the form and then later print on the report.

If you are an expert in Access, then you just create a table with OLE OBJECT FIELD and then a form with that field only. Try for yourself. How come it only display the name of the photograph. Please do not direct me to a web site. This should be simple.

thanks.
 
So you think this is easy?

If I have a database that saves only the file path, this is my method.

On a form I put a textbox and an image control. The text box is bound to the field with the pictures path. For this example my text box is called "txtBxPicPath" and my image control is called "imgCntrlOne"

On any form or report all I need do the following

Code:
Option Compare Database
Option Explicit
Private objPictureFromFile As PictureFromFile

Private Sub Form_Open(Cancel As Integer)
   Set objPictureFromFile = New PictureFromFile
   Set objPictureFromFile.PictureForm = Me.Form
   Set objPictureFromFile.PictureControl = Me.imgCntrlOne
   Set objPictureFromFile.PictureNameControl = Me.txtBxPicPath
End Sub

Your done. As you change the records the img control will show the picture from the path in txtBxPicPath.

Now to make this work you have to use the following class module that I built. Build a class module called exactly
"PictureFromFile"

Now drop the following code in this class module.
Code:
'This class works when the folder with pictures is located in the same folder as the linked tables
'or the current database
Option Compare Database
Option Explicit

Private mPictureNameControl As TextBox
Private mImagePath As String
Private mImageControl As Image
Private WithEvents mForm As Access.Form
Private WithEvents mReport As Access.Report

Public Property Set PictureNameControl(thePictureNameControl As TextBox)
  Set mPictureNameControl = thePictureNameControl
 End Property

Public Property Set PictureControl(theControl As Image)
  Set mImageControl = theControl
End Property

Public Property Set PictureForm(theForm As Access.Form)
  On Error GoTo HandleError
  Set mForm = theForm
  mForm.OnCurrent = "[Event Procedure]"
  Exit Property
HandleError:
  MsgBox Err.Number & "  " & Err.Description
End Property

Public Property Set PictureReport(theReport As Access.Report)
  Set mReport = theReport
  mReport.OnPage = "[Event Procedure]"
  mReport.OnActivate = "[Event Procedure]"
End Property

Private Sub subLoadImage()
   Dim strImagePathAndFile As String
   Dim intSlashLoc As Integer
   Dim intLastSlashLoc As Integer
   Dim objFileSystem As Object
   Dim connection As String
   Set objFileSystem = CreateObject("Scripting.FileSystemObject")
  ' On Error GoTo PictureNotAvailable
   'Obtain the full path of the current database or the linked database
      strImagePathAndFile = mPictureNameControl.Value
   If objFileSystem.FileExists(strImagePathAndFile) Then
      mImageControl.Picture = strImagePathAndFile
      mImagePath = strImagePathAndFile
   Else
      mImageControl.Picture = ""
   End If
   Exit Sub
PictureNotAvailable:
   MsgBox Err.Number & "  " & Err.Description & Chr(13) & "In PictureFromFile Class"
End Sub
Private Sub mForm_Current()
  Call subLoadImage
End Sub
Private Sub mReport_Activate()
  Call subLoadImage
End Sub
Private Sub mReport_Page()
  Call subLoadImage
End Sub
Public Property Get ImagePath() As String
  ImagePath = mImagePath
End Property
 
Disregard this comment in the class module:
'This class works when the folder with pictures is located in the same folder as the linked tables
'or the current database

This code was originally part of a far more complex class module.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top