### Access 97 Database Schema

Field Name | Type | Size
Image_id | AutoNumber |
A_Image | Ole Object | 2 gigabytes
Description | Text | 50
MyKey | Number | LongInteger

### Imaging using GetChunk and AppendChunk

    Option Explicit
    Const conChunkSize = 8192       '2048 increment  multiples  depending the image sizes

    Get the path and file name of .bmp, .ico, .gif, .jpg file
    Associate a Key Number to the file being stored in the database
    Load the image into the picture control for display
    Get the byte size of the image
    Calculate the number of chunksize increments exists
    Calculate the remaining nFragmentOffset smaller than one chunksize
    Load the Fragment Offset Byte array and Append it to the record field
    Load the remaining Byte Chunks into the record field
    Commit the record to the database

    Private Sub cmdAddImage_Click()

        Dim rsImage As Recordset
        Dim lOffset As Long
        Dim lSize As Long
        Dim sPath As String
        Dim nHandle As Integer
        Dim lSubChunks As Long
        Dim Chunk() As Byte
        Dim nFragmentOffset As Integer
        Dim i As Integer
        Dim lChunks As Long
        Dim lKey As Long
        Dim sSQL As String
        On Error GoTo cmdAddImage_Click_Error

        frmFileDialog.Show vbModal

        If gsFileName <> "" Then
            lKey = InputBox("Please enter a Number (1..n) as a key to the image")
            If lKey > 0 Then

                'check if unique Key
                sSQL = "select * from images"
                sSQL = sSQL & " where myKey=" & lKey

                Set rsImage = objDB.OpenRecordset(sSQL, dbOpenDynaset)
                If Not rsImage.EOF Then
                    MsgBox "Key is already in use"
                    GoTo Exit_cmdAddImage_Click
                End If

               'Load the image in the picture control
                Set Picture1.Picture = LoadPicture(gsFileName)

                Set rsImage = objDB.OpenRecordset("Images", dbOpenDynaset)

               'Calculate the Byte Size of the file
                nHandle = FreeFile
                Open gsFileName For Binary Access Read As nHandle

                lSize = LOF(nHandle)
                If nHandle = 0 Then
                    Close nHandle
                End If

               'Calculate the number of Chunksize Increments and remaining Fragment byte offset

                lChunks = lSize \ conChunkSize
                nFragmentOffset = lSize Mod conChunkSize

                rsImage.AddNew
                rsImage("Description") = gsFileName
                rsImage("MyKey") = lKey

              'Get Fragment Offset Byte Data and Append it to the image field

                ReDim Chunk(nFragmentOffset)
                Get nHandle, , Chunk()
                rsImage("A_Image").AppendChunk Chunk()
                ReDim Chunk(conChunkSize)
                lOffset = nFragmentOffset
              'Get ChunkSize Byte Data and Append it to the image field
                For i = 1 To lChunks
                    Get nHandle, , Chunk()
                    rsImage("A_Image").AppendChunk Chunk()
                    lOffset = lOffset + conChunkSize
                    txtByteCount = lOffset
                    DoEvents
                Next

                rsImage.Update
            End If
        End If

    Exit_cmdAddImage_Click:

       Exit Sub

    cmdAddImage_Click_Error:

        #If gnDebug Then
            Stop
            Resume
        #End If

        HandleError "cmdAddImage_Click", Err.Description, Err.Number, gErrFormName
        Resume Exit_cmdAddImage_Click

    End Sub

    Get the Key Number from the user interface and locate record
    Output file must be created to associate with the Picture control.
    Direct access to the recordset image fields is not available in
    this example.
    Calculate the Fragment Offset and ChunkSize Increments
    Display the image generated from the database

    Private Sub cmdLoadImage_Click()

        Dim sSQL As String
        Dim lKey As Long
        Dim rsImage As Recordset
        Dim lSize As Long
        Dim varChunk() As Byte
        Dim lOffset As Long
        Dim sPath As String
        Dim nHandle As Integer
        Dim iChunks As Integer
        Dim nFragmentOffset As Integer
        Dim i As Integer
        Dim sFile As String
        On Error GoTo cmdLoadImage_Click_Error

       'Get the Key Number for the Image from the user

        lKey = InputBox("Please input a Key")

        If lKey > 0 Then
            sSQL = "select * from images"
            sSQL = sSQL & " where myKey=" & lKey

            Set rsImage = objDB.OpenRecordset(sSQL, dbOpenDynaset)

            Screen.MousePointer = vbHourglass

            If Not rsImage.EOF Then
                nHandle = FreeFile
                sPath = App.Path
                sFile = sPath & "\output.bin"

                Open sFile For Binary Access Write As nHandle

               lSize = rsImage("a_image").FieldSize

              'Calculate the Fragment Offset and ChunkSize Increments

               iChunks = lSize \ conChunkSize
               nFragmentOffset = lSize Mod conChunkSize

               ReDim Buffer(nFragmentOffset) As Byte
               varChunk() = rsImage("a_image").GetChunk(lOffset, nFragmentOffset)
               Put nHandle, , varChunk()
               lOffset = nFragmentOffset
               For i = 1 To iChunks
                    ReDim varChunk(conChunkSize) As Byte
                    varChunk() = rsImage("a_image").GetChunk(lOffset, conChunkSize)
                    Put nHandle, , varChunk()
                    lOffset = lOffset + conChunkSize
                    txtByteCount = lOffset
                    DoEvents
               Next

            End If

            Close nHandle

            'Display the image generated from the database
            Set Picture1.Picture = LoadPicture(sFile)
        End If

    Exit_cmdLoadImage_Click:

        Screen.MousePointer = vbDefault

       Exit Sub

    cmdLoadImage_Click_Error:

        #If gnDebug Then
            Stop
            Resume
        #End If

        HandleError "cmdLoadImage_Click", Err.Description, Err.Number, gErrFormName
        Resume Exit_cmdLoadImage_Click

    End Sub

    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)

        If Not objDB Is Nothing Then
            objDB.Close
        End If
        Set objDB = Nothing

    End Sub