?
Solved

MS Access VBA export issue

Posted on 2014-12-16
5
Medium Priority
?
372 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 2000 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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
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…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

650 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