Solved

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

Posted on 2009-07-02
3
402 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
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…

932 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now