Bagikan melalui


Metode Tambahan Kunci, Jenis Kunci, RelatedColumn, RelatedTable, dan Contoh Properti UpdateRule (Visual Basic)

Kode berikut menunjukkan cara membuat hubungan kunci asing baru antara dua tabel yang ada bernama Pelanggan dan Pesanan.

' BeginCreateKeyVB  
Sub Main()  
    On Error GoTo CreateKeyError  
  
    Dim kyForeign As New ADOX.Key  
    Dim cat As New ADOX.Catalog  
  
    ' Connect to the catalog.  
    cat.ActiveConnection = "Provider='Microsoft.Jet.OLEDB.4.0';" & _  
        "Data Source='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 to the keys collection.  
    cat.Tables("Orders").Keys.Append kyForeign  
  
    'Delete the key t demonstrate the Delete method.  
    cat.Tables("Orders").Keys.Delete kyForeign.Name  
  
    'Clean up.  
    Set cat.ActiveConnection = Nothing  
    Set cat = Nothing  
    Set kyForeign = Nothing  
    Exit Sub  
  
CreateKeyError:  
    Set cat = Nothing  
    Set kyForeign = Nothing  
  
    If Err <> 0 Then  
        MsgBox Err.Source & "-->" & Err.Description, , "Error"  
    End If  
  
End Sub  
' EndCreateKeyVB  

Lihat juga

Metode Tambahan (Kolom ADOX)
Metode Tambahan (Kunci ADOX)
Objek Katalog (ADOX)
Objek Kolom (ADOX)
Kumpulan Kolom (ADOX)
Objek Kunci (ADOX)
Koleksi Kunci (ADOX)
Properti Nama (ADOX)
Properti RelatedColumn (ADOX)
Properti RelatedTable (ADOX)
Objek Tabel (ADOX)
Koleksi Tabel (ADOX)
Ketik Properti (Kunci) (ADOX)
Properti UpdateRule (ADOX)