Solved

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

Posted on 2014-07-27
4
403 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access Runtime 2010 Error 17 32
MS Access to SQL Conversion - Bit and Yes/No Datatypes 12 46
Help writing a query 6 72
Error: Operation must use an updateable query 2 10
When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

920 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

16 Experts available now in Live!

Get 1:1 Help Now