Solved

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

Posted on 2009-07-02
3
406 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Webinar: Choosing a MySQL HA Solution

Join Percona’s Principal Technical Services Engineer, Marcos Albe as he presents Choosing a MySQL High Availability Solution on Thursday, June 29, 2017 at 10:00 am PDT / 2:00 pm EDT (UTC-7).

Question has a verified solution.

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

Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

717 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