Export Access query to Excel specific worksheet (1 Viewer)

  • Thread starter Deleted member 134417
  • Start date
D

Deleted member 134417

Guest
Hey guys,
Been racking my brain all day to try and solve this (supposedly easy) task.
I want to export a query into a specific worksheet in Excel.
Have tried DoCmd.TransferSpreadsheet acExport, but it appears you can't specify an existing worksheet or cell range with a query. Some have said tables only.
I want to assign this export task to a command button.
Can someone please help me out with some basic code.
The variables are:
Query name = TrainingDataQ
Excel workbook path & name = C:\Users\me\Desktop\2015\AccessExportTest.xlsm
Desired Excel worksheet = RawData

I think thats all you will need to know. The data in the query varies but would be no more than about 500 rows.
Thanks in advance

Also for the next time i want to run the export, does anyone know a code to open that same excel file and delete the data in the RawData worksheet so that i can export new data from Access?
 
D

Deleted member 134417

Guest
This thread of mine a while back
http://www.access-programmers.co.uk/forums/showthread.php?t=259994

Contains some code you can probably use....
Thank namliam,
I had a look at the code from your thread and it looks way over my head for my limited VBA knowledge. Also Im only trying to export one basic query to a blank worksheet called RawData.
Any help for a simplified version of that code? Or just the main bits that will get my job done?
 

namliam

The Mailman - AWF VIP
Local time
Today, 11:59
Joined
Aug 11, 2003
Messages
11,695
Soemthing like
docmd.TransferSpreadsheet acExport,acSpreadsheetTypeExcelXX,"queryName", "Filename", true, "sheetname"

SHOULD work, however this as I have shown in that other thread seems to fail sometimes on some sort of named range issue....
One issue with my current code, it exports to the same sheetname as the queryname, I guess I should get around to adjusting it so you can actually enter sheet names as well...
 
D

Deleted member 134417

Guest
Soemthing like
docmd.TransferSpreadsheet acExport,acSpreadsheetTypeExcelXX,"queryName", "Filename", true, "sheetname"

SHOULD work, however this as I have shown in that other thread seems to fail sometimes on some sort of named range issue....
One issue with my current code, it exports to the same sheetname as the queryname, I guess I should get around to adjusting it so you can actually enter sheet names as well...

Thanks again for the reply. Ive tried that code before and it exports but it makes a duplicate of the sheet. So for example if i enter:
docmd.TransferSpreadsheet acExport,acSpreadsheetTypeExcelXX,"queryName", "Filename", true, "RawData" - where i want the data to export to my workbook in the worksheet called RawData, it ignores the sheet thats already called RawData and creates a new one called RawData1....?
Any more ideas
 

namliam

The Mailman - AWF VIP
Local time
Today, 11:59
Joined
Aug 11, 2003
Messages
11,695
That doesnt seem to happen for me, though I do tend to run into the named range issue... this one seems new to me?

I could try and adjust the code in the link to send the output to a specific sheet, which is probably much easier to do than to add a parameter to the code....
 
D

Deleted member 134417

Guest
That doesnt seem to happen for me, though I do tend to run into the named range issue... this one seems new to me?

I could try and adjust the code in the link to send the output to a specific sheet, which is probably much easier to do than to add a parameter to the code....

If you could send through any other code that would be very much appreciated!
 

namliam

The Mailman - AWF VIP
Local time
Today, 11:59
Joined
Aug 11, 2003
Messages
11,695
Check that thread again, dont have time to test this right now, but made some quick adjustments that should work. You can now simply name the query and target sheet to export to and that should work.
 

Users who are viewing this thread

Top Bottom