export dynamic query to excel spreadsheet (1 Viewer)

selahlynch

Registered User.
Local time
Tomorrow, 01:20
Joined
Jan 3, 2010
Messages
63
Hello,

I'm trying to export a crosstab query from a form in Access to an Excel spreadsheet.

Unfortunately the following code did not work:

Code:
sqlquery = "SELECT * FROM AllPossibilitiesAttendance WHERE isSS"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, sqlquery, "C:\AllPossibilitiesAttendance.xls", True
It gave me an error "Run-time error '3011: The microsoft engine could not find the object 'SELECT * FROM AllPossibilitiesAttendance WHERE isSS; '..."

I'd like to keep my query as a variable within the code, because the query is dynamic; it is modified as a result of selections that are made on the form.

Any suggestions?
 
Last edited:

namliam

The Mailman - AWF VIP
Local time
Today, 23:20
Joined
Aug 11, 2003
Messages
11,695
In order to export a query you need to have it either as a report, form or query object

You can then use the OutputTo or Transferspreadsheet commands to export it...

Good luck
 

selahlynch

Registered User.
Local time
Tomorrow, 01:20
Joined
Jan 3, 2010
Messages
63
Perhaps you can help me with creating a query object within VBA code?

The following code gave me an error:

Code:
Dim dbsA As Database
Set dbsA = CurrentDb()
Dim qdfTemp As QueryDef
Set qdfTemp = dsbA.CreateQueryDef("tempqueryy", "SELECT * FROM Employees")
The error was "Run-time error '424': Object required"
 

namliam

The Mailman - AWF VIP
Local time
Today, 23:20
Joined
Aug 11, 2003
Messages
11,695
The easiest way to do this is have a "default" export query... Just an empty "dummy" query object that is called like "qryExport" or something

Then filling that object with sql works like so:
Code:
Dim qryDef as queryDef
set qryDef = currentdb.querydefs("qryExport")
qrydef.sql = "Select ....."
set qrydef = nothing
' then export qryExport

Hope that helps
 

Users who are viewing this thread

Top Bottom