943,562 Members | Top Members by Rank

Ad:
Mar 19th, 2009
0

Retrieve Image from Access DB to ImageControl in Form

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

Re: Retrieve Image from Access DB to ImageControl in Form

try the following sample code

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

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: Limiting Cursor VB6
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Use VB6 to read email in outlook





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


Follow us on Twitter


© 2011 DaniWeb® LLC