简体   繁体   中英

Access to Excel Export

I am Exporting 5 queries from Access forms to Excel using Recordset by looping the Excel Object and creating a new excel each time I've looped it. In the end when I want to highlight the First sheet it throws me an error no : 1004

<code>
'Once the loop ends
 xlWorkbook.Sheets("xlsheet1").Select
 xlSheet.Range("A3").Select
</Code>

But when I select the last sheet which is by default highlighted I don't get the error.

<code>
'Once the loop ends
 xlWorkbook.Sheets("xlsheet5").Select
 xlSheet.Range("A3").Select
</Code>

Can someone help me with this.

Thanks in Advance.

It is just as simple as this.

Private Sub ExportExcel()
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "your_query", "C:\your_path_here\your_excel_file.xlsx", True
End Sub

Here is a screen shot of the parameters of the 'DoCmd.TransferSpreadsheet acExport' function:

在此处输入图片说明

So, the next-to-last argument is the sheet name. See this for more info.

http://access-excel.tips/access-vba-cocmd-transferspreadsheet/

Finally, something like this should work for you.

Option Compare Database

Private Sub Command0_Click()

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel10, "Query1", _
"C:\Users\your_path\access_export.xlsx", True, "Query1"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel10, "Query2", _
"C:\Users\your_path\access_export.xlsx", True, "Query2"

End Sub

I can't test this on my work machine because it's locked down so tight, I can barely do any work at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM