View Full Version : Modify Query


FoFa
10-25-2004, 07:23 AM
I have this code to modify a pass through query:
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim strSQL As Variant
Set db = CurrentDb
Set qdf = db.QueryDefs("Dep_Check_History")
strSQL = Left(qdf.SQL, 30) ' Maintain original portion
strSQL = strSQL & " 'F36','09/01/2004','09/30/2004'" ' add parameters
qdf.SQL = strSQL

It uses DAO, and works fine (Basically making a parameter query out of a pass through).

BUT since every thing uses ADO except this, I was wondering if anyone had the ADO code to do the same thing? I can't seem to find it anywhere. I could eleminate the DAO library then, and well, I just want to know how in ADO.
Thanks

Jon K
10-25-2004, 06:56 PM
In Access ADO, we can declare qdf as an Object and set qdf with CurrentDb.QueryDefs as if it was a DAO qdf. The following works on my system in ADO:-

Dim qdf As Object

Set qdf = CurrentDb.QueryDefs("Query1")

To do it in the ADO way, you can make a reference to the Microsoft ADO Ext. 2.x for DDL and Security and use ADOX.Catalog to modify the command text of a Porcedure/View object.

The following is taken from Access's help:-

Procedure Text Example

The following code demonstrates how to use the Command property to update
the text of a procedure.

Sub ProcedureText()

Dim cnn As New ADODB.Connection
Dim cat As New ADOX.Catalog
Dim cmd As New ADODB.Command

' Open the Connection
cnn.Open _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=c:\Program Files\Microsoft Office\" & _
"Office\Samples\Northwind.mdb;"

' Open the catalog
Set cat.ActiveConnection = cnn

' Get the Command
Set cmd = cat.Procedures("CustomerById").Command

' Update the CommandText
cmd.CommandText = "Select CustomerId, CompanyName, ContactName " & _
"From Customers " & _
"Where CustomerId = [CustId]"

' Update the Procedure
Set cat.Procedures("CustomerById").Command = cmd
End Sub

FoFa
11-05-2004, 06:15 AM
Thanks Jon!
I saw that in Access help, but I did not want to hard code that connect string and I have not used the command object. I like the first Access ADO example, simple. I'll give it a shot.