?
Solved

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

Posted on 2009-07-02
3
Medium Priority
?
407 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

Major Serverless Shift

Comparison of major players like AWS, Microsoft Azure, IBM Bluemix, and Google Cloud Platform

Question has a verified solution.

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

Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
In this article, we’ll look at how to deploy ProxySQL.
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

770 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