Writing commas & quotes to flat-file?

D

Dennis

I have a string with commas & quotes in it. I need to be able to output that
to a flat-file, while maintaining the text intact. For example, the line:

testdata1 = "hello there","basketball"

is written out as:

testdata1 = "hello there"
"basketball"

I need the line to remain as it is, rather than being parsed into separate
records. I've tried Print #, Write #, and put #, all to no avail. Any ideas?
(BTW, please ignore the bad syntax in the example line; it's just for show.)

Access 2002.

Thanks!
 
J

John Nurick

Hi Dennis,

This line

testdata1 = "hello there","basketball"

won't compile in VBA, so you can't be using it. On the other hand, if
you use a statement like this

testdata1 = = """hello there"",""basketball"""

to assign the string

"hello there","basketball"

to the variable, a statement like this

Print #1, testdata1

outputs it correctly to the file.


So what are you actually doing?
 
D

Dennis

I'm simply opening a flat-file and reading it in line-by-line. I'm looking
for specific keywords in each line, and replacing those values with
information from a database table. Then I write out the line to an output
flat-file. So I read-in a template, and write-out a custom-file. The only
thing the database is used for is holding the replacement values to insert in
the file.

That help?

Dennis
 
J

John Nurick

Dennis,

Perhaps I misread your original message. But in my experience and
according to the documentation, if you assemble the line you want in a
string variable, the Print # statement will output it to file as it
stands followed by a carriage return and line feed to terminate the
line.

E.g.
Dim strS As String
strS = "testdata1 = ""hello there"",""basketball"""
Open "C:\temp\testwrite.txt" For Append as #1
Print #1, strS
Close #1

will put this into strS
testdata1 = "hello there","basketball"
and write this
testdata1 = "hello there","basketball"<CR><LF>
to the file.

To prevent Print # outputting CR and LF to terminate a line, end the
statement with a semicolon, e.g.
Print #1, strS;
There are other subtleties if you pass Print # multiple arguments, or if
you pass numeric and date arguments as well as strings. I steer clear of
these and always assemble the data I need in a string and then Print #
it.
 
D

Dennis

That's the way I understand it works as well. However, that doesn't seem to
be the case, and I'm not at all sure why...
 
D

Dennis

Never mind. The issue was on the INPUT statement, when reading the line into
a var. Because the line has commas in it, it was parsed at read-time.
Changing the input to LINE INPUT solved the problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top