Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

export access query data to multiple specified worksheet

Posted on 2014-02-04
12
Medium Priority
?
2,858 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Microsoft has changed the look and feel of Azure AD and Microsoft account sign-in pages so that you will have a more unified look and feel when moving between the two interfaces.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
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…

604 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