VBA and binary file write 
Author Message
 VBA and binary file write

Hello,
This might be a stupid question, but I could not find any answer.
According to MS doc, if you open a file in RANDOM mode to write a string,
you write also a header giving the length of the string. An if you open in
BINARY mode, you write only the lenght of the string.
I tried Open Random or Open Binary, and it gives exactly the same results.
Example :

Dim s
s = "ABCDE"
Open "Test" For Binary Access Write As 1
Put #1, , s
Close #1

According to the doc, this should write only 5 bytes. It does not (I tried
Access 97 and Word 97).
I want my file to read "ABCDE", how can I do this in VBA ?
That is obvious to do in any language I know.
Please help.



Sat, 14 Feb 2004 20:54:33 GMT  
 VBA and binary file write
Jean:

Works fine on my PC (with minor changes).  Are you looking at the right
file?  I notice you haven't specified a path.  I also notice you haven't got
a file handle using FreeFile.

Dim s
Dim i
s = "ABCDE"
i= FreeFile
Open "C:\Test.txt" For Binary Access Write As i
Put i, , s
Close i

--
Paul Overway, MCSD
Logico Solutions, LLC


Quote:
> Hello,
> This might be a stupid question, but I could not find any answer.
> According to MS doc, if you open a file in RANDOM mode to write a string,
> you write also a header giving the length of the string. An if you open in
> BINARY mode, you write only the lenght of the string.
> I tried Open Random or Open Binary, and it gives exactly the same results.
> Example :

> Dim s
> s = "ABCDE"
> Open "Test" For Binary Access Write As 1
> Put #1, , s
> Close #1

> According to the doc, this should write only 5 bytes. It does not (I tried
> Access 97 and Word 97).
> I want my file to read "ABCDE", how can I do this in VBA ?
> That is obvious to do in any language I know.
> Please help.



Sat, 14 Feb 2004 21:16:20 GMT  
 VBA and binary file write
I did all these already (even reinstalled access thinking that maybe
something was corrupt), but nothing helps.
I thought I got something when trying to add a crlf at the end of the
string, but I only moved the problem.
I tried in a brand new database with just this routine, and i does not work.
I really don't understand at all.
Thanks anyway.
JM



Quote:
> Jean:

> Works fine on my PC (with minor changes).  Are you looking at the right
> file?  I notice you haven't specified a path.  I also notice you haven't
got
> a file handle using FreeFile.

> Dim s
> Dim i
> s = "ABCDE"
> i= FreeFile
> Open "C:\Test.txt" For Binary Access Write As i
> Put i, , s
> Close i

> --
> Paul Overway, MCSD
> Logico Solutions, LLC



> > Hello,
> > This might be a stupid question, but I could not find any answer.
> > According to MS doc, if you open a file in RANDOM mode to write a
string,
> > you write also a header giving the length of the string. An if you open
in
> > BINARY mode, you write only the lenght of the string.
> > I tried Open Random or Open Binary, and it gives exactly the same
results.
> > Example :

> > Dim s
> > s = "ABCDE"
> > Open "Test" For Binary Access Write As 1
> > Put #1, , s
> > Close #1

> > According to the doc, this should write only 5 bytes. It does not (I
tried
> > Access 97 and Word 97).
> > I want my file to read "ABCDE", how can I do this in VBA ?
> > That is obvious to do in any language I know.
> > Please help.



Sat, 14 Feb 2004 22:35:34 GMT  
 VBA and binary file write
Jean,

One thing I noticed was that you did a :
   Dim S

This dimensions S as a variant. When I copied your code
and executed it, I received extra characters.

I dimensioned the variable as string and re-executed the
code and came out with 5 bytes.

Give it a try and let us know.

Justin

Quote:
>-----Original Message-----
>I did all these already (even reinstalled access thinking
that maybe
>something was corrupt), but nothing helps.
>I thought I got something when trying to add a crlf at
the end of the
>string, but I only moved the problem.
>I tried in a brand new database with just this routine,

and i does not work.
Quote:
>I really don't understand at all.
>Thanks anyway.
>JM



crit dans le message
Quote:

>> Jean:

>> Works fine on my PC (with minor changes).  Are you

looking at the right
Quote:
>> file?  I notice you haven't specified a path.  I also
notice you haven't
>got
>> a file handle using FreeFile.

>> Dim s
>> Dim i
>> s = "ABCDE"
>> i= FreeFile
>> Open "C:\Test.txt" For Binary Access Write As i
>> Put i, , s
>> Close i

>> --
>> Paul Overway, MCSD
>> Logico Solutions, LLC


message

>> > Hello,
>> > This might be a stupid question, but I could not find
any answer.
>> > According to MS doc, if you open a file in RANDOM
mode to write a
>string,
>> > you write also a header giving the length of the

string. An if you open

- Show quoted text -

Quote:
>in
>> > BINARY mode, you write only the lenght of the string.
>> > I tried Open Random or Open Binary, and it gives
exactly the same
>results.
>> > Example :

>> > Dim s
>> > s = "ABCDE"
>> > Open "Test" For Binary Access Write As 1
>> > Put #1, , s
>> > Close #1

>> > According to the doc, this should write only 5 bytes.
It does not (I
>tried
>> > Access 97 and Word 97).
>> > I want my file to read "ABCDE", how can I do this in
VBA ?
>> > That is obvious to do in any language I know.
>> > Please help.

>.



Sun, 15 Feb 2004 00:50:17 GMT  
 VBA and binary file write
I tried that too, and also to set a Type, etc., but nothing went right. So i
wrote a quick filter in C linked to my module and that's it. I already spent
one day trying to figure out why.
Thanks anyway for your help
Jean-Michel



Jean,

One thing I noticed was that you did a :
   Dim S

This dimensions S as a variant. When I copied your code
and executed it, I received extra characters.

I dimensioned the variable as string and re-executed the
code and came out with 5 bytes.

Give it a try and let us know.

Justin

Quote:
>-----Original Message-----
>I did all these already (even reinstalled access thinking
that maybe
>something was corrupt), but nothing helps.
>I thought I got something when trying to add a crlf at
the end of the
>string, but I only moved the problem.
>I tried in a brand new database with just this routine,

and i does not work.
Quote:
>I really don't understand at all.
>Thanks anyway.
>JM



crit dans le message
Quote:

>> Jean:

>> Works fine on my PC (with minor changes).  Are you

looking at the right
Quote:
>> file?  I notice you haven't specified a path.  I also
notice you haven't
>got
>> a file handle using FreeFile.

>> Dim s
>> Dim i
>> s = "ABCDE"
>> i= FreeFile
>> Open "C:\Test.txt" For Binary Access Write As i
>> Put i, , s
>> Close i

>> --
>> Paul Overway, MCSD
>> Logico Solutions, LLC


message

>> > Hello,
>> > This might be a stupid question, but I could not find
any answer.
>> > According to MS doc, if you open a file in RANDOM
mode to write a
>string,
>> > you write also a header giving the length of the

string. An if you open

- Show quoted text -

Quote:
>in
>> > BINARY mode, you write only the lenght of the string.
>> > I tried Open Random or Open Binary, and it gives
exactly the same
>results.
>> > Example :

>> > Dim s
>> > s = "ABCDE"
>> > Open "Test" For Binary Access Write As 1
>> > Put #1, , s
>> > Close #1

>> > According to the doc, this should write only 5 bytes.
It does not (I
>tried
>> > Access 97 and Word 97).
>> > I want my file to read "ABCDE", how can I do this in
VBA ?
>> > That is obvious to do in any language I know.
>> > Please help.

>.



Sun, 15 Feb 2004 07:48:45 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Writing Binary Files With VBA for Excel 95

2. reading binary file and writing into a random access file

3. VB -- Long File Name , Fast Binary File Read / Write , interface with DLL

4. Write ADO Field containing binary data (files) to the file system

5. VBA - Write To File - Simple Log File!

6. Write image data as binary to append existing file

7. Write data in binary file / Convert Image

8. open a binary file for reading/writing

9. Writing partial array to binary file

10. Reading/Writing Data for Binary Files

11. How are Binary files (MKD$) written in BASIC?

12. reading from and writing to a binary file?!?

 

 
Powered by phpBB® Forum Software