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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Manage an "image" field in VB/Access

Status
Not open for further replies.

drimades

IS-IT--Management
Nov 8, 2004
221
MK
-----------------------------------

Hello!

If I need an image field in a table (Access) can I use a string for indicating just the path of the ".jpg" file and then every time retrieve it from the path?

I need a procedure for loading the file in the Access Database.

Thanks!


-----------------------------------
 
I can not tell if you want to populate a table with OLE images or if you want to display an image from a path. This examples displays an image in a form or report based on a path.
This is how I do it. I built a class module called "pictureFromFile". My picture folder must either be in the location of the application or the location of the linked tables. It works with an image control on a report or a form. I am saving one picture per item, and it has the System ID. For example "1.JPG, 33.JPG". Therefore I just look for a picture matching the system ID. Here is 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
Option Compare Database
Option Explicit

Private mPictureNameControl As TextBox
Private mImagePath As String
Private mImageControl As Image
Private mPictureDirectoryName As String
Private mLinkedTableName As String
Private mBlnLinked As Boolean
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 strDBPathAndFile As String
Dim strImagePath As String
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
If mBlnLinked Then
connection = CurrentDb.TableDefs(mLinkedTableName).Connect
strDBPathAndFile = Mid(connection, 11)
Else
strDBPathAndFile = Application.DBEngine(0).Databases(0).Properties("Name")
End If
intSlashLoc = 1
Do
intLastSlashLoc = intSlashLoc
intSlashLoc = InStr(intSlashLoc + 1, strDBPathAndFile, "\")
Loop Until intSlashLoc = 0
'Trim off the database name, leaving the path
'and append the name of the image file
strImagePath = Left(strDBPathAndFile, intLastSlashLoc) & mPictureDirectoryName & "\"
strImagePathAndFile = strImagePath & mPictureNameControl & ".BMP"
If objFileSystem.FileExists(strImagePathAndFile) Then
mImageControl.Picture = strImagePathAndFile
mImagePath = strImagePathAndFile
Else
'If .gif exists Set ImageFrame to the path of the image file
strImagePathAndFile = strImagePath & mPictureNameControl & ".GIF"
If objFileSystem.FileExists(strImagePathAndFile) Then
mImageControl.Picture = strImagePathAndFile
mImagePath = strImagePathAndFile
Else
'If BMP exists Set ImageFrame to the path of the image file
strImagePathAndFile = strImagePath & mPictureNameControl & ".JPG"
If objFileSystem.FileExists(strImagePathAndFile) Then
mImageControl.Picture = strImagePathAndFile
mImagePath = strImagePathAndFile
Else
mImageControl.Picture = ""
End If
End If
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
Public Property Get PictureDirectoryName() As String
PictureDirectoryName = mPictureDirectoryName
End Property

Public Property Let PictureDirectoryName(ByVal theDirectoryName As String)
mPictureDirectoryName = theDirectoryName
End Property

Public Property Get LinkedTableName() As String
LinkedTableName = mLinkedTableName
End Property

Public Property Let LinkedTableName(ByVal theLinkedTableName As String)
mLinkedTableName = theLinkedTableName
End Property

Public Property Get IsLinked() As Boolean
IsLinked = mBlnLinked
End Property
Public Property Let IsLinked(ByVal blnIsLinked As Boolean)
mBlnLinked = blnIsLinked
End Property


Then I instantiate a a "PictureFromFile" object and pass it some controls:

Private objPictureFromFileEdit As PictureFromFile

Private Sub Form_Open(Cancel As Integer)
Set objPictureFromFileEdit = New PictureFromFile
Set objPictureFromFileEdit.PictureForm = Me.Form
Set objPictureFromFileEdit.PictureControl = Me.imgCntlEquipment
Set objPictureFromFileEdit.PictureNameControl = Me.autoIDSystem
objPictureFromFileEdit.PictureDirectoryName = "MERSPictures"
End Sub


I save all of the images for my piecs of equipment using the system ID (autoIDSystem).
 
To add to my last. The beauty of using this class file is reusability. In the future all you have to do is add another image control to a form or report. Then instantiate a PictureFromFile object and set the Form object, the Image Control object, a text box with the ID in it, and a folder name for the pictures. All of the events are contained in the class file.
Also my class could be shortened if you pass it the absolute path.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top