dbTalk Databases Forums  

Output to file

comp.databases.ms-access comp.databases.ms-access


Discuss Output to file in the comp.databases.ms-access forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
imb
 
Posts: n/a

Default Output to file - 10-09-2010 , 03:30 PM






Hi,

Within Access2003 I use this peace of code:

Open Filename For Output As #1
Print #1, SomeText
Close #1.

When I read the contents of Filename, it has two bytes more, viz.
Chr(13)Chr(10).

How can I prevent the addition of these two bytes?

Thanks,
Imb.

Reply With Quote
  #2  
Old   
Bob Quintal
 
Posts: n/a

Default Re: Output to file - 10-09-2010 , 04:46 PM






imb <imb4u (AT) onsmail (DOT) nl> wrote in news:bc2b3f17-f5ee-4892-aaf7-
94347a8284c6 (AT) a15g2000yqm (DOT) googlegroups.com:

Quote:
Hi,

Within Access2003 I use this peace of code:

Open Filename For Output As #1
Print #1, SomeText
Close #1.

When I read the contents of Filename, it has two bytes more, viz.
Chr(13)Chr(10).

How can I prevent the addition of these two bytes?

Thanks,
Imb.

Without the statement that sets the value of the variable Filename,
people will not be able to help.
Note: Chr(13)Chr(10) is the two bytes of the Windows Newline .


--
Bob Q.
PA is y I've altered my address.

Reply With Quote
  #3  
Old   
imb
 
Posts: n/a

Default Re: Output to file - 10-09-2010 , 05:03 PM



On Oct 9, 11:46*pm, Bob Quintal <rquin... (AT) sPAmpatico (DOT) ca> wrote:
Quote:
imb <im... (AT) onsmail (DOT) nl> wrote in news:bc2b3f17-f5ee-4892-aaf7-
94347a828... (AT) a15g2000yqm (DOT) googlegroups.com:





Hi,

Within Access2003 I use this peace of code:

Open Filename For Output As #1
Print #1, SomeText
Close #1.

When I read the contents of Filename, it has two bytes more, viz.
Chr(13)Chr(10).

How can I prevent the addition of these two bytes?

Thanks,
Imb.

Without the statement that sets the value of the variable Filename,
people will not be able to help.
Note: Chr(13)Chr(10) is the two bytes of the Windows Newline .

--
Bob Q.
PA is y I've altered my address.- Hide quoted text -

- Show quoted text -
Hi Bob,

Filename is the name of the file to write to, including the path.
SomeText is a string of characters.

Yes, Chr(13)Chr(10) is the Windows Newline, but why is this added to
the file?
I just want to have the value of SomeText in the file, because this
file is passed to a next application, that does not like this Newline.

Thanks,
Imb.

Reply With Quote
  #4  
Old   
Salad
 
Posts: n/a

Default Re: Output to file - 10-09-2010 , 05:48 PM



imb wrote:

Quote:
Hi,

Within Access2003 I use this peace of code:

Open Filename For Output As #1
Print #1, SomeText
Close #1.

When I read the contents of Filename, it has two bytes more, viz.
Chr(13)Chr(10).

How can I prevent the addition of these two bytes?

Thanks,
Imb.
Most likely your mode. Ex:
Sub x()
Dim intFile As Long
Dim intfor As Integer
Dim s As String
Dim sfile As String

intFile = FreeFile()

'change the file name value to yours
sfile = "C:\YourFileName.Txt"

'8 byes
Open sfile For Binary Access Write As intFile
Put intFile, , "SomeText"
Close intFile

MsgBox FileLen(sfile)
Kill sfile

'10 bytes
Open sfile For Output As #intFile
Print #intFile, "SomeText"
Close intFile

MsgBox FileLen(sfile)
Kill sfile


End Sub

Reply With Quote
  #5  
Old   
Tony Toews
 
Posts: n/a

Default Re: Output to file - 10-09-2010 , 08:04 PM



On Sat, 9 Oct 2010 13:30:45 -0700 (PDT), imb <imb4u (AT) onsmail (DOT) nl> wrote:

Quote:
Open Filename For Output As #1
Print #1, SomeText
Close #1.

When I read the contents of Filename, it has two bytes more, viz.
Chr(13)Chr(10).

How can I prevent the addition of these two bytes?
That's how Print works. It's designed to create a human readable
file which is easily viewed with Notepad. I use such for debugging
occasionally when Access/VB 6 crash while I'm trying to do weird
stuff.

See Salad's reply for detailed code.

Tony
--
Tony Toews, Microsoft Access MVP
Tony's Main MS Access pages - http://www.granite.ab.ca/accsmstr.htm
Tony's Microsoft Access Blog - http://msmvps.com/blogs/access/
For a convenient utility to keep your users FEs and other files
updated see http://www.autofeupdater.com/

Reply With Quote
  #6  
Old   
Stuart McCall
 
Posts: n/a

Default Re: Output to file - 10-09-2010 , 10:07 PM



"imb" <imb4u (AT) onsmail (DOT) nl> wrote

Quote:
Hi,

Within Access2003 I use this peace of code:

Open Filename For Output As #1
Print #1, SomeText
Close #1.

When I read the contents of Filename, it has two bytes more, viz.
Chr(13)Chr(10).

How can I prevent the addition of these two bytes?

Thanks,
Imb.
The Print # command appends a newline unless you suffix it with a
semi-colon, ie:

Print #1, SomeText;

or

Print #1, "Testing";

Incidentally, it's good practice to obtain a free file number for use with
all file IO commands:

Dim f As Integer

f = FreeFile
Open Filename For Output As #f

etc.

Reply With Quote
  #7  
Old   
Bob Quintal
 
Posts: n/a

Default Re: Output to file - 10-10-2010 , 08:40 AM



imb <imb4u (AT) onsmail (DOT) nl> wrote in
news:4c5c0a0a-20a0-4433-a510-19e28df3c9b9 (AT) x42g2000yqx (DOT) googlegroups.co
m:

Quote:
On Oct 9, 11:46*pm, Bob Quintal <rquin... (AT) sPAmpatico (DOT) ca> wrote:
imb <im... (AT) onsmail (DOT) nl> wrote in news:bc2b3f17-f5ee-4892-aaf7-
94347a828... (AT) a15g2000yqm (DOT) googlegroups.com:





Hi,

Within Access2003 I use this peace of code:

Open Filename For Output As #1
Print #1, SomeText
Close #1.

When I read the contents of Filename, it has two bytes more,
viz. Chr(13)Chr(10).

How can I prevent the addition of these two bytes?

Thanks,
Imb.

Without the statement that sets the value of the variable
Filename, people will not be able to help.
Note: Chr(13)Chr(10) is the two bytes of the Windows Newline .

--
Bob Q.
PA is y I've altered my address.- Hide quoted text -

- Show quoted text -

Hi Bob,

Filename is the name of the file to write to, including the path.
SomeText is a string of characters.

Yes, Chr(13)Chr(10) is the Windows Newline, but why is this added
to the file?
I just want to have the value of SomeText in the file, because
this file is passed to a next application, that does not like this
Newline.

Thanks,
Imb.
From the Help file:
Write # inserts a newline character, that is, a carriage
return–linefeed (Chr(13) + Chr(10)), after it has written the final
character in outputlist to the file.

What you need to do is to open the file as binary,
Open "TESTFILE" For Binary Access Write As #1
Put #1, , Sometext
Close #1

--
Bob Q.
PA is y I've altered my address.

Reply With Quote
  #8  
Old   
imb
 
Posts: n/a

Default Re: Output to file - 10-11-2010 , 11:56 AM



On Oct 10, 5:07*am, "Stuart McCall" <smcc... (AT) myunrealbox (DOT) com> wrote:
Quote:
"imb" <im... (AT) onsmail (DOT) nl> wrote in message

news:bc2b3f17-f5ee-4892-aaf7-94347a8284c6 (AT) a15g2000yqm (DOT) googlegroups.com...

Hi,

Within Access2003 I use this peace of code:

Open Filename For Output As #1
Print #1, SomeText
Close #1.

When I read the contents of Filename, it has two bytes more, viz.
Chr(13)Chr(10).

How can I prevent the addition of these two bytes?

Thanks,
Imb.

The Print # command appends a newline unless you suffix it with a
semi-colon, ie:

Print #1, SomeText;

or

Print #1, "Testing";

Incidentally, it's good practice to obtain a free file number for use with
all file IO commands:

Dim f As Integer

f = FreeFile
Open Filename For Output As #f

etc.
Thank you for all your answers.

Especially for Stuart, his solution is exact the thing what I was
looking for. I was wondering how or where you got this kind of
information.

For Salad and Bob,
long time ago I experimented with the opening of a file in the binary
mode. Yes, it worked, but in some instance it gave a mess, because the
file could be assessed by different processes simultaneous, so very
dangerous in a multi-user environment. However you can prevent that
with the right LOCK in the OPEN definition.
Since then I have always used Print #, and that works well for
generating HTML- or RTF-code, because the readers are indifferent to
Nlcr.

Now I have an application where the record-separator is only the
CHR(10).
With your solution - the semicolon - I can use again the good old
Print #.

Imb.

Reply With Quote
  #9  
Old   
Stuart McCall
 
Posts: n/a

Default Re: Output to file - 10-11-2010 , 12:50 PM



Quote:
I was wondering how or where you got this kind of
information
The file IO commands haven't changed at all since Microsoft Basic, back in
the eighties. That's where I learned about the semicolon 'trick'.

For your info, you can also use the semicolon to concatenate strings 'on the
fly', eg:

strOne = "The cat "
strTwo = "sat on the mat"

Print #f, strOne; strTwo

Result in the file: "The cat sat on the mat"

Also FYI, you can use the comma to move the print position to the next
tab-stop.
There's also the Tab function:

Print #f, strOne, strTwo; Tab(30); "End of string"

Result in the file: "The cat sat on the mat End of string"

All these techniques apply to print commands in the immediate window, too.

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.