473,385 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Exporting Tables/Queries into csv files

17
I am working with large text files.
I have a request to break up 1 large text file into indiviudal files.

My data example is, i have more fields, but in hopes of keeping it simple this is what i have.
CustID, CustName.

The large file currently has over 100 different CustID's. The request is to create a csv file at each change in the custID and to name the file "CustName".csv

i was able to create VBA code to export to CSV at the click of a button, however it exports the whole table or the whole query, how could i create it creates a file per?

export code...
Private Sub Command19_Click()
DoCmd.TransferText acExportDelim, , "qryMiffcoIncoming", "c:\my documents\\test_csv.csv"
'Shell "excel.exe C:\test_csv.xls ", vbNormalFocus ' Opens Specified file
End Sub
Feb 7 '08 #1
3 6493
It would depend on what you need the program to do. My first inclination would be to suggest getting a list of the unique IDs (by query) and then looping through them (using a recordset). On each loop you could set the export query to filter by the current ID and kick out a csv file.

However I see that you are opening the file right after you create it, did you want to do this for every file created?

Let me know if you would like examples in code as opposed to generalities! ^_^
Feb 7 '08 #2
ckrows
17
It would depend on what you need the program to do. My first inclination would be to suggest getting a list of the unique IDs (by query) and then looping through them (using a recordset). On each loop you could set the export query to filter by the current ID and kick out a csv file.

However I see that you are opening the file right after you create it, did you want to do this for every file created?

Let me know if you would like examples in code as opposed to generalities! ^_^
I would love some code examples. Opening the file at the end was just for me to check the output. Once it is coded it should run and no need to open.
Feb 12 '08 #3
Okay , here's a rough outline for you. It should give you general idea of how I was thinking of handling the problem, but please keep in mind I'm by no means an expert. ^_^;; <-(sheepish look) ((baa!))

1. I'm assuming you're importing the original text file into Access as a table.

2. I'm naming this table [tblData]

2a. I'm assuming [tblData] has two fields (in this mockup), [CustID] and [CustName]

2b. I'm assuming that [CustID] is a Long (autonumber) and [CustName] is a string.

2c. I'm assuming the [CustID] is unique and that [CustName] may have duplicates. (ie. two John Smith's)

3. I'm creating a throw-away query called [qrySQL]

So with these assumptions:

Expand|Select|Wrap|Line Numbers
  1. Private Sub ExportFile()
  2. Dim rsCustID As DAO.Recordset
  3. Dim strSQL As String
  4. Dim strFileName As String
  5.  
  6. 'First we need to get a list of the unique CustIDs
  7. strSQL = "SELECT DISTINCT tblData.CustID"
  8. strSQL = strSQL & " FROM tblData"
  9. strSQL = strSQL & " ORDER BY tblData.CustID;"
  10.  
  11. 'Open a copy of this query as a recordset
  12. Set rsCustID = CurrentDb.OpenRecordset(strSQL)
  13.  
  14. 'I always assume an empty recordset is possible, so...
  15. If Not ((rsCustID.EOF) And (rsCustID.BOF)) Then
  16. rsCustID.MoveFirst
  17.  
  18. 'Loop until you hit the end of the query/recordset
  19. Do While Not (rsCustID.EOF)
  20.  
  21. 'First we need to filter the table to the current CustID
  22. strSQL = "SELECT tblData.CustID, tblData.CustName"
  23. strSQL = strSQL & " FROM tblData"
  24. strSQL = strSQL & " WHERE (((tblData.CustID)=" & rsCustID!CustID & "));"
  25. CurrentDb.QueryDefs("qrySQL").SQL = strSQL
  26.  
  27. 'Since we pulled the unique CustID from the table, it should be safe to
  28. '  assume that there will be data to export
  29. strFileName = "c:\my documents\" & rsCustID!CustName & ".csv"
  30. DoCmd.TransferText acExportDelim, , "qrySQL", strFileName
  31.  
  32. rsCustID.MoveNext
  33. Loop
  34.  
  35. MsgBox "Export Complete."
  36. End If
  37.  
  38. End Sub
  39.  
  40.  
Now obviously there isn't as much error trapping in this as there should be, but that should give you a rough idea of what I was thinking. ^_^
Feb 12 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Janne Ruuttunen | last post by:
Hello DB2 people, I'm having problems exporting >= 250000 lobs to IXF files with the LOBSINFILE option, using a legacy DB2 2.1 system on Win NT. If I don't specify a path for the lobs,...
4
by: Dom Hicklin | last post by:
I have created a form onto which images can be dropped and thus added to the OLE field of a Table (Access 2000 linked to SQL 2000 server). I use the Stephen Lebans ExportOLE function to do this...
5
by: jsudo | last post by:
I have a Access database with a large amount of records (close to 500,000) that I would like to export to Excel. I found out that Excel has the capability of of about 65,000 rows so I know I...
2
by: Snozz | last post by:
The short of it: If you needed to import a CSV file of a certain structure on a regular basis(say 32 csv files, each to one a table in 32 databases), what would be your first instinct on how to...
3
by: David P. Donahue | last post by:
I'm re-writing an application in C# that was originally written in Delphi 7. The heart of the application is a DataSet displayed in a DataGrid. One of the main functions of the previous...
5
by: rdemyan via AccessMonster.com | last post by:
I have a need to add another field to all of my tables (over 150). Not data, but an actual field. Can I code this somehow. So the code presumabley would loop through all the tables, open each...
8
by: paquer | last post by:
I'd like to provide an Export Function from my forms, where the User can choose the File name & save location Although I cannot get the "Save As" dialog box to open properly from access. What is...
4
by: myemail.an | last post by:
Hi all, I use Access 2007 and have the following problems: when exporting banal select queries (either to Excel or to a csv file) I find that exporting often doesn't work and creates a file with...
2
by: jjwiet | last post by:
Hello, I use access 2003 and attempting to export/copy records between two access databases (almost identical) with multiple tables (both databases having the same relations between the tables)....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.