The following code demonstrates how to create a new foreign key. It assumes two tables (Customers and Orders) exist.
' BeginCreateKeyVB
Sub CreateKey()
Dim kyForeign As New ADOX.Key
Dim cat As New ADOX.Catalog
' Connect the catalog
cat.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=c:\Program Files\Microsoft Office\" & _
"Office\Samples\Northwind.mdb;"
' Define the foreign key
kyForeign.Name = "CustOrder"
kyForeign.Type = adKeyForeign
kyForeign.RelatedTable = "Customers"
kyForeign.Columns.Append "CustomerId"
kyForeign.Columns("CustomerId").RelatedColumn = "CustomerId"
kyForeign.UpdateRule = adRICascade
' Append the foreign key
cat.Tables("Orders").Keys.Append kyForeign
'Delete the Key as this is a demonstration
cat.Tables("Orders").Keys.Delete kyForeign.Name
End Sub
' EndCreateKeyVB
Append Method (Columns) | Append Method (Keys) | Catalog Object | Column Object | Columns Collection | Key Object | Keys Collection | Name Property | RelatedColumn Property | RelatedTable Property | Table Object | Tables Collection | Type Property (Key) | UpdateRule Property
© 1998-2001 Microsoft Corporation. All rights reserved.