Solved

Create multiple .CSV files from a single Query or SQL Select command

Posted on 2009-07-02
3
403 Views
Last Modified: 2012-06-27
I'd done some SQL coding years ago, and am rusty.  I recall being able to import data from .csv files, and export table data to .csv files, but I forget the syntax.  

Ideally, I'd like the .csv filenames to be programmed based on the data.

A bit more detail:
I'm working in Access 2007.  I need to export data to .csv files, and the filenames are dependent on three table columns (fields), say X, Y, and Z, with more columns A, B, & C.

Even more detail:
I'm thinking of an SQL Select command that will take columns (fields) A,B,C,X,Y,Z and write out the comma-delimited flat files like such:

filename:  X1-Y1-Z1.csv  (where X1, Y1, and Z1 equal the actual contents of those text fields)
if X1="big"
Y1="green"
Z1="Wednesday"
the filename would be big-green-Wednesday.csv

And, with the header row included, the first file contents might look like this:
"X","Y","Z","A","B","C"
"big","green","Wednesday",10,"box",02052009
"big","green","Wednesday",12,"bag",03052009
"big","green","Wednesday",190,"cup",04062009

The next file, X1-Y1-Z2, might be named big-green-Friday.csv, and the next file, X1-Y3-Z5, might be big-yellow-Sunday.csv

This would be a huge help - I'd award more points than 500 if I could.
0
Comment
Question by:Robert
  • 2
3 Comments
 
LVL 16

Expert Comment

by:Chuck Wood
ID: 24768692
Do any of the fields have one or more commas in the contents?
0
 

Author Comment

by:Robert
ID: 24773526
No, none of them do.  But possibly, they could.  Of course, I could run a function to replace commas with a dash or tilde.

I'm not sure that Access' version of SQL permits writing of .csv files, period.  Any Access Experts out there???

I've tried the Macro TransferText action, but the File Name cannot be an expression, at least I can't get it to accept an expression.

- ub

0
 

Accepted Solution

by:
Robert earned 0 total points
ID: 24796952
Solved this with help outside of EE.
Dim dbs As DAO.Database
Dim rst, data As DAO.Recordset
 
Set dbs = CurrentDb
Set data = dbs.OpenRecordset("csv Filenames", dbOpenSnapshot)
 
data.MoveFirst
Do While Not data.EOF
'strfinal is the filename
  strfinal = "c:\Test\" + data![X1] + "\" + data![X1] + "-" + data![X2] + "-" + data![X3] + "-" + ".csv"
 
  DoCmd.TransferText acExportDelim, "", "TET", strfinal, True, ""
  data.MoveNext
  Exit Sub
Loop

Open in new window

0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question