My command button to preview the data writes the SQL statement based on the
selections. What is the best way to then display the results? If I remember
correctly, DoCmd.RunSQL expects an action statement. Also, DoCmd.OpenQuery
is expected a named query, not a SQL statement (or string variable containing
such).
Suggestions?
So now that I have the query def, I'm still stuck at the point from the
first post - how do I get the results to display?
You can use code to change an already existing query's SQL.
Dim qdf As DAO.QueryDef
Set qdf = CurrentDb.QueryDefs("YourQueryName")
Dim strSQL as String
strSQL = "Select YourTable.[FieldA], YourTable.[FieldB] from
YourTable Order by [FieldA];"
DoCmd.OpenQuery "YourQueryName", acViewDesign
qdf.SQL = strSQL
DoCmd.Close acQuery, "YourQueryName", acSaveYes
DoCmd.OpenQuery "YourQueryName"
--
Fred
Please respond only to this newsgroup.
I do not reply to personal e-mail
I'm not sure my previous reply went.
Here it is again.
Regarding > There has to be some way of writing a SQL statement in VBA
and then viewing the results.<
You are, I believe, mistaken. There is no way to run a new Select
query using just VBA.
You can create a brand new query (using CreateQueryDef) and view that,
but that is still a query.
It's easiest to simply create any query and save it in the database.
Then modify it's SQL as I've indicted in my previous reply.
How does one vary what fields are displayed on a form in a datasheet view
from query to query?
Hi Fred
Actually you don't need the OpenQuery or DoCmd.Close lines. Just qdf.SQL =
<whatever> will do it.
if you use a querydef and modify the SQL property, you can use
DoCmd.OpenQuery qdf.Name
and it will open in normal view as if you opened it from the queries
tab.