943,706 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Mar 16th, 2009
0

VB Add/Edit/Save Pictures in Access DB

Expand Post »
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.
Reputation Points: 15
Solved Threads: 4
Junior Poster in Training
arvin2006 is offline Offline
69 posts
since Sep 2006
Mar 17th, 2009
0

Re: VB Add/Edit/Save Pictures in Access DB

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

vb Syntax (Toggle Plain Text)
  1. Dim CN As New ADODB.Connection
  2. Dim RS As ADODB.Recordset
  3. Dim DataFile As Integer, Fl As Long, Chunks As Integer
  4. Dim Fragment As Integer, Chunk() As Byte, i As Integer, FileName As String
  5.  
  6. Private Const ChunkSize As Integer = 16384
  7. Private Const conChunkSize = 100
  8.  
  9. Private Sub cmdSave_Click()
  10. CN.Open "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=Pubs;Data Source=Test"
  11. Dim strSQL As String
  12.  
  13. strSQL = "SELECT * FROM pub_info where pub_id = '9999'"
  14. RS.Open strSQL, CN, adOpenForwardOnly, adLockOptimistic
  15.  
  16. RS.AddNew
  17. SavePicture
  18. RS.Update
  19.  
  20. Set RS = Nothing
  21. Set RS = New Recordset
  22. End Sub
  23.  
  24. Private Sub SavePicture()
  25. Dim strFileNm As String
  26. DataFile = 1
  27. Open strFileNm For Binary Access Read As DataFile
  28. Fl = LOF(DataFile) ' Length of data in file
  29. If Fl = 0 Then Close DataFile: Exit Sub
  30. Chunks = Fl \ ChunkSize
  31. Fragment = Fl Mod ChunkSize
  32. ReDim Chunk(Fragment)
  33. Get DataFile, , Chunk()
  34. RS!logo.AppendChunk Chunk()
  35. ReDim Chunk(ChunkSize)
  36. For i = 1 To Chunks
  37. Get DataFile, , Chunk()
  38. RS!logo.AppendChunk Chunk()
  39. Next i
  40. Close DataFile
  41. End Sub
Featured Poster
Reputation Points: 665
Solved Threads: 427
Posting Genius
debasisdas is offline Offline
6,406 posts
since Feb 2007
Mar 17th, 2009
0

Re: VB Add/Edit/Save Pictures in Access DB

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

vb Syntax (Toggle Plain Text)
  1. Dim CN As New ADODB.Connection
  2. Dim RS As ADODB.Recordset
  3. Dim DataFile As Integer, Fl As Long, Chunks As Integer
  4. Dim Fragment As Integer, Chunk() As Byte, i As Integer, FileName As String
  5.  
  6. Private Const ChunkSize As Integer = 16384
  7. Private Const conChunkSize = 100
  8.  
  9. Private Sub Form_Load()
  10. CN.Open "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=Pubs;Data Source=Test"
  11. Dim strsql As String
  12.  
  13. strsql = "SELECT * FROM pub_info where pub_id = '9999'"
  14. RS.Open strsql, CN, adOpenForwardOnly, adLockReadOnly
  15. ShowPic
  16. Set RS = Nothing
  17. Set RS = New Recordset
  18. End Sub
  19.  
  20. Private Sub ShowPic()
  21. DataFile = 1
  22. Open "pictemp" For Binary Access Write As DataFile
  23. Fl = RS!logo.ActualSize ' Length of data in file
  24. If Fl = 0 Then Close DataFile: Exit Sub
  25. Chunks = Fl \ ChunkSize
  26. Fragment = Fl Mod ChunkSize
  27. ReDim Chunk(Fragment)
  28. Chunk() = RS!logo.GetChunk(Fragment)
  29. Put DataFile, , Chunk()
  30. For i = 1 To Chunks
  31. ReDim Buffer(ChunkSize)
  32. Chunk() = RS!logo.GetChunk(ChunkSize)
  33. Put DataFile, , Chunk()
  34. Next i
  35. Close DataFile
  36. FileName = "pictemp"
  37. Picture1.Picture = LoadPicture(FileName)
  38. End Sub
Featured Poster
Reputation Points: 665
Solved Threads: 427
Posting Genius
debasisdas is offline Offline
6,406 posts
since Feb 2007
Mar 18th, 2009
0

Re: VB Add/Edit/Save Pictures in Access DB

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.
Reputation Points: 15
Solved Threads: 4
Junior Poster in Training
arvin2006 is offline Offline
69 posts
since Sep 2006
Mar 19th, 2009
0

Re: VB Add/Edit/Save Pictures in Access DB

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.
Featured Poster
Reputation Points: 665
Solved Threads: 427
Posting Genius
debasisdas is offline Offline
6,406 posts
since Feb 2007
Mar 19th, 2009
0

Re: VB Add/Edit/Save Pictures in Access DB

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...
Reputation Points: 15
Solved Threads: 4
Junior Poster in Training
arvin2006 is offline Offline
69 posts
since Sep 2006
Feb 24th, 2010
-1
Re: VB Add/Edit/Save Pictures in Access DB
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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
onlyonebui is offline Offline
2 posts
since Feb 2010
Feb 24th, 2010
0
Re: VB Add/Edit/Save Pictures in Access DB
Click to Expand / Collapse  Quote originally posted by arvin2006 ...
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...
Reputation Points: 15
Solved Threads: 4
Junior Poster in Training
arvin2006 is offline Offline
69 posts
since Sep 2006
Oct 4th, 2010
0
Re: VB Add/Edit/Save Pictures in Access DB
hi, just try to create application for my kids, but facing problem with image. could you please share the code. TQ in advance.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
amrahim is offline Offline
6 posts
since Oct 2010
Oct 4th, 2010
0
Re: VB Add/Edit/Save Pictures in Access DB
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/newthr...=newthread&f=4
Reputation Points: 329
Solved Threads: 347
Senior Poster
AndreRet is offline Offline
3,700 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Atteching an exe to my brownser mozila fire fox
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: how can i make a software for mobiles in vb





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC