On Wed, 23 Jul 2008 10:26:08 -0700, Pendragon wrote:
> Thanks. I can certainly use this in other operations I have, but with the
> present project (which I will use elsewhere), I would rather not create a
> saved query. There has to be some way of writing a SQL statement in VBA and
> then viewing the results.
>
> "fredg" wrote:
>
>> On Wed, 23 Jul 2008 09:29:02 -0700, Pendragon wrote:
>>
>>> I have two unbound text boxes for user input in order to do a quick search.
>>> Based on the selection of one of two check boxes (individuals or companies),
>>> the criteria entered in the text boxes are written into an SQL statement.
>>>
>>> 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?
>>
>> 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
>>
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.
--
Fred
Please respond only to this newsgroup.
I do not reply to personal e-mail
|