Home
dbPix
Download
Order
Testimonials
Support
Tutorials
Samples
KnowledgeBase
Links
Revision History
Documentation
Search
Contact
Site Map

Graph: dbPix image storage vs OLE Embedding and Linking in Microsoft Access


Using external image files in Microsoft Access Forms and Reports

    
DBPix Image Control 
Free Evaluation
Fully functional


Download Now

For Windows XP through 10 & all Windows Server versions.

Buy Now!

dbPix Resources

Samples
Support
About dbPix


This technique is used by various DBPix samples. Visit the Support area for details of available samples.


In this example the image files will be stored in the same directory as the database mdb file.
The image files are named according to the database record's Id field (an autonumber), e.g. "1234.jpg"

The first task is to display the relevant image(s) in the form or report when the recordset data changes (ie when scrolling though records on a form or generating a report/preview).
This is achieved by handling the "Form_Current " event in Forms and the "Detail_Format " event in Reports. See the code below.

The second task is to save new or changed images to the correct location and with the right filename. In this example a "Load" button on the form is used to select images from the filesystem. If you use other means to load images (Camera, Clipboard etc) simply call "SaveImage" after loading a new image, like the code in "BtnLoad_Click".

DBPix can be configured to resample new images to constrain/normalize the dimensions, and you could also generate a thumbnail here.
We will also save the image dimensions in the recordset since this is useful if we are publishing the data/images on a web-server.


Form Code

Form_Current is the ' Current' event handler for the form istelf. It is called when the form controls need to be updated, such as when the recordset is scrolled or refreshed.
BtnLoad_Click is called when a button on the form is clicked to load an image from disk.
SaveImage is a helper that saves the image to the relevant directory, for example after loading, pasting, TWAIN acquire etc.
GetImagePath is a helper which returns the path to load/save images in. The code given here returns the path of the database mdb file itself, though it could be used to generate a relative path.

Private Sub Form_Current()
    ' Loads the control with the image specified in the 'Filename' field.
    ' The path is returned by the 'GetImagePath' function which uses the
    ' path of the database .mdb file
.
    Dim FullPath As String
    If IsNull(Me!Filename) Then
        DBPixCtl.ImageClear
    Else
        FullPath = GetImagePath & Me!Filename
        If Not (DBPixCtl.ImageLoadFile(FullPath) = True) Then
            DBPixCtl.ImageClear
        End If
    End If
End Sub


Private Sub BtnLoad_Click()
    ' Called when 'Load' button clicked to load an image
    ' into the current record.
    If DBPixCtl.ImageLoad Then
        ' Call our 'SaveImage' function to do the work.
        ' (Use it also for paste, twain operations etc).
        SaveImage
    End If
End Sub


Sub SaveImage()
    Dim ImageFilename As String

    If Me.NewRecord Then
        ' If this is a new record then change the 'Filename' field to force the
        ' Id (autonumber) field to be updated so we can use it's value.
        Me!Filename = ""
    End If
    ' Generate a filename using the 'Id' field, e.g. "29488.jpg".
    ImageFilename = Me!Id & ".jpg"

    ' Save the image
    If DBPixCtl.ImageSaveFile(GetImagePath & ImageFilename) Then
        ' Store the path in the Filename field
        Me!Filename = ImageFilename
        ' Store the dimensions - Useful for asp (Active Server Pages) web databases
        Me!ImgWidth = DBPixCtl.ImageWidth
        Me!ImgHeight = DBPixCtl.ImageHeight
    End If
End Sub


Private Function GetImagePath() As String
    ' Returns the path of the database mdb file
    Dim DBFullPath As String
    Dim I As Integer
    DBFullPath = CurrentDb().Name
    ' Strip the filename from the full path
    For I = 1 To Len(DBFullPath)
        If Mid(DBFullPath, I, 1) = "\" Then
            GetImagePath = Left(DBFullPath, I)
        End If
    Next
End Function



Report Code

Detail_Format is the 'Format' event handler for the 'Detail' section of the report. It is called when when the report section data is available and Access is about to format the section for printing or previewing.
GetImagePath is a helper which returns the path to load/save images in. The code given here returns the path of the database mdb file itself, though it could be used to generate a relative path.
Important Note:You may need to place an invisible edit control on the report section bound to the 'FileName' field in order to force that field in the recordset being updated.

Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
    Dim FullPath As String
    ' If the 'FileName' field is empty clear the control
    ' or we'll be left with the previous image.
    If IsNull(Me("FileName")) Then
        DBPixCtl.ImageClear
    Else
       ' Get a full path by combining the image path and .
       ' the filename from the recordset.
        FullPath = GetImagePath & Me("FileName")
       ' Try to load the image.   If error clear the control.
        If Not DBPixCtl.ImageLoadFile(FullPath) Then
            DBPixCtl.ImageClear
        End If
    End If
End Sub


Private Function GetImagePath() As String
    ' Returns the path of the database mdb file
    Dim DBFullPath As String
    Dim I As Integer
    DBFullPath = CurrentDb().Name
    ' Strip the filename from the full path
    For I = 1 To Len(DBFullPath)
        If Mid(DBFullPath, I, 1) = "\" Then
            GetImagePath = Left(DBFullPath, I)
        End If
    Next
End Function