Foreign Property

Applies To   Index object.

Description

Returns a value that indicates whether an Index object represents a foreign key in a table (Microsoft Jet workspaces only).

Return Values

The return value is a Boolean data type that returns True if the Index object represents a foreign key.

Remarks

A foreign key consists of one or more fields in a foreign table that uniquely identify all rows in a primary table.

The Microsoft Jet database engine creates an Index object for the foreign table and sets the Foreign property when you create a relationship that enforces referential integrity.

See Also   Primary property, TableDef object.

Example

This example shows how the Foreign property can indicate which Index objects in a TableDef are foreign key indexes. Such indexes are created by the Microsoft Jet database engine when a Relation is created. The default name for the foreign key indexes is the name of the primary table plus the name of the foreign table. The ForeignOutput function is required for this procedure to run.

Sub ForeignX()

    Dim dbsNorthwind As Database

    Set dbsNorthwind = OpenDatabase("Northwind.mdb")

    With dbsNorthwind
        ' Print report on foreign key indexes from two
        ' TableDef objects and a QueryDef object.
        ForeignOutput .TableDefs!Products
        ForeignOutput .TableDefs!Orders
        ForeignOutput .TableDefs![Order Details]

        .Close
    End With

End Sub

Function ForeignOutput(tdfTemp As TableDef)

    Dim idxLoop As Index

    With tdfTemp
        Debug.Print "Indexes in " & .Name & " TableDef"
        ' Enumerate the Indexes collection of the specified
        ' TableDef object.
        For Each idxLoop In .Indexes
            Debug.Print "    " & idxLoop.Name
            Debug.Print "        Foreign = " & idxLoop.Foreign
        Next idxLoop
    End With

End Function
Example (Microsoft Access)

The following example prints the value of the Foreign property for each index in an Orders table:

Sub CheckForeign()
    Dim dbs As Database, tdf As TableDef, idx As Index

    ' Return reference to current database.
    Set dbs = CurrentDb
    ' Return reference to Orders table.
    Set tdf = dbs.TableDefs!Orders
    ' Enumerate through Indexes collection of table.
    For Each idx In tdf.Indexes
        ' Print value of Foreign property.
        Debug.Print idx.Name; "        "; idx.Foreign
    Next idx
    Set dbs = Nothing
End Sub