Hi everyone, is it possible to add/edit/save picture in MS Access DB using ADO, I have this code

If (MsgBox("Are you sure you entered correct data?" & vbNewLine & "Save now?", vbYesNo + vbQuestion, "Confirm Save") = vbYes) Then
squery = "": squery = "SELECT * FROM tblEmp"
Call ExecuteCommand
Set rs = New ADODB.Recordset
With rs
.Open squery, conn, adOpenStatic, adLockPessimistic
.AddNew
.Fields("EmpID") = txtEmpID.Text
.Fields("LName") = txtLast.Text
.Fields("FName") = txtFirst.Text
.Fields("MName") = txtMiddle.Text
.Fields("Pos") = txtPos.Text
.Update
End With

I want also to add/save picture together with this data, using Image or PictureBox.

Then I want to retrieve it later on and display the picture within the Form based on the user's QUERY. then is it also possible to display this picture in Data Report when the user click the command PREVIEW.

Thank you very much for your help.
I will highly appreciate it.

Recommended Answers

All 12 Replies

please try to use the sample code to store picture.
this sample code uses ADO for database connection

Dim CN As New ADODB.Connection
Dim RS As ADODB.Recordset
Dim DataFile As Integer, Fl As Long, Chunks As Integer
Dim Fragment As Integer, Chunk() As Byte, i As Integer, FileName As String

Private Const ChunkSize As Integer = 16384
Private Const conChunkSize = 100

Private Sub cmdSave_Click()
    CN.Open "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=Pubs;Data Source=Test"
    Dim strSQL As String

    strSQL = "SELECT * FROM pub_info where pub_id = '9999'"
    RS.Open strSQL, CN, adOpenForwardOnly, adLockOptimistic

    RS.AddNew
      SavePicture
    RS.Update

    Set RS = Nothing
    Set RS = New Recordset
End Sub

Private Sub SavePicture()
    Dim strFileNm As String
    DataFile = 1
    Open strFileNm For Binary Access Read As DataFile
        Fl = LOF(DataFile)   ' Length of data in file
        If Fl = 0 Then Close DataFile: Exit Sub
        Chunks = Fl \ ChunkSize
        Fragment = Fl Mod ChunkSize
        ReDim Chunk(Fragment)
        Get DataFile, , Chunk()
        RS!logo.AppendChunk Chunk()
        ReDim Chunk(ChunkSize)
        For i = 1 To Chunks
            Get DataFile, , Chunk()
            RS!logo.AppendChunk Chunk()
        Next i
    Close DataFile
End Sub

please try to use the sample code to retrieve picture from database tables.
again this sample code uses ADO for database connection.

Dim CN As New ADODB.Connection
Dim RS As ADODB.Recordset
Dim DataFile As Integer, Fl As Long, Chunks As Integer
Dim Fragment As Integer, Chunk() As Byte, i As Integer, FileName As String

Private Const ChunkSize As Integer = 16384
Private Const conChunkSize = 100

Private Sub Form_Load()
    CN.Open "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=Pubs;Data Source=Test"
    Dim strsql As String

    strsql = "SELECT * FROM pub_info where pub_id = '9999'"
    RS.Open strsql, CN, adOpenForwardOnly, adLockReadOnly
      ShowPic
    Set RS = Nothing
    Set RS = New Recordset
End Sub

Private Sub ShowPic()
    DataFile = 1
    Open "pictemp" For Binary Access Write As DataFile
        Fl = RS!logo.ActualSize ' Length of data in file
        If Fl = 0 Then Close DataFile: Exit Sub
        Chunks = Fl \ ChunkSize
        Fragment = Fl Mod ChunkSize
        ReDim Chunk(Fragment)
        Chunk() = RS!logo.GetChunk(Fragment)
        Put DataFile, , Chunk()
        For i = 1 To Chunks
            ReDim Buffer(ChunkSize)
            Chunk() = RS!logo.GetChunk(ChunkSize)
            Put DataFile, , Chunk()
        Next i
    Close DataFile
    FileName = "pictemp"
    Picture1.Picture = LoadPicture(FileName)
End Sub

good evening:

how could this code be fitted to the above code (to what i have posted)..

for example, in my form i have imgPhoto in the form, having UPLOAD Picture button, then after locating the photo by this code:

Private Sub cmdUpload_Click()
cdPhoto.Filter = "Image Files (*.jpg;*.gif;*.bmp)|*.jpg;*.gif;*.bmp"
cdPhoto.ShowOpen
filepath = cdPhoto.FileName
imgPhoto.Picture = LoadPicture(filepath)
End Sub

by means of this code, i have successfully loaded the picture within the image on the form but for the SAVE BUTTON?

for database access:
i have field Photo defined as "OLE Object" in MS Access Datafield

what should be on the cmdSave button:

together with this,

Set rs = New ADODB.Recordset
With rs
.Open squery, conn, adOpenStatic, adLockPessimistic
.AddNew
.Fields("EmpID") = txtEmpID.Text
.Fields("LName") = txtLast.Text
.Fields("FName") = txtFirst.Text
.Fields("MName") = txtMiddle.Text
.Fields("Photo")=????
.Update

what should be added with this code or anything much simpler than the one you have given?

thank you very much for the reply.

You need to do the following .

1.add all the records except picture
2.update the recordset
3.edit the same record
4.update the picture field with the photo.
5.update the recordset again.

good morning, i have successfully saved the picture together with the data by this code:

Private Sub cmdSave_Click()
Dim picsm As ADODB.Stream
Set picsm = New ADODB.Stream
picsm.Type = adTypeBinary
picsm.Open
picsm.LoadFromFile filepath
With rs
.AddNew
.Fields("Last") = txtLast.Text
.Fields("First") = txtFirst
.Fields("Pic").Value = picsm.Read
imgPic.Picture = LoadPicture(filepath)
.Update
MsgBox "success"
End With
picsm.Close
Set picsm = Nothing
End Sub

but my problem now is on how to retrieve the picture and load it into the image control on the form:

i have this code but it is working with data only, it retrieves the data but not the image.

Private Sub cmdSearch_Click()
On Error GoTo notfound
Dim photo As String
squery = "select * from tblpic where Last='" & txtLast.Text & "'"
Call ExecuteCommand
photo = rs.Fields("pic").Value
With Me
.txtLast = rs!Last
.txtFirst = rs!First
.imgPic.Picture = LoadPicture(photo)
End With
Exit Sub
notfound:
MsgBox "Not Found"

End Sub


any idea?

thank you...

did u manage to do it? arvin2006.
im facing the same problm...
plz let me knw if u hv any idea to do it...
thanx!

good morning, i have successfully saved the picture together with the data by this code:

Private Sub cmdSave_Click()
Dim picsm As ADODB.Stream
Set picsm = New ADODB.Stream
picsm.Type = adTypeBinary
picsm.Open
picsm.LoadFromFile filepath
With rs
.AddNew
.Fields("Last") = txtLast.Text
.Fields("First") = txtFirst
.Fields("Pic").Value = picsm.Read
imgPic.Picture = LoadPicture(filepath)
.Update
MsgBox "success"
End With
picsm.Close
Set picsm = Nothing
End Sub

but my problem now is on how to retrieve the picture and load it into the image control on the form:

i have this code but it is working with data only, it retrieves the data but not the image.

Private Sub cmdSearch_Click()
On Error GoTo notfound
Dim photo As String
squery = "select * from tblpic where Last='" & txtLast.Text & "'"
Call ExecuteCommand
photo = rs.Fields("pic").Value
With Me
.txtLast = rs!Last
.txtFirst = rs!First
.imgPic.Picture = LoadPicture(photo)
End With
Exit Sub
notfound:
MsgBox "Not Found"

End Sub


any idea?

thank you...

wow, it was a year ago.. anyway, i was able to manage it..

give me your email and i'll show.. i will find my code...

hi, just try to create application for my kids, but facing problem with image. could you please share the code. TQ in advance.

Amrahim, I see you are a new poster, so welcome. You will have to open a new post though, this is quite old. I'll post all the code needed for save, edit, open and show a picture in vb6/access.

How to open a new thread? Go to the top of the page, hover your mouse over 'Software' (Located between >>>Editorial, Hardware & Software, SOFTWARE, Web Development etc<<<.

Click on VB4/5/6. Once the page opens, at the top as well, it gives you the option to open a new thread. I have for now included the link below. Just click on it and post your question.

Thank you so much.

http://www.daniweb.com/forums/newthread.php?do=newthread&f=4

hi, where you have posted all the code needed for save, edit, open and show a picture in vb6/access. if you have this code, please help me with the link
????????????????????????????

Amrahim, I see you are a new poster, so welcome. You will have to open a new post though, this is quite old. I'll post all the code needed for save, edit, open and show a picture in vb6/access.

How to open a new thread? Go to the top of the page, hover your mouse over 'Software' (Located between >>>Editorial, Hardware & Software, SOFTWARE, Web Development etc<<<.

Click on VB4/5/6. Once the page opens, at the top as well, it gives you the option to open a new thread. I have for now included the link below. Just click on it and post your question.

Thank you so much.

http://www.daniweb.com/forums/newthread.php?do=newthread&f=4

I believe the code he is referring to can be found in this post.

If you have further questions, please start a new thread of your own and post your current code and specific questions.

Please see may sample:

<

'byte Variables for storing images
Public pBag As PropertyBag
Public pByteA() As Byte



Private Sub cmdbrowse_Click()
 Dim fileName As String
    
    On Error Resume Next
     browsedialog.Filter = "Image Files (*.jpg;*.gif;*.bmp)|*.jpg;*.gif;*.bmp"
    browsedialog.ShowOpen  'common dialog
    
    fileName = browsedialog.fileName
    Image2.Picture = LoadPicture(fileName)
End Sub



Private Sub cmdsave_Click()

If Image2.Picture = 0 Then

MsgBox "Profile Picture is emplty!", vbCritical, "error"

Me.cmdbrowse.SetFocus

Exit Sub
End If
 

  'Create propertybag
    Set pBag = New PropertyBag
     'Write object
    pBag.WriteProperty "MyPicture", Image2.Picture
     'Fill array with binary data of pic
    pByteA = pBag.Contents
     


 
         Dim rs As New ADODB.Recordset

        If rs.State = 1 Then rs.Close
        rs.Open "Select * from yourtbl", cn, 3, 2
        rs.AddNew
        'sample data
        rs!ID_Number=me.txtID.text
        rs!Magstripe_Serial = Me.TxtDummy.Text
        .....
        ..... so on...
       'Save Image
        rs!Student_Pic = pByteA             
        rs.Update
        MsgBox "Successfully Save!", vbInformation, ""
        
       

End Sub




Private Sub Retreive_Picture()



        Dim rs As New ADODB.Recordset

       
        rs.Open "SELECT * FROM yourtbl WHERE ID_Number = '" & me.txtSearch.text & "'", cn, 3, 2

       txtID.Text = rs!ID_Number
    
       TxtDummy.Text = rs!Magstripe_Serial
        ....
       ....
       ..... sample fields 

       'then retreive the picture form database
  
       On Error Resume Next
       rs!ID_Number=me.txtID.text

       pByteA = rs!Student_Pic

       Set pBag = New PropertyBag
       pBag.Contents = pByteA
       Set .Image2 = pBag.ReadProperty("MyPicture")
       
       If Dir(App.Path & "\myPic", vbDirectory) = "" Then MkDir (App.Path & "\myPic")
   
        SavePicture .Image2, App.Path & "\myPic\temp.bmp"
        
      
End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.