View Full Version : Using the Open Statement


aziz rasul
06-01-2007, 02:53 AM
I am trying to write data from a query to a text file using the Open Statement. Here's the code I have so far.

Dim FileName As String
Dim rstInternetRanging As DAO.Recordset

Set rstInternetRanging = CurrentDb.OpenRecordset("qryIntExp11", dbOpenDynaset)

FileName = "D:\Crystal\Current Development\Test.txt"

Open FileName For Output As #1

Write #1, rstInternetRanging!Keyword; rstInternetRanging!CountOfKeyword
rstInternetRanging.MoveNext
Close #1

The code works in the sense that I get the first line in the text file as I want it except that I want a SEMI-COLON to separate the 2 values not a COMMA

Here's what I get: -
"Control","272"

Here's what I want: -
"Control";"272"

How do I change my code to obtain this?

boblarson
06-01-2007, 03:32 AM
Change:
Write #1, rstInternetRanging!Keyword; rstInternetRanging!CountOfKeyword

To This
Write #1, rstInternetRanging!Keyword, rstInternetRanging!CountOfKeyword

Oh, and just one more thing. Just as an FYI, if you do NOT want the quotations around the results, use Print #1 instead of Write #1.

aziz rasul
06-01-2007, 03:42 AM
Tried your suggestion, but it didn't work.

Thanks for the tip about the difference between Write and Print.

aziz rasul
06-01-2007, 03:47 AM
BTW I have now been informed that the text file must also conform to utf-8?

Dennisk
06-01-2007, 05:07 AM
I think you will have to build up each line manually.
Concatenate each Column together bracketed by double quotes (its easy if you use the chr() function and seperate each column by ;. Finally write the whole line as one.

aziz rasul
06-01-2007, 05:14 AM
OK. I'll try that and worry about the utf-8 later.

aziz rasul
06-01-2007, 08:23 AM
No matter what I try I can't get exactly what I want.

boblarson
06-01-2007, 08:31 AM
Try this instead:


Dim FileName As String
Dim rstInternetRanging As DAO.Recordset
Dim strToPrint As String

Set rstInternetRanging = CurrentDb.OpenRecordset("qryIntExp11", dbOpenDynaset)

FileName = "D:\Crystal\Current Development\Test.txt"

Open FileName For Output As #1
strToPrint = rstInternetRanging!Keyword & ";" & rstInternetRanging!CountOfKeyword
Write #1, strToPrint
rstInternetRanging.MoveNext



And why are you closing the output file right after moving to the next record in the recordset? If you do that and are looping it should be replacing the thing you just did with a single record.