Solved

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

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

830 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