?
Solved

ADO equivalent of DAO QueryDef object

Posted on 2006-07-22
5
Medium Priority
?
1,054 Views
Last Modified: 2008-02-01
i am a good DAO coder.  but this new project requires that i learn ADO.
how do you say the following in ADO?

Dim qdf As DAO.QueryDef
Set qdf = CurrentDb.QueryDefs("qryMyQuery")
qdf.SQL = "SELECT * FROM [MyTable] WHERE [MyField] = 1;"
DoCmd.OpenReport ("rptMyReport"), acPreview
Set qdf = Nothing

thanks,
mac
0
Comment
Question by:MacRena
  • 3
  • 2
5 Comments
 
LVL 44

Expert Comment

by:Leigh Purvis
ID: 17161492
Things like this are more a tad more "faffy" in ADO.  (IMHO)
Not ADO's fault - DAO was born to work with Jet data and understands Access objects so well.

You'll have to add the ADOX library (ADO's data definition library - not all in one as it is with DAO).
(I suppose you could use late binding to get around that requirement).

Here's a general function for you to change the SQL of a querydef object using ADO.
(Assumes query is in the same mdb of course - your example above it so this should be fine ;-)

You can just call it like
AlterQdfADO "qryMyQuery", "SELECT * FROM [MyTable] WHERE [MyField] = 1;"


Sub AlterQdfADO(strQuery As String, strSQL As String)
   
    Dim cat As New ADOX.Catalog
    Dim cmd As New ADODB.Command
   
    Set cat.ActiveConnection = CurrentProject.Connection
   
    Set cmd = cat.Procedures(strQuery).Command
    cmd.CommandText = strSQL
    Set cat.Procedures(strQuery).Command = cmd
   
    Set cmd = Nothing
    Set cat = Nothing
   
End Sub
0
 
LVL 4

Author Comment

by:MacRena
ID: 17161574
hey there, LPervis.
thanks.  i forgot to mention that i have ADO 2.6 selected.
i can't find ADOX but i think you're referring to ADO Ext. 2.8 for DDL and Security?

it breaks on the line
   Set cmd = cat.Procedures(strQuery).Command
with the following error:
   Item cannot be found in the collection corresponding to the requested name or ordinal.

but the query that strQuery represents ("qryTrackingGroupMilestones_subform") exists and is spelled correctly.

*********  here's my code  **********
Option Compare Database
Option Explicit

Private Sub Form_Current()
    Dim cmd As New ADODB.Command
    Dim cat As New ADOX.Catalog
    Dim strQuery As String
    Dim strSQL As String
    Dim lngI As Long

    strQuery = "qryTrackingGroupMilestones_subform"
    lngI = Nz(Me.Parent!sfmTrackingGroup.Form!txtHiddenID, 0)
   
    If lngI <> 0 Then
       
        strSQL = "SELECT * FROM [tblTrackingGroupMilestones] WHERE ("
        strSQL = strSQL & "([ID] = " & lngI & ")"
        strSQL = strSQL & ");"
       
        Set cat.ActiveConnection = CurrentProject.Connection
       
        Set cmd = cat.Procedures(strQuery).Command   '<--- breaks here
        cmd.CommandText = strSQL
        Set cat.Procedures(strQuery).Command = cmd
       
    End If
   
    Set cmd = Nothing
    Set cat = Nothing
   
End Sub

*******  end code  ******
0
 
LVL 44

Accepted Solution

by:
Leigh Purvis earned 400 total points
ID: 17161680
Sorry I forgot to give the full name for the ADOX library yes.

The distinction between types of queries is relevant.
If your query doesn't update data and takes no parameters then even though it's all the same to Access (and hence DAO) in ADO that is enough to classify it as a View.

So instead of reading and updating the
cat.Procedures
collection, you'd work with
cat.Views
in exactly the same way.  Give it a try.
0
 
LVL 4

Author Comment

by:MacRena
ID: 17163359
error's gone, function works.  you're my hero yet again!
i'll award the points now.  if you feel like it, can you explain when the cat.Procedures would be used? in other words, would i use cat.Procedures if i wanted to create an updatable recordset?
thanks so much,
mac
0
 
LVL 44

Expert Comment

by:Leigh Purvis
ID: 17167273
It helps to think of it terms of a server database (which ADO excels at IMO).

Access queries combine some of the functionality that would be provided by Stored Procedures as well as simple Views.
So anything that performs a data update - or any SELECT statement that takes parameters would be considered to fall under the Procedures banner.
And more standard SELECT statements would come under Views.

You could quite easily combine a check for both into one procedure - and just use the appropriate collection.
(Error handling can be a good friend :-)
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re using QODBC to update QuickBooks data from Microsoft® Access but Access is not showing the updated data, you could have set up QODBC incorrectly.
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month13 days, 16 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question