Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extract 'Long binary data' to a file

Status
Not open for further replies.
Jul 15, 2002
9
AU
I have an access table that contains binary data in a field. Such things as PDF's, TIF's, and PaperPort .max documents. I want to extract them, but I can't seem to get the code right. The below seems to run through, but the resulting file is corrupted.

Function extractfile()
Dim db As Database
Set db = CurrentDb()
Dim rstFile As Recordset
Set rstFile = db.OpenRecordset("File", dbOpenDynaset)
Dim data As Variant
data = rstFile!File_Data
rstFile.MoveFirst
Open "c:\testfile.max" For Binary Access Write As #1
Put #1, , data
'Write #1, data
Close #1


And yes, the first record in the table is a max document.
The Write #1, data line fails with an error "Bad File Mode"
Any Ideas?

James
 
James,
I will appologize in advance for comparing apples to oranges, but you appear to be using DAO (OpenRecordset() method) and my world is all about ADO. There is a [tt]GetChunk()[/tt] method in DAO, but the arguments are a little different.
In ADO you use the [tt]GetChuck(FileLength)[/tt] method to extract binary information from a table. Here is a code snipit as an example of how it is used.
Code:
Public Function GetBlankWordTemplate(OutputFileName As String)
Dim conCurrent As ADODB.Connection
Dim rstWordDoc As New ADODB.Recordset
Dim lngOutputFile As Long

Set conCurrent = CurrentProject.Connection
rstWordDoc.Open "tblWordDoc", conCurrent

lngOutputFile = FreeFile
Open OutputFileName For Binary Access Write As #lngOutputFile
Put #lngOutputFile, , rstWordDoc.Fields(0).GetChunk(Len(rstWordDoc.Fields(0)))
Close #lngOutputFile
rstWordDoc.Close
Set rstWordDoc = Nothing
Set conCurrent = Nothing
Close #lngOutputFile
End Function

Hope this helps,
CMP


Instant programmer, just add coffee.
 
Hi CautionMP,

Thanks for your ADO example. I tried it and got it working, but the result was exactly the same as my DAO example.

I eventually tracked the problem with my corrupted files down to being the PUT command. When I looked up the put command in the help, it told me that the PUT command writes a file descriptor (a variable number of bytes depending on the type of data that is detected) to the beginning of the output.

The solution ended up being in the way that I declared my data variable.

For anyone interested, please find the relevant code below.

Function extractfile()
Dim db As Database
Set db = CurrentDb()
Dim rstFile As Recordset
Set rstFile = db.OpenRecordset("File", dbOpenDynaset)
Dim data() As Byte
rstFile.MoveFirst
data = rstFile!File_Data
Open "c:\testfile.txt" For Binary Access Write As #1
Put #1, , data
Close #1
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top