VBA to Retrieve Query SQL

CT Witter

MrExcel MVP
Joined
Jul 7, 2002
Messages
1,212
I have the following code to retrieve the SQL from a specified query. The problem is that it can only run once and then locks up the db so I can't run again without closing access.

Is there a better way to retrieve the SQL of a query?

Thanks,
CT

Code:
Sub ModifyQuery(strDBPath As String, _
                strQryName As String)
   Dim catDB As ADOX.Catalog
   Dim cmd   As ADODB.Command
   
   Set catDB = New ADOX.Catalog
   ' Open the Catalog object.
   catDB.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
      "Data Source=" & strDBPath
   
   Set cmd = New ADODB.Command
   ' Get the query from the Procedures collection.
   Set cmd = catDB.Procedures(strQryName).Command
   
   ' Get the query SQL.
   Debug.Print cmd.CommandText
   Application.RefreshDatabaseWindow
   Set cmd = Nothing
   Set catDB = Nothing
   
End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Where are you doing this?

In Access or somewhere else?

Code:
Sub SpitSQL()
Dim qdf As QueryDef

    For Each qdf In CurrentDb.QueryDefs
        Debug.Print qdf.SQL
    Next
End Sub
 
Upvote 0
Well can you not adapt the code I posted?
 
Upvote 0
Well if you could explain further what you want to do.

The QueryDefs collection is simply a collection of all the QueryDef's in a database.

And a QueryDef is the definition of the query which has the SQL property which represents the SQL string for the query.

It also has a Name property.
 
Upvote 0
I want to specify a query name ie "Query1" and return the SQL from that query.

I tried using Debug.Print CurrentDb.QueryDefs("Query1").SQL but all I get back is SELECT;

when i do the loop it returns the entire SQL

**EDIT**

Now Seems to work (re-started PC).

Thanks for your help!
 
Upvote 0
I realize this is an answered question - over and done, but why did you want to get the SQL statement anyways?

Is it for display such as on a form?
 
Upvote 0
Yep, exactly I am using on a form. I am letting the user view it to make changes and then re-saving.
 
Upvote 0

Forum statistics

Threads
1,223,261
Messages
6,171,075
Members
452,377
Latest member
bradfordsam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top