Solved

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

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

772 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