简体   繁体   中英

Access VBA - Export from query with query - to Excel

I have a query which shows joined records from 2 tables. Now I want to select certain records with desired ID from that query and export them to Excel. How can I do this ?

This doesn't work :

Dim SQL As String
 
SQL = "SELECT * FROM Created_Query" & _
         " WHERE ID=" & Me![Combobox]
         
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, SQL, FileName, True

I've also tried with DAO.Recordset like this:

Dim Desired As Recordset
Dim SQL As String
      SQL = "SELECT * FROM Created_Query" & _
         " WHERE ID=" & Me![Combobox]
Set Desired= CurrentDb.OpenRecordset(SQL, dbOpenSnapshot)

None of the above methods work. Where am I wrong ?

Solved, CreateQueryDef is what I needed :)

Dim db As DAO.Database
Dim rs As Recordset
Set db = CurrentDb
Dim mySql As String
mySql = "SELECT * FROM Created_Query" & _
         " WHERE ID=" & Me![Combobox]


db.CreateQueryDef "temp", mySql
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "temp", FileName, True
DoCmd.DeleteObject acQuery, "temp"

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