Opening Query from Form

C

CJ

I have a form that is used to generate dynamic SQL. On
the form is a command button that - upon click - will
produce a query based upon the variables input into the
form. It prompts for a name for this query, then saves
it, but does not open the query for viewing.

Any idea how I might get it to do so (also open the
query)? Thanks in advance.
CJ

Here's the code that generates the query:

Private Sub cmdCreateQDF_Click()
On Error GoTo ErrHandler
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim strName As String
strName = Application.Run
("acwzmain.wlib_stUniquedocname", "Query1", acQuery)
strName = InputBox("Please specify a query
name", "Save As", strName)
If Not strName = vbNullString Then
Set db = CurrentDb
Set qdf = db.CreateQueryDef(strName, Me.txtSQL)
qdf.Close
Else
MsgBox "The save operation was cancelled." & vbCrLf
& _
"Please try again.", vbExclamation +
vbOKOnly, "Cancelled"
End If
ExitHere:
On Error Resume Next
qdf.Close
Set qdf = Nothing
db.QueryDefs.Refresh
Set db = Nothing
Exit Sub
ErrHandler:
Resume ExitHere
End Sub
 
G

Guest

Thanks Roger, but I'm still having difficulties. Perhaps
you can help -

The problem (I neglected to make clear the first time) is
that we don't know in advance WHAT the user will name
this query - it will not necessarily be "Query1."

Is there some way to define this parameter as a variable
tied to the variable in the previous action - the action
that created the query?

CJ
 
R

Roger Carlson

Sorry. I did not read carefully enough. You can use exactly the same
variable.

Try this:
DoCmd.OpenQuery strName, acNormal, acEdit
 
C

CJ

It works! You're a genius; thank you.
CJ

-----Original Message-----
Sorry. I did not read carefully enough. You can use exactly the same
variable.

Try this:
DoCmd.OpenQuery strName, acNormal, acEdit

--
--Roger Carlson
www.rogersaccesslibrary.com
Reply to: Roger dot Carlson at Spectrum-Health dot Org




.
 

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