Solved

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

Posted on 2014-07-27
4
408 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
[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
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 58

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 58
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

705 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