Open Parameter queries from code
Posted by Superadmin on May 02 2019 07:13:40

Open Parameter queries from code

 

Open Parameter queries from code

Author(s)
Dev Ashish

(Q) How do I open a Parameter Query from code?

(A) In order to open parametric queries, you need to explicitly pass each parameter by using the Parameters property before opening a recordset based on the query.

For example, in the following code snippet, the Query "Qry1" has two parameters; namely, [Please enter Code:] and [Please enter city:].

'************ Code Start ****************
Dim db As Database 'current database
Dim rs As Recordset 'holds query resultset
Dim qdfParmQry As QueryDef 'the actual query object
    Set db = CurrentDb()
    Set qdfParmQry = db.QueryDefs("Qry1")
    qdfParmQry("Please Enter Code:") = 3

    ' or try this alternate method
    ' to pass the parameter
   
    qdfParmQry![Please Enter City:] = "New York"
    Set rs = qdfParmQry.OpenRecordset()
'************** Code End ***************