Establishing Relationships Using Code : Table Join « Access « VBA / Excel / Access / Word






Establishing Relationships Using Code

 
Sub CreateRelation()
    Dim tbl As ADOX.Table
    Dim fk As ADOX.Key
    Dim cat As ADOX.Catalog
    Set cat = New ADOX.Catalog

    cat.ActiveConnection = CurrentProject.Connection
    Set tbl = cat.Tables("tblPeople")
    Set fk = New ADOX.Key
    With fk
        .Name = "PeopleFood"
        .Type = adKeyForeign
        .RelatedTable = "Foods"
        .Columns.Append "FoodID"
        .Columns("FoodID").RelatedColumn = "FoodID"
    End With
    tbl.Keys.Append fk

    Set cat = Nothing
    Set tbl = Nothing
    Set fk = Nothing
End Sub

 








Related examples in the same category

1.Open the recordset, designating that the source is a SQL statement based on more than one table