Solved

MS Access VBA export issue

Posted on 2014-12-16
5
345 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 VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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.
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…
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…

867 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

18 Experts available now in Live!

Get 1:1 Help Now