MacRena
asked on
ADO equivalent of DAO QueryDef object
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("qryMy Query")
qdf.SQL = "SELECT * FROM [MyTable] WHERE [MyField] = 1;"
DoCmd.OpenReport ("rptMyReport"), acPreview
Set qdf = Nothing
thanks,
mac
how do you say the following in ADO?
Dim qdf As DAO.QueryDef
Set qdf = CurrentDb.QueryDefs("qryMy
qdf.SQL = "SELECT * FROM [MyTable] WHERE [MyField] = 1;"
DoCmd.OpenReport ("rptMyReport"), acPreview
Set qdf = Nothing
thanks,
mac
ASKER
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).C ommand
with the following error:
Item cannot be found in the collection corresponding to the requested name or ordinal.
but the query that strQuery represents ("qryTrackingGroupMileston es_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 = "qryTrackingGroupMilestone s_subform"
lngI = Nz(Me.Parent!sfmTrackingGr oup.Form!t xtHiddenID , 0)
If lngI <> 0 Then
strSQL = "SELECT * FROM [tblTrackingGroupMilestone s] WHERE ("
strSQL = strSQL & "([ID] = " & lngI & ")"
strSQL = strSQL & ");"
Set cat.ActiveConnection = CurrentProject.Connection
Set cmd = cat.Procedures(strQuery).C ommand '<--- breaks here
cmd.CommandText = strSQL
Set cat.Procedures(strQuery).C ommand = cmd
End If
Set cmd = Nothing
Set cat = Nothing
End Sub
******* end code ******
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).C
with the following error:
Item cannot be found in the collection corresponding to the requested name or ordinal.
but the query that strQuery represents ("qryTrackingGroupMileston
********* 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 = "qryTrackingGroupMilestone
lngI = Nz(Me.Parent!sfmTrackingGr
If lngI <> 0 Then
strSQL = "SELECT * FROM [tblTrackingGroupMilestone
strSQL = strSQL & "([ID] = " & lngI & ")"
strSQL = strSQL & ");"
Set cat.ActiveConnection = CurrentProject.Connection
Set cmd = cat.Procedures(strQuery).C
cmd.CommandText = strSQL
Set cat.Procedures(strQuery).C
End If
Set cmd = Nothing
Set cat = Nothing
End Sub
******* end code ******
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
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 :-)
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 :-)
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).C
cmd.CommandText = strSQL
Set cat.Procedures(strQuery).C
Set cmd = Nothing
Set cat = Nothing
End Sub