G said:
Dirk,
That worked. but I have a question on it.
the line
Set qdf = Nothing ' we're done manipulating it.
Why is that set to nothing before exporting? Seems like that would
make the dataset null (which it didn't).
A QueryDef object is a code structure that represents a query that is
stored in an Access database. When the query is actually created in the
database -- whether by way of the Acess user interface or by code such
as the "db.CreateQueryDef" line in the code I posted -- there is
something physically stored in the database's internal works that
defines the query. A QueryDef object, as I said, is a code
representation of that physically stored query, and it includes
properties and methods that allow us to manipulate the query in code,
but it is not the same thing as the stored query.
The code I posted creates a QueryDef object to represent the query named
"qryDataExport". If the query doesn't already exist it first creates
it, but either way we end up with the object variable qdf pointing to a
QueryDef object (code structure) in memory, which is a representation of
the actual query as it is stored in the database. We then set its SQL
property, which (because that's the way the QueryDef object is defined
to work) physically changes the stored query. Then we destroy the
QueryDef object, but that doesn't destroy the stored query; it only
destroys this code representation of the query. The query itself is
still there, stored in the database, until we delete it one way or
another. I decided not to automatically deleted it after exporting the
query to Excel, because I figure you're going to use it again, just with
different SQL.
Does that clarify the matter?