Connecting Tech Pros Worldwide Forums | Help | Site Map

Export query result to csv file

xxoulmate's Avatar
Member
 
Join Date: May 2007
Posts: 69
#1: Apr 23 '09
i wanted to export the query result into csv file
is there any way to do it.,


e.g. select field1,field2 from table into csvfile

Delerna's Avatar
Expert
 
Join Date: Jan 2008
Location: Sydney
Posts: 860
#2: Apr 23 '09

re: Export query result to csv file


You could link an excel worksheet as a table and run an insert query into the linked worksheet.
xxoulmate's Avatar
Member
 
Join Date: May 2007
Posts: 69
#3: Apr 23 '09

re: Export query result to csv file


how about in sql query., is there a way to do it in pure sql statement
Delerna's Avatar
Expert
 
Join Date: Jan 2008
Location: Sydney
Posts: 860
#4: Apr 23 '09

re: Export query result to csv file


Not that I am aware of.
You coud do it with a combination of VBA, (DAO or ADO) and VBA file handling functionality.

Open a text file object
Open A recordset object from your query
loop through each record
add each fields content separated with commas to the file
close the recordset object
close the file object



You could also do a similar thing with vbscript and the File system object.
Again using ADO to query the DB
The advantage there would be you could schedule the script to run automatically.
If that was important.
Delerna's Avatar
Expert
 
Join Date: Jan 2008
Location: Sydney
Posts: 860
#5: Apr 23 '09

re: Export query result to csv file


well, time to take foot out of mouth :)

1) Create your csv file
2) Add 1 row of comma separated data.
3) Link the csv file as a table. (there must be at least 1 row of data or it wont link)
4) Create your query and make it an append query (into the linked csv)
5) Run the query
6) Close Database
7) Open csv file and VOILA
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 16,173
#6: Apr 23 '09

re: Export query result to csv file


An alternative would be to create a simple SELECT QueryDef, and Export (File / Export...) this into your new CSV file.
Reply