开发者

Export From Access to Excel

开发者 https://www.devze.com 2023-03-19 17:20 出处:网络
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开发者

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"
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号