Solved

Need to use VBA to create CSV comma delimited file for Access 2k

Posted on 2014-07-27
4
397 Views
Last Modified: 2014-07-28
Hello,

My access application has a problem and the Export Specification wizard will not work.  I don't have the original disk so I need to create a CSV file from a query programmatically.

The column names of the query will change from time to time, but the name will not.

Can anyone help?

I guess I can use a maketable query then the recordset object to walk through the code, but I would think there should be a more straight forward approach.  I tried using the recordset object to cycle through the query without any luck.

This is a weekend project for me, so I need to get this code working today.

Thanks.
0
Comment
Question by:pcalabria
  • 2
4 Comments
 
LVL 22

Assisted Solution

by:Kelvin Sparks
Kelvin Sparks earned 100 total points
ID: 40222885
Try something like
Docmd.OutputTo acOutputTable, tName,acFormatCSV, sName,False

Where tname is the table you created in the maketable you suggested and sname will be the path and filename you want to output to. The False at the end just stops the output from opening (change to true if you want it to automatically open after being created.

Kelvin
0
 
LVL 57

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 400 total points
ID: 40222957
So here's an example:

 Dim strExportRecord as string

' Open your datasource (query or table)

' Open your output file:
       iFileNum = FreeFile
      Open "C:\TEMP\MyFile.txt" For Append As #iFileNum

' Write a header out to the text file here if you need it.

' Loop reading records in and build up output recs
Do until rs.EOF
   strExportRecord = Chr$(34) & rst![myTextField] & chr$(34) & ", "
   strExportRecord = strExportRecord & rst![myNumericField] & ","
   strExportRecord = strExportRecord & rst![LastfieldNoComma]

  ' Write the record out
   Print #iFileNum, strExportRecord

   rst.MoveNext
Loop

' Done - close the file
   Close #iFileNum

Jim.


 strExportRecord = ""
0
 
LVL 57
ID: 40222961
chr$(34) by the way is the quote (") and using it is one way to get a quote inside a string argument.

Jim.
0
 

Author Closing Comment

by:pcalabria
ID: 40223728
Thanks to both of you!

Kelvin,  I never was able to get this to work with a comma delimited file, but can see how this solution will help in the future.

Jim, you did it again!
Thanks!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

747 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

13 Experts available now in Live!

Get 1:1 Help Now