Hi,
could anyone tell me how to run Access action (append) query in code using ADO ?
I don't want to use a SQL-string , since this SQL-string is too long (it involves many field to be updated).
Thank you.
Surotkin
Hi vbajock,
it is good to see you after new year!
Thank you for your answer, however I know how to append records using code. My question is - is it possible to run my old action (append) query in code using ADO? I have my old append query and I want to run it within code.
Surotkin
The query can be accessed through the View Collection. You will need a reference to the ADOX library. Here is an example that uses the Open method of the recordset, but the Execute method could be used as well.
Dim cn As New adodb.Connection, sql1 As String
Dim rs As adodb.Recordset, connString As String, bdate As String
Set rs = New adodb.Recordset
''- Microsoft ADO Ext. 2.6 for DDL and Security
Dim cg As New ADOX.Catalog
Set cg.ActiveConnection = CurrentProject.Connection
Dim v As View
Dim vn As View
''For Each v In cg.Views
''Debug.Print "views = "; v.Name
'' If v.Name = "query1" Then
'' Set vn = v
'' End If
''Next
Set vn = cg.Views("query1"
rn.execute "qrySomeQuery", I don't think that is possible in ADO. Everything I have seen shows a SQL string has to be passed to the execute method.
Why not just use the docmd.openquery "qrySomeQuery" to execute your action query?
Hi vbajock,
unfortunatelly you can not use docmd.openquery "qrySomeQuery" with parameters. But I need to pass parameters with my action query.
Anyway, thank you and cmmrfrds for trying to help me.
If you have any other ideas let me know using this thread.
Best regards.
Surotkin
When I've had a problem like that, I create a single record table, store the parameter values into it when they are collected, and then use dlookup("[SomeParameterField],"myParamterTable" in the criteria column of the query, or I use joins on the parameter table.
Then one can use the openquery method to achieve the same results as passing parameters to a stored procedure. For some reason Access's ADO implementation doesn't support querydefs as stored procedure objects, which is the root of the problem.
vbajock, although I did't test it out, I believe the action queries (procedures) reside in the Procedures Collection. This could be applied similiar to the way the View is applied with the Open or Execute methods. Iterate through the procedures collection and you will probably find the action queries.
Ok, that worked. If you use the open method, it executes the query, then automatically closes the object.
Dim cn As New ADODB.Connection, sql1 As String
Dim rs As ADODB.RecordsetSet rs = New ADODB.Recordset
''- Microsoft ADO Ext. 2.6 for DDL and Security
Dim cg As New ADOX.Catalog
Set cg.ActiveConnection = CurrentProject.Connection
Dim v As Procedure
For Each v In cg.Procedures
If v.Name = "Query1" Then
rs.Open v.Name, CurrentProject.Connection
exit for
End If
Next
I bet if you played with it long enough you could figure how to append the parameters to the existing query via the command object
You CAN execute queries using ADO (but I'd agree with Ben that DAO is usually better with any native Access operations).
I created an append query as "qry_test_ADO" in the Access GUI:
[tt]
INSERT INTO applications ( APP_ID, NAME )
VALUES (444, 'a new value');[/tt]
Here's ASP code I used with parameters. The "rows updated" parameter is an output parameter to get a feedback on success. It's essentially the same in the VBA context. You'll need to do some homework on using parameters because there are a number of approaches, consequences, and bugs involved.
[tt]Sub CheckForReviewUpdate
Dim strMode
Dim strVisitID
Dim Prm_VisitGUID
Dim Prm_RowsUpdated
Dim Cmd_UPDATE
Dim intRowsUpdated
Response.Write ("<font color=FF0000 size=2>An error occurred while processing the review update. " & _
"Please try again or contact the administrator if error persists.</font>"
End If
End If
End Sub 'CheckForReviewUpdate[/tt]
Jeffrey R. Roberts
Insight Data Consulting
Access, SQL Server, & Oracle Development
Thank you vbajock and quehay for taking the time to show and test out the different methods. It appears from quehay's use of the execute method with the command object that the execute method will handle the action query as opposed to the execute method of the connection object.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.