Solved

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

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 …
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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…

757 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

22 Experts available now in Live!

Get 1:1 Help Now