SQL Subqueries Example (DAO)

Microsoft Jet SQL Reference

SQL Subqueries Example

This example lists the name and contact of every customer who placed an order in the second quarter of 1995.

This example calls the EnumFields procedure, which you can find in the SELECT statement example.

Sub SubQueryX()

    Dim dbs As Database, rst As Recordset

    ' Modify this line to include the path to Northwind

    ' on your computer.

    Set dbs = OpenDatabase("Northwind.mdb")

    

    ' List the name and contact of every customer

    ' who placed an order in the second quarter of

    ' 1995.

    Set rst = dbs.OpenRecordset("SELECT ContactName," _

        & " CompanyName, ContactTitle, Phone" _

        & " FROM Customers" _

        & " WHERE CustomerID" _

        & " IN (SELECT CustomerID FROM Orders" _

        & " WHERE OrderDate Between #04/1/95#" _

        & " And #07/1/95#);")

    

    ' Populate the Recordset.

    rst.MoveLast

    

    ' Call EnumFields to print the contents of the

    ' Recordset. Pass the Recordset object and desired

    ' field width.

    EnumFields rst, 25

    dbs.Close

End Sub