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 record开发者_高级运维set.
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"
精彩评论