Solved

MS Access VBA export issue

Posted on 2014-12-16
5
358 Views
Last Modified: 2014-12-22
I am trying to export q query result set from MS access to CSV using VBA.  When I run the query, I get 397 rows.  When I run the export, I get nothing.  The export is creating the file, but there are no rows in the file.  The CSV export methods that I have tried are:

   
DoCmd.RunSavedImportExport "Export-MyQuery_CSV"
 
DoCmd.TransferText acExportDelim, , "MyQuery", "E:\path\path\filename.csv", True
   
DoCmd.OutputTo "MyQuery", "E:\path\path\filename", acFormatTXT, True

Funny thing is, when I export to XLS file format, I get the full query results.   All 397 rows.
The XLS export method I am using is:

     
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "MyQuery", "E:\path\path\filename.xls", True

It is too bad that the 3rd party system that I need to export the data for is not capable of using XLS.  Must be CSV.

Help!
Thanks.
0
Comment
Question by:cef_soothsayer
[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
  • 3
  • 2
5 Comments
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 total points
ID: 40503513
you should use "export specification" for exporting to csv file

to create an export specification
1.right click on the table or query
2.select export > Text file
   click on Browse and locate the destination folder
3. (you can accept the proposed name or change it)
click Save, then click OK
4. In the export text wizard select the type (Delim / Fixed width)
5. Follow the wizard, before clicking on Finish
     5a .Click Advanced
6. In the Export Specification dialog box Field Information List, correct any descrepancies

7. click save as, give the specification a name <-- this is the specification name that you will use in the command line below

"ExportSpecName"  << sample name of export specification

DoCmd.TransferText acExportDelim, "ExportSpecName", "Table Or Query Name", "C:\myCsv.csv", True
0
 
LVL 1

Author Comment

by:cef_soothsayer
ID: 40503830
Please Re-read my post.  That was the first method that I tried.  :)

Anyway, I think I found the bug.  Has nothing to do with the method.  Working to confirm the solution now.
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40503979
<Please Re-read my post.  That was the first method that I tried.  :)>

sorry, but NO. what I posted is NOT the same as what you tried first
0
 
LVL 1

Assisted Solution

by:cef_soothsayer
cef_soothsayer earned 0 total points
ID: 40505141
Yes, I used the RunSavedImportExport method, as well as the TransferText method both with and without the saved export specification.  

Anyway, I solved it and that wasn't the issue.  All of the above code actually works fine. <faceplam>

SOLUTION:

The issue was bad data in the table.  I'm doing this export in a loop through the data by county.  When exporting data for each county, one county showed up twice (same name, different code so it didn't group together), and the second instance had one row, containing nulls, and overwrote the file exported by the first instance.  Hence, a blank output file (headers only).

Thanks for the assistance!
0
 
LVL 1

Author Closing Comment

by:cef_soothsayer
ID: 40512680
Issue was not code.  It was data.  Confusing at first, but I found it.  Gave other person credit anyway.
0

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

729 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