Parameter Object, Parameters Collection Example (MDB)

The following example creates a new parameter query and supplies values for the parameters:

Sub NewParameterQuery()
    Dim dbs As Database, qdf As QueryDef, rst As Recordset
    Dim prm As Parameter, strSQL As String

    ' Return reference to current database.
    Set dbs = CurrentDb
    ' Construct SQL string.
    strSQL = "PARAMETERS [Beginning OrderDate] DateTime, " _
        & "[Ending OrderDate] DateTime; SELECT * FROM Orders " & _
        "WHERE (OrderDate Between[Beginning OrderDate] " _
        & "And [Ending OrderDate]);"
    ' Create new QueryDef object.
    Set qdf = dbs.CreateQueryDef("ParameterQuery", strSQL)
    ' Supply values for parameters.
    qdf.Parameters![Beginning OrderDate] = #4/1/95#
    qdf.Parameters![Ending OrderDate] = #4/30/95#
    ' Open recordset on QueryDef object.
    Set rst = qdf.OpenRecordset
    rst.MoveLast
    MsgBox "Query returned " & rst.RecordCount & " records."
    rst.Close
    Set dbs = Nothing
End Sub