Solved

MS Access VBA export issue

Posted on 2014-12-16
5
347 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 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access 2016 7 35
Access query expression 6 20
Access 2003 query lost it's only join 7 27
Query group by data in SQL Server - cursor? 3 33
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

773 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