Putting Pictures
in a
Database

by Rick Meyer

1. Start a new standard exe.
2. On Form1 put a Data Control named Data1.
3. On Form1 put a Commandbutton named Command1.
4. On Form1 put a Textbox named Text1.
    (Set the Text1 DataSource property to Data1)
5. On Form1 put an Imagebox named Image1.
    (Set the Image1 DataSource property to Data1)
6. In Project->Components add Microsoft CommonDialog Control
7. On Form1 put a CommonDialog named CommonDialog1.


      Form1 should look like the picture above.
Now you are ready for the code. Select all of the following code by clicking on the word Option three times and copy it to the clipboard [Ctrl][Insert]. Then paste it into the code window of Form1 with [Shift][Insert].
When you run the program, you will be able to put a caption in the textbox for the pictures you choose. Then to see all the pictures you have entered, click on the Data1 Control.
Option Explicit
Const FLTR = "Pictures|*.jpg*;*.gif*;*.bmp;*.ico|All Files|*.*"
     
Private Sub Form_Load()
    Dim dbsNew As Database
    Dim tdfNew As TableDef
    Dim dbName As String
    dbName = "c:\NewDB.mdb"
    
    If Dir(dbName) = "" Then
        Set dbsNew = CreateDatabase(dbName, dbLangGeneral)
        Set tdfNew = dbsNew.CreateTableDef("Pictures")

        With tdfNew
            .Fields.Append .CreateField("Name", dbText)
            .Fields.Append .CreateField("Picture", dbLongBinary)
        End With
    
        With dbsNew
            .TableDefs.Append tdfNew
            .Close
        End With
    End If
    
    Data1.DatabaseName = dbName
    Data1.RecordSource = "Pictures"
    Text1.DataField = "Name"
    Command1.Caption = "Add Picture"
    Image1.BorderStyle = 1
    Image1.DataField = "Picture"
End Sub


Private Sub Command1_Click()
    Dim f%
    Dim fSize&
    Dim Chunk() As Byte
    Dim picFile As String
    
    CommonDialog1.CancelError = True
    On Error GoTo er1

    CommonDialog1.Filter = FLTR
    CommonDialog1.ShowOpen
    
    f = FreeFile
    picFile = CommonDialog1.filename
    Open picFile For Binary Access Read As f

    fSize = LOF(f)
    ReDim Chunk(fSize)
    Get f, , Chunk()
    Close f
    
    With Data1.Recordset
        .AddNew
        !Picture.AppendChunk Chunk()
        .Update
        .Bookmark = .LastModified
    End With
    
    GoTo er2
    
er1: Resume er2
er2: On Error GoTo 0
End Sub
As you see placing pictures in a database is a matter of transferring binary data from the picture file to a LongBinary database field. You can then take advantage of the Picture and Image databound properties (DataSource and DataField).

The pictures displayed using the OLE control in Demo1 only works for BMP files and the output from an OLE Control is rather grainy.

It is possible to use a text field in your database to store the name of the picture file to load in the picture or image control. Unfortunately, this means that you also have a bunch of picture files to transfer with your database. Picture and image controls are set with the LoadPicture() function and the only parameter allowed is a filename.