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


DBPix Sample Source Code: External_Main
Back to sample
Option Compare Database
Option Explicit

Private Sub Form_Current()
    ' Display the main and thumbnail images corresponding to the current record
    Dim Path As String
    Dim FileName As String

    Path = GetImgFolder                             ' See implementation in the 'PathFuncs' module

    DBPixThumb.ImageViewBlob (Null)                 ' Clear the Thumbnail DBPix control
    DBPixMain.ImageViewBlob (Null)                  ' Clear the Main DBPix control

    FileName = Me!ItemId & ".jpg"                   ' Generate the filename using the record's Id field value
    If Dir(Path & FileName) <> vbNullString Then    ' Check that the file exists
        DBPixMain.ImageViewFile (Path & FileName)   ' If it exists, display the image
    End If

    FileName = "t" & Me!ItemId & ".jpg"             ' Thumbnail filename prefixed with 't'
    If Dir(Path & FileName) <> vbNullString Then    ' Check that the file exists
        DBPixThumb.ImageViewFile (Path & FileName)  ' If it exists, display the image
    End If

End Sub

' The main image has been modified.  Update the thumbnail and save the main and thumbnail images
Private Sub DBPixMain_ImageModified()

    DBPixThumb.ImageViewBlob (Null)                 ' Clear the Thumbnail DBPix control
    
    Me("ThumbWidth") = 0                            ' Reset image info (and force update of Id if new record)
    Me("ThumbHeight") = 0
    Me("DetailWidth") = 0
    Me("DetailHeight") = 0

    If DBPixMain.ImageBytes > 0 Then
       DBPixThumb.ImageLoadBlob (DBPixMain.Image)  ' Copy/Paste is faster, if you don't mind overwriting the clipboard
'        DBPixMain.ImageCopy
'        DBPixThumb.ImagePaste

        If DBPixMain.ImageSaveFile(GetImgFolder & Me!ItemId & ".jpg") Then
            Me("DetailWidth") = DBPixMain.ImageWidth        ' Uppdate Detail image info
            Me("DetailHeight") = DBPixMain.ImageHeight

            If DBPixThumb.ImageSaveFile(GetImgFolder & "t" & Me!ItemId & ".jpg") Then
                Me("ThumbWidth") = DBPixThumb.ImageWidth    ' Update the thumbnail image info
                Me("ThumbHeight") = DBPixThumb.ImageHeight
            End If
        End If
    End If
End Sub


' Batch-Load button clicked: Load an entire folder of images (into new records)
Private Sub btnBatchLoad_Click()
    On Error GoTo Finish

    Dim strFullPath As String
    Dim strFolderName As String
    Dim I As Integer

    ' Display a 'Browse for folder' dialog - see 'BrowseForFolder' module
    strFolderName = BrowseFolder("Select folder to load images from")
    
    If Not IsEmpty(strFolderName) And Not strFolderName = "" Then
        Dim FileList As New Collection ' List of files in folder (to prevent potential recursive calls to Dir)
        Dim strFile As String
    
        strFile = Dir(strFolderName + "\" + "*.jpg", vbNormal)
        Do While strFile <> ""
            FileList.Add strFile
            strFile = Dir
        Loop
    
        For I = 1 To FileList.Count ' Try to load each file - DBPixMain_ImageModified does the work of updating
            strFile = FileList(I)
            If Len(strFile) > 1 Then
                strFullPath = strFolderName + "\" + strFile
                DoCmd.GoToRecord , , acNewRec
                DBPixMain.ImageLoadFile (strFullPath)
            End If
        Next I
    End If

Finish:

End Sub

Back to sample