Query OutputTo

santosm
3StarLounger
Posts: 253
Joined: 19 Apr 2010, 09:01
Location: Indiana, USA

Query OutputTo

Post by santosm »

Hi All,
I have a query that I want exported as an XLS doc. I am trying this but it doesn't work:

Code: Select all

        
strReport = "select * from Assigned_que_email where [group] = " & intLocation
DoCmd.OutputTo acOutputQuery, strReport, acFormatXLS, strLoc
I am pretty sure I have it wrong where you can't call the query that way in a OutputTo statement. What would be the best way to do this considering the query must be filtered on a variable (intLocation)?

Thanks,
Mark
Thanks,
Mark

User avatar
HansV
Administrator
Posts: 78620
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Query OutputTo

Post by HansV »

Indeed, you cannot use a SQL statement in DoCmd.OutputTo acOutputQuery. You have to refer to a saved query.
You could create and save a query that selects * (all fields) from Assigned_que_email. Let's say that you name it qryAssigned_que_email.
You can then use

Dim strQueryName As String
strQueryName = "qryAssigned_que_email"
CurrentDb.QueryDefs(strQueryName).SQL = "select * from Assigned_que_email where [group] = " & intLocation
DoCmd.OutputTo acOutputQuery, strQueryName, acFormatXLS, strLoc
Best wishes,
Hans

santosm
3StarLounger
Posts: 253
Joined: 19 Apr 2010, 09:01
Location: Indiana, USA

Re: Query OutputTo

Post by santosm »

Thanks Hans! So I got to learn yet another new thing today!
Thanks,
Mark