Open a query with a parameter

Boo

Registered User.
Local time
Yesterday, 16:44
Joined
Aug 30, 2004
Messages
32
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim n As Integer
Dim strSQL As String

Set db = CurrentDb
strSQL = "Select * from [Duh Query]"
Set rs = db.OpenRecordset(strSQL)

This code is on a control (controlA), on a form (FormA), on afterupdate.

It works fine until I put a parameter in the query
The parameter in criteria: [Forms]![FormA]![controlA]

The debugger highlights the line - Set rs = db.OPenRecordset(strSQL)

The error message is: Too few parameters expected 1
 
Last edited:
Options

If you want to pull it off the form, try

Forms!FormA.ControlA

The ! operator might be pulling from field rather than the control name.

Alternately, if you want to pass a parameter to the query in code, work with the Parameters collection:

Dim rs As DAO.Recordset
Dim qd as DAO.QueryDef
set qd = CurrentDb.QueryDefs("Duh Query")
qd.Parameters("forms!FormA!ControlA") = "Monkeys"
Set rs = qd.OpenRecordset
 

Users who are viewing this thread

Back
Top Bottom