Retrieve Image from Access DB to ImageControl in Form

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

Retrieve Image from Access DB to ImageControl in Form

 
0
  #1
Mar 19th, 2009
hi everyone, i have successfully saved the image file to DB 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") = picsm.Read
.Update
MsgBox "Successful"
End With
picsm.Close
Set picsm = Nothing
End Sub

But anyone can help me retrieve this image from DB and load it into the IMAGE CONTROL on the form?

i have this code:

Private Sub cmdSearch_Click()
Dim photo As String
squery = "select * from tblpic where Last='" & txtLast.Text & "'"
photo = rs.Fields("pic")

Set cm = New ADODB.Command
With cm
.ActiveConnection = conn
.CommandText = squery
.CommandType = adCmdText
End With
Set rs = New ADODB.Recordset
With rs
.CursorLocation = adUseClient
.CursorType = adOpenStatic
.LockType = adLockPessimistic
.Open cm
End With
With Me
.txtLast = rs!Last
.txtFirst = rs!First
.imgPic.Picture = LoadPicture(App.Path & "\pic\" & photo)
End With
End Sub

here is the error:

"file/path access error."

how could i retrieve the picture in the Access DB,
i think a folder must be reserved for the location of the photo.

thanks for the response.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 2,134
Reputation: debasisdas will become famous soon enough debasisdas will become famous soon enough 
Solved Threads: 132
debasisdas's Avatar
debasisdas debasisdas is offline Offline
Postaholic

Re: Retrieve Image from Access DB to ImageControl in Form

 
0
  #2
Mar 20th, 2009
try the following sample code

  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  
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



Tag cloud for Visual Basic 4 / 5 / 6
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC