I've tested this with a table called tblImage that has 2 columns. First column is ID (type: autonumber) and the second one is IMGDATA (type: OLE Data).
On Form1, I have 2 buttons, Button1 and Button2 to IMPORT from IMAGE file and EXPORT from DB respectively. I also have an ADO DATA CONTROL (Adodc1) that is pointing to the MDB and the .RECORDSOURCE set to tblImage.
Code:
' saves from IMG file into DB
Private Sub Button1_Click()
' get file name
dim szFile as string
szFile = InputBox("Enter image path")
' open file and read data
dim imgByte() as Byte
open szFile for Binary access read for #1
redim imgByte(LOF(1))
dim nPos as long
nPos = 0
while (not eof(1))
get #1, nPos+1, imgByte(nPos)
nPos = nPos + 1
wend
' save into db
dim adoRS as ADODB.RecordSet
set adoRS = Adodc1.RecordSet
With adoRS
.AddNew
.Fields(1).Value = imgByte
end with
' close everything
close #1
redim imgByte(0)
set adoRS = Nothing
End Sub
' Saves from DB into file
Private Sub Button2_Click()
' get file name
dim szFile as string
szFile = InputBox("Enter new image path")
' get current record
dim adoRS as ADODB.RecordSet
set adoRS = Adodc1.RecordSet
' get data
dim imgByte() as Byte
redim imgByte(adors.Fields(1).actualsize)
imgByte = adoRs.Fields(1).value
' write data
open szfile for binary access write as #1
put #1, , imgByte
close #1
set adoRS = nothing
End Sub
Good Luck,
-Cool Bizs
[Cimperiali colorized for better reading]
Last edited by Cimperiali; December 7th, 2002 at 05:39 AM.
I tryed to do all this using simply Database and recordset. Everythig was perfectly except results ) Files that appears on my disk are not files that I have put into OLE field. It include ole object as I understand.
Code:
Dim byProg() As Byte
byProg = rsSystem.Fields(3).Value
Dim nFreeFiles As Integer
nFreeFiles = FreeFile
Dim sFileName As String
sFileName = sFolder & "\" & sDir & "\" & sName
Open sFileName For Binary Access Write As #nFreeFiles
Put #nFreeFiles, , byProg
Close #nFreeFiles
Just assign the BYTE array to the recordset value. For example,
Code:
Sub SaveBinary(adoRS as RecordSet)
' byte array
dim bytData(100) as Byte
.... fill up the bytData with data ...
' assuming the field index 1 is OLE DATA type
adoRS.Fields(1).Value = bytData
End Sub
I also have created an application that reads photos from a database and can add them too. Its a different solution, to the one mentioned above.
I used common dialog 1 to retrieve the filename of the picture and embed the picture in the ole object. As follows
Code:
Private Sub olePhoto_Click()
CommonDialog1.Filter = "Photo Files(*.bmp,*.jpg,*.gif)|*.bmp;*.jpg;*.gif"
CommonDialog1.CancelError = True
On Error GoTo errorhandler
CommonDialog1.ShowOpen
olePhoto.CreateEmbed CommonDialog1.FileName
Exit Sub
errorhandler:
Exit Sub
End Sub
I had already set up an access 2000 database with an ole photo field to store the picture and linked this to my application using a data control called dtaControl. Then I set the datasource of olePhoto to dtaControl, and set the datafield to photo (the ole photo field set up with access). The only other thing to do is update the database. I did this with a command button as follows:
Code:
Private Sub cmdUpdate_Click()
dtaControl.UpdateRecord
End Sub
hello.
I've done all the things that coolbiz have shown in his message, and I used also the database sample that he has posted in your previos reply.
first of all - thanks a lot, coolbiz.
second, it works well, but only if I save the picture into an access data base with the code that coolbiz gave, or import an image within MS access.
my problem is that I have allready many many images in an access 2000 table, in an ole object field, and all the images where saved with cut & paste.
in the access table inside the ole object field - "picture" is written, while in coolbiz's database "long bynary data" is written.
when I use his code to save an image file from the "long bynary data", to a file, it saves the picture ok,
BUT when I use this code to save images that I inserted with cut & paste, where "picture" is written inside the ole object field, i recieve a file of unknown format. it does save the file with the correct size, but image applications cannot show the file.
please, does anybody how can I export all my images from my access 2000 DataBase, to files??
Hi!
I'm a newbie of VB programming. What is the difference between the two methods above and, e.g., to save the path and filename in a text field of db?
I have downloaded sample in C drive, it is not adding any new picture in the table when I click save from file .. I have given image file name also please help. it is working for savetofile method....
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.