I using following codes to add images in my project & its working properly in winxp but not working properly in win7 platform. please help me to find the problem. If you any other better codes to add images to MS-Access databae then please suggest. thanks

Public Sub FillFields()
    If Not (adorsstudent.BOF And adorsstudent.EOF) Then
        Set Image1.DataSource = adorsstudent 
        Image1.DataField = "Picture" 
    End If
End Sub


Private Sub cmdAddImage_Click()
       Dim bytData() As Byte
    'Dim strDescription As String
    On Error GoTo err
    With dlImage 'common dialog box.
          'filtering so only jpg's and gifs are shown!
        .Filter = "Picture Files (*.jpg, *.gif)|*.jpg;*.gif"
        .DialogTitle = "Select Picture"  'sets the title of it.
        .ShowOpen  'show the open dialog box.
          'bit to "convert" the image to binary.
        Open .FileName For Binary As #1
        ReDim bytData(FileLen(.FileName))
    End With
    Get #1, , bytData
    Close #1
    With adorsstudent
         .Fields("Picture").AppendChunk bytData  'adding the picture to the db
    End With
    FillFields
    Exit Sub
err:
    If err.Number = 32755 Then     'simple error check.
   Else
        MsgBox err.Description
        err.Clear
    End If

End Sub

This is my code in adding picture to database:

Add this code to your module:

Public Function SavePictureToDB(tblName As String, _
                                WHERECondition As String, _
                                FldName As String, _
                                strFileNm As String, _
                                xcn As ADODB.Connection) As Boolean

   Dim FF         As Integer
   Dim rs_pic     As ADODB.Recordset

   Dim Fl         As Long
   Dim DataFile   As Long
   Dim Chunks     As Integer
   Dim ChunkSize  As Integer
   Dim Fragment   As Integer
   Dim Chunk()    As Byte

   ChunkSize = 16384

   FF = FreeFile()
   DataFile = FileLen(strFileNm)
   ReDim Chunk(DataFile)
   Open strFileNm For Binary Access Read As #FF
      Get #FF, , Chunk
      tmp_rc.Open "SELECT * FROM " & tblName & " " & WHERECondition, xcn
      rs_pic(FldName).AppendChunk (Chunk())
      rs_pic.Update
      rs_pic.Close
   Close #FF
   Set rs_pic = Nothing
   SavePictureToDB = True

End Function

To use it:

Call SavePictureToDB("Guardians", "WHERE ID = 1", "Picture", PicURL, cn)

"Guardians" is the Table name
"WHERE ID = 1" is the condition,
"Picture" is the OLE Object / Picture field in your database table
PicURL is the filename of the image you want to save in the database.
cn is your connection object (ADODB.CONNECTION)

Just play with the code above and customize it according in your needs,

This article has been dead for over six months. Start a new discussion instead.