ad-hoc query

G

Guest

I must be stupid, but I can't figure out how to do an ad-hoc query in Access.
I can generate the SQL statement in VBA, but I can't get it to pop up like a
regular query. I need to generate the SQL statement based on user choices,
and then show the records (and their particular fields) retrieved.
 
D

Douglas J. Steele

You could always create a QueryDef object in code, assign it the SQL
statement, and then open the query.
 
G

Guest

Doug:
I created a QueryDef, opened a recordset, and still don't get anything
displayed. How do I get the query to display the records?
 
D

Douglas J. Steele

Dim qdfNew As QueryDef
Dim strSQL As String

strSQL = "SELECT ...."

On Error Resume Next
Set qdfNew = CurrentDb().QueryDefs("MyQuery") I

If Err.Number = 3265 Then ' "Item not found in this collection:
Set qdfNew = CreateQueryDef("MyQuery", strSQL)
Else
qdfNew.SQL = strSQL
End If

DoCmd.OpenQuery "MyQuery"
 
G

Guest

Thanks - the Help talks about not creating new Queries, but handles a
QueryDef as a Query.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top