简体   繁体   中英

Export From Access to Excel

I want to export some data in one recordset in Access to Excel.

I know the DoCmd.TransferSpreadsheet command but it works only with stored queries, and in my case it's a runtime-filtered recordset.

I've tried some codes to do what I want. I can get the data exported but I cannot get the column name from the recordset.

Any suggestion on the commands or how to get those column names from recordset?

DAO recordsets have a name property you can use.

Dim rs As DAO.Recordset

Set rs = CurrentDb.OpenRecordset("SELECT * FROM ARTIKELGRUPPE")
Debug.Print rs.Fields(0).Name
Debug.Print rs.Fields(1).Name

Output for my table:

id
Name

You can alter a stored query prior to calling transfer spreadsheet

Dim myQuery As QueryDef
Set myQuery = CurrentDb.QueryDefs("SampleQuery")
myQuery.SQL = "SELECT * FROM myTable WHERE something"
DoCmd.TransferSpreadsheet acExport,acSpreadsheetTypeExcel9, "SampleQuery", "c:\test.xls"

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