VB Add/Edit/Save Pictures in Access DB

Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2006
Posts: 65
Reputation: arvin2006 is an unknown quantity at this point 
Solved Threads: 4
arvin2006's Avatar
arvin2006 arvin2006 is offline Offline
Junior Poster in Training

VB Add/Edit/Save Pictures in Access DB

 
0
  #1
Mar 16th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 2,120
Reputation: debasisdas will become famous soon enough debasisdas will become famous soon enough 
Solved Threads: 129
debasisdas's Avatar
debasisdas debasisdas is offline Offline
Postaholic

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

 
0
  #2
Mar 17th, 2009
please try to use the sample code to store picture.
this sample code uses ADO for database connection

  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
Share your Knowledge.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 2,120
Reputation: debasisdas will become famous soon enough debasisdas will become famous soon enough 
Solved Threads: 129
debasisdas's Avatar
debasisdas debasisdas is offline Offline
Postaholic

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

 
0
  #3
Mar 17th, 2009
please try to use the sample code to retrieve picture from database tables.
again this sample code uses ADO for database connection.

  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
Share your Knowledge.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 65
Reputation: arvin2006 is an unknown quantity at this point 
Solved Threads: 4
arvin2006's Avatar
arvin2006 arvin2006 is offline Offline
Junior Poster in Training

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

 
0
  #4
Mar 18th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 2,120
Reputation: debasisdas will become famous soon enough debasisdas will become famous soon enough 
Solved Threads: 129
debasisdas's Avatar
debasisdas debasisdas is offline Offline
Postaholic

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

 
0
  #5
Mar 19th, 2009
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.
Share your Knowledge.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 65
Reputation: arvin2006 is an unknown quantity at this point 
Solved Threads: 4
arvin2006's Avatar
arvin2006 arvin2006 is offline Offline
Junior Poster in Training

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

 
0
  #6
Mar 19th, 2009
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...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Visual Basic 4 / 5 / 6 Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC