?
Solved

export access query data to multiple specified worksheet

Posted on 2014-02-04
12
Medium Priority
?
2,817 Views
Last Modified: 2014-04-23
Hi Experts,

I am trying to export data from access query to specific sheets of an excel workbook.

Below is the VBA code I am using:

    Kill "c:\test\test_report.xlsx"   delete the old report
    FileCopy "c:\test\work\test_report_work.xlsx", "c:\test\test_report.xlsx" ' Copy the work file
    xlfile = "c:\test\test_report.xlsx"  
   
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "test_report_01", xlfile, True, "test01"
   DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "test_report_02", xlfile, True, "test02"
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "test_report_03", xlfile, True, "test03"
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "test_report_04", xlfile, True, "test04"
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "test_report_05", xlfile, True, "test05"
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "test_report_06", xlfile, True, "test06"
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "test_report_07", xlfile, True, "test07"
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "test_report_08", xlfile, True, "test08"
 
test_report_work.xlsx workbook contain total of blank 8 sheet named as above(test01 ~test08)
while I execute it only copy data from the query to the same sheet first 4 sheets then add new sheets instead of copy the on the same sheets.


Please advise why its not copy data on the sheet same as first four sheet instead of adding new sheet on the test_report.xlsx workbook.

Your prompt response would be highly appreciated.

Thanks
0
Comment
Question by:alam747
[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
  • 5
  • 4
  • 3
12 Comments
 
LVL 19

Expert Comment

by:Eric Sherman
ID: 39834586
<<<<<<test_report_work.xlsx workbook contain total of blank 8 sheet named as above(test01 ~test08)  while I execute it only copy data from the query to the same sheet first 4 sheets then add new sheets instead of copy the on the same sheets.
Please advise why its not copy data on the sheet same as first four sheet instead of adding new sheet on the test_report.xlsx workbook.>>>

Probably because sheets 1 thru 4 did not exist.  I think Access creates new worksheets inside your existing workbook if the worksheet already exist.  Try deleting all 8 worksheets first then run your export to see if all 8 worksheets will be created.

ET
0
 
LVL 32

Expert Comment

by:Robberbaron (robr)
ID: 39834621
from MSDN, your use of Transfer sheet is not correct
expression.TransferSpreadsheet(TransferType, SpreadsheetType, TableName, FileName, HasFieldNames, Range, UseOA)

Range  Optional Variant. A string expression that's a valid range of cells or the name of a range in the spreadsheet. This argument applies only to importing. Leave this argument blank to import the entire spreadsheet. When you export to a spreadsheet, you must leave this argument blank. If you enter a range, the export will fail.

so it appears you cant export to a specific sheet within a workbook.
0
 
LVL 32

Expert Comment

by:Robberbaron (robr)
ID: 39834624
so you may have to export to 8 workbooks, then combine into a master workbook with 8 sheets.
0
Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

 
LVL 19

Assisted Solution

by:Eric Sherman
Eric Sherman earned 1500 total points
ID: 39834633
Try this ... Including the Sheet Name! with the Range A:A should export the results starting with column A of the existing worksheet.

 DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "test_report_01", xlfile, True, "test01!A:AZ"

 DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "test_report_02", xlfile, True, "test02!A:AZ"


Etc.

Etc.




ET
0
 
LVL 19

Accepted Solution

by:
Eric Sherman earned 1500 total points
ID: 39834644
Rob ...

See my post above.  If you include the SheetName! with a valid Column Range A:AZ like ("test01!A:AZ") then the export will write the data to an existing worksheet in the workbook.

ET
0
 
LVL 32

Expert Comment

by:Robberbaron (robr)
ID: 39834664
mmm.  never tried and cant now as dont have access anymore..

but further searching suggests that etsherman is correct (and MSDN is not...)
http://www.utteraccess.com/forum/lofiversion/index.php/t1989431.html; maybe its a relatively recent ie 2007/2010 update.
0
 

Author Comment

by:alam747
ID: 39835610
Hi etsherman,

I tried the way advised and getting the follwing run time error:

"Table "Test01$A:AE" already exists"

would you please let me know if I missed anything.

Thanks
0
 
LVL 19

Expert Comment

by:Eric Sherman
ID: 39836918
Use the exclamation point (!) instead of the dollar sign ($) to define the worksheet range as shown below:

"Test01!A:AE"



ET
0
 

Author Comment

by:alam747
ID: 39838571
I used ! not the dollar sign but the message shows same as I mentioned.
0
 
LVL 19

Expert Comment

by:Eric Sherman
ID: 39839975
This works each time for me ... I am using Office 2010 but should be the same for other versions of Office.  I am exporting the Employee table to Employee.xlsx which includes the worksheet Emp_List.

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "EMPLOYEE", "C:\Temp\Employee.xlsx", True, "Emp_List!A:AZ"


By any chance do you have the workbook open at the time you are running the export???


ET
0
 

Author Comment

by:alam747
ID: 39881455
No, while exporting workbook in not open.
0
 

Author Closing Comment

by:alam747
ID: 40018877
Thanks
0

Featured Post

Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

This article describes how to import an Outlook PST file to Office 365 using a third party product to avoid Microsoft's Azure command line tool, saving you time.
This article helps those who get the 0xc004d307 error when trying to rearm (reset the license) Office 2013 in a Virtual Desktop Infrastructure (VDI) and/or those trying to prep the master image for Microsoft Key Management (KMS) activation. (i.e.- C…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

764 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