0

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 ?

2
  • In first option you try to export an SQL statement, not a query object. In the second you create a Record set, and don't export it. Commented Apr 10, 2016 at 20:27
  • @marlan, thanks for response, but I've solved It allready. Commented Apr 10, 2016 at 20:29

1 Answer 1

3

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"
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.