rajeev
rajeev

Reputation: 323

Execute saved query and retrieve results in access 2007

Here is the problem I have. I need to execute a saved query that accepts parameters from an entry in a form. After the query is executed, I need to retrieve the selected value. How do I go about doing that?

I understand I can execute the query using CurrentDB.Openrecordset("myquery"). I want to use ADO for this.

I was able to insert rows to the table using ADO.

Appreciate your help in advance

Upvotes: 0

Views: 90

Answers (1)

Gord Thompson
Gord Thompson

Reputation: 123419

You can do that in Access VBA with ADO by treating the query as a Stored Procedure, like so:

Dim cmd As ADODB.Command, rst As ADODB.Recordset
Set cmd = New ADODB.Command
cmd.ActiveConnection = CurrentProject.Connection
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "yourQueryName"
cmd.Parameters.Append cmd.CreateParameter("yourParameterName", adInteger, adParamInput, , 5)
Set rst = cmd.Execute
Do Until rst.EOF
    Debug.Print rst(0).Value
    rst.MoveNext
Loop
rst.Close
Set rst = Nothing
Set cmd = Nothing

Upvotes: 1

Related Questions