CREATE INDEX Statement Example (DAO)

Microsoft Jet SQL Reference

CREATE INDEX Statement Example

This example creates an index consisting of the fields Home Phone and Extension in the Employees table.

Sub CreateIndexX1()

    Dim dbs As Database

    ' Modify this line to include the path to Northwind

    ' on your computer.

    Set dbs = OpenDatabase("Northwind.mdb")

    ' Create the NewIndex index on the Employees table.

    dbs.Execute "CREATE INDEX NewIndex ON Employees " _

        & "(HomePhone, Extension);"

    dbs.Close

End Sub

This example creates an index on the Customers table using the CustomerID field. No two records can have the same data in the CustomerID field, and no Null values are allowed.

Sub CreateIndexX2()

    Dim dbs As Database

    ' Modify this line to include the path to Northwind

    ' on your computer.

    Set dbs = OpenDatabase("Northwind.mdb")

    ' Create a unique index, CustID, on the

    ' CustomerID field.

    dbs.Execute "CREATE UNIQUE INDEX CustID " _

        & "ON Customers (CustomerID) " _

        & "WITH DISALLOW NULL;"

    dbs.Close

End Sub