SQL Property Example (MDB)

The following example creates a parameter query based on an Orders table. The query selects all orders for which the order date falls between the dates entered by the user.

Sub RangeOfOrders()
    Dim dbs As Database, qdf As QueryDef, rst As Recordset

    ' Return reference to current database.
    Set dbs = CurrentDb
    ' Create new query.
    Set qdf = dbs.CreateQueryDef("RangeOfOrders")
    ' Construct SQL statement including parameters.
    qdf.SQL = "PARAMETERS [Start] DATETIME, [End] DATETIME; " & _
        "SELECT * FROM Orders WHERE OrderDate BETWEEN " _
        & "[Start] AND [End];"
    qdf.Parameters("Start") = #1/1/96#
    qdf.Parameters("End") = #1/31/96#
    ' Create snapshot-type Recordset object from QueryDef object.
    Set rst = qdf.OpenRecordset(dbOpenSnapshot)
    ' Perform operations with recordset.
    .
    .
    .
    rst.Close
    Set dbs = Nothing
End Sub