Solved

MS Access VBA export issue

Posted on 2014-12-16
5
339 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
  • 3
  • 2
5 Comments
 
LVL 119

Accepted Solution

by:
Rey Obrero 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 119

Expert Comment

by:Rey Obrero
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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

757 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

21 Experts available now in Live!

Get 1:1 Help Now