Option Explicit
Private Sub Command1_Click()
'
' This code adds a single-field Primary key
'
Dim Cn As ADODB.Connection, Cat As ADOX.Catalog, objTable As ADOX.Table
Set Cn = New ADODB.Connection
Set Cat = New ADOX.Catalog
Set objTable = New ADOX.Table
'Open the connection
Cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=biblio.mdb"
'Open the Catalog
Set Cat.ActiveConnection = Cn
'Create the table
objTable.Name = "Test_Table"
'Create and Append a new field to the "Test_Table" Columns Collection
objTable.Columns.Append "PrimaryKey_Field", adInteger
'Create and Append a new key. Note that we are merely passing
'the "PimaryKey_Field" column as the source of the primary key. This
'new Key will be Appended to the Keys Collection of "Test_Table"
objTable.Keys.Append "PrimaryKey", adKeyPrimary, "PrimaryKey_Field"
'Append the newly created table to the Tables Collection
Cat.Tables.Append objTable
' clean up objects
Set objKey = Nothing
Set objTable = Nothing
Set Cat = Nothing
Cn.Close
Set Cn = Nothing
End Sub
Private Sub Command2_Click()
'
' This code adds a multi-field Primary Key
'
Dim Cn As ADODB.Connection, Cat As ADOX.Catalog
Dim objTable As ADOX.Table, objKey As ADOX.Key
Set Cn = New ADODB.Connection
Set Cat = New ADOX.Catalog
Set objTable = New ADOX.Table
Set objKey = New ADOX.Key
Cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=biblio.mdb"
Set Cat.ActiveConnection = Cn
objTable.Name = "Test_Table2"
objTable.Columns.Append "PrimaryKey_Field1", adInteger
objTable.Columns.Append "PrimaryKey_Field2", adInteger
objKey.Name = "PrimaryKey"
objKey.Type = adKeyPrimary
objKey.Columns.Append "PrimaryKey_Field1"
objKey.Columns.Append "PrimaryKey_Field2"
objTable.Keys.Append objKey
Cat.Tables.Append objTable
' clean up objects
Set objKey = Nothing
Set objTable = Nothing
Set Cat = Nothing
Cn.Close
Set Cn = Nothing
End Sub