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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Exporting Query Records to Indivedual .CSV files 1

Status
Not open for further replies.

dw1

IS-IT--Management
Oct 2, 2002
47
0
0
US
I am looking for a way to export Query Records to Indivedual .CSV files? Any ideas?
 
Open a recordset over the query. Loop through the records, opening an output file for each one and Printing the fields from the record into the file, with separating commas.

Help topics you might need:
Database Object
OpenRecordset method
Close method
Recordset Object
EOF property
MoveNext method
FreeFile function
Open statement
Print statement
Close statement


Rick Sprague
Want the best answers? See faq181-2886
To write a program from scratch, first create the universe. - Paraphrased from Albert Einstein
 
Sounds simple now who can I find to do it?
 
If you're looking for a manual method, open query, chose from menu: File, Export. Choose Text Files as type, and give destination filename. Click Save, and Export Wizard begins (Access 2000). If you're looking for a more automated method to use in a macro, or VBA code (preferred), then look at the TransferText method. VBA basic command is:

docmd.TransferText acExportDelim,,"qryName","DestinationFileName.csv",true

Ken
 
Thanks for the VB Ken,

One item needs to be clarified in my request. I need to eport one record from the query to one file from the query
 
You mean for instance, your query returns 100 records...You want to write a separate CSV file for each of the 100 records? What do you mean by "one file from the query?" Are you saying the CSV filename is part of the records returned? Just curious.

Ken
 
Correct 100 records in the seleted queary returns 100 .csv file for each record
 
Well, it looks like your going to have to take Rick's advice above and do some coding. Using DAO, maybe code like:

Set DB = CurrentDB()
Set RS = DB.OpenRecordset("qryName")
Do until RS.EOF
Filename = RS![ExportFileName]
OutF = FreeFile
Open Filename For Output As OutF
Print #OutF, RS![Field1] & "," & RS[Field2] & ",' & Etc.
Close #OutF
RS.MoveNext
Loop
RS.Close
DB.Close
 
Thanks, Thats a start I will play with this over the weekend. How do I make the file name from one of the fields?

Filename = RS![[Field1].csv]
 
Filename = RS![Field1] & ".csv"

Assuming Field1 is a valid filename without the ".csv" extension.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top