I took some old code that used the AppendChunck, and GetChunk of the ADO recordset object to get and store an image from the database to a file. I converted it to use a byte array. So now it reads and stores the image to and from a byte array. So when you extract the image data to a byte array you can store it in your database and of course extracting the bytes from the database and loading them into you image control. I have NOT tested this code, so you may run into errors. If so let me know and I will help you debug this. Also if you don't know how to get and put the image into and out of a byte array, let me know, I know I have some code arround that does this. One more thought is that if you have any trouble passing the byte array as a parameter to the SetDbPhotoBytes function, try changing it to a variant instead of a byte array. ,,,bytes() As Byte becomes ,,,bytes As Variant. It should work either way but I have run into problems with passing arrays as parameters and whenever I do if I change it a type variant the the problem goes away.
The GetDbPhotoBytes returns the byte array with the image data in it.
The SetDbPhotoBytes returns the number of bytes stored, so you can test it to see if anything got stored. >0 means data got stored.
Code:
Public Function GetDbPhotoBytes(rstMain As Recordset, FieldName As String) As Variant
On Error GoTo Er
Dim PicSize As Long
Dim bytes() As Byte
'PicSize = rstMain(FieldName).DefinedSize 'this might work better, not sure ?
PicSize = rstMain(FieldName).ActualSize
ReDim bytes(PicSize)
bytes() = rstMain(FieldName).GetChunk(PicSize)
GetDbPhotoBytes = bytes()
Ex:
Erase bytes
Exit Function
Er:
MsgBox Err.Description
Resume Ex
End Function
Public Function SetDbPhoto(rstMain As Recordset, FieldName As String, bytes() As Byte) As Long
On Error GoTo Er
Dim PicSize As Long
'if bytes is defined as (1 To 10): 10 -1 + 1 = 10 or (0 To 9) 9 - 0 + 1 = 10 ...
PicSize = UBound(bytes) - LBound(bytes) + 1
If PicSize > 0 Then
rstMain(FieldName).AppendChunk bytes()
End If
Ex:
SetDbPhoto = PicSize
Exit Function
Er:
MsgBox Err.Description
Resume Ex
End Function
Bookmarks