CREATE TABLE Statement, CONSTRAINT Clause Example

This example creates a new table called ThisTable with two Text fields.

Sub CreateTableX1()

   Dim dbs As Database

   ' Modify this line to include the path to Northwind
   ' on your computer.
   Set dbs = OpenDatabase("Northwind.mdb")

' Create a table with two text fields.
   dbs.Execute "CREATE TABLE ThisTable " _
      & "(FirstName TEXT, LastName TEXT);"

   dbs.Close

End Sub

This example creates a new table called MyTable with two Text fields, a Date/Time field, and a unique index made up of all three fields.

Sub CreateTableX2()

   Dim dbs As Database

   ' Modify this line to include the path to Northwind
   ' on your computer.
   Set dbs = OpenDatabase("Northwind.mdb")

   ' Create a table with three fields and a unique
   ' index made up of all three fields.
   dbs.Execute "CREATE TABLE MyTable " _
      & "(FirstName TEXT, LastName TEXT, " _
      & "DateOfBirth DATETIME, " _
      & "CONSTRAINT MyTableConstraint UNIQUE " _
      & "(FirstName, LastName, DateOfBirth));"

   dbs.Close

End Sub

This example creates a new table with two Text fields and an Integer field. The SSN field is the primary key.

Sub CreateTableX3()

    Dim dbs As Database

   ' Modify this line to include the path to Northwind
   ' on your computer.
   Set dbs = OpenDatabase("Northwind.mdb")

   ' Create a table with three fields and a primary
   ' key.
   dbs.Execute "CREATE TABLE NewTable " _
      & "(FirstName TEXT, LastName TEXT, " _
      & "SSN INTEGER CONSTRAINT MyFieldConstraint " _
      & "PRIMARY KEY);"

   dbs.Close

End Sub