Field.ForeignName 属性 (DAO)

适用于:Access 2013、Office 2013

设置或返回一个值,该值指定某个外表中的 Field 对象的名称,而该外表对应于某个关系的主表中的字段(仅适用于 Microsoft Access 工作区)。

语法

表达式 。ForeignName

表达式 一个表示 Field 对象的变量。

说明

如果 Relation 对象未追加到 Database ,但是 Field 已追加到 Relation 对象,则 ForeignName 属性是可读写的。 将 Relation 对象追加到数据库后, ForeignName 属性是只读的。

只有属于 Relation 对象 Fields 集合的 Field 对象才能支持 ForeignName 属性。

Field 对象的 NameForeignName 属性设置指定关系的主表和外表中相应字段的名称。 Relation 对象的 TableForeignTable 属性设置确定关系的主表和外部表。

例如,如果您有一个存储在 ValidParts 表中的有效部件代码列表(这些代码存储在名为 PartNo 的字段中),则可以与 OrderItem 表建立关系,这样的话,如果部件代码输入到 OrderItem 表中,它就会出现在 ValidParts 表中。 如果 ValidParts 表中不存在部件代码,并且您尚未将 Relation 对象的 Attributes 属性设置为 dbRelationDontEnforce,则会发生可捕获错误。

在这种情况下,ValidParts 表将是外表,因此 Relation 对象的 ForeignTable 属性将设置为 ValidParts, Relation 对象的 Table 属性将设置为 OrderItem。 Relation 对象的 Fields 集合中的 Field 对象的 NameForeignName 属性将设置为 PartNo。

示例

以下示例演示 TableForeignTableForeignName 属性如何定义两个表之间的 Relation 条件。

    Sub ForeignNameX() 
     
     Dim dbsNorthwind As Database 
     Dim relLoop As Relation 
     
     Set dbsNorthwind = OpenDatabase("Northwind.mdb") 
     
     Debug.Print "Relation" 
     Debug.Print " Table - Field" 
     Debug.Print " Primary (One) "; 
     Debug.Print ".Table - .Fields(0).Name" 
     Debug.Print " Foreign (Many) "; 
     Debug.Print ".ForeignTable - .Fields(0).ForeignName" 
     
     ' Enumerate the Relations collection of the Northwind 
     ' database to report on the property values of 
     ' the Relation objects and their Field objects. 
     For Each relLoop In dbsNorthwind.Relations 
     With relLoop 
     Debug.Print 
     Debug.Print .Name & " Relation" 
     Debug.Print " Table - Field" 
     Debug.Print " Primary (One) "; 
     Debug.Print .Table & " - " & .Fields(0).Name 
     Debug.Print " Foreign (Many) "; 
     Debug.Print .ForeignTable & " - " & _ 
     .Fields(0).ForeignName 
     End With 
     Next relLoop 
     
     dbsNorthwind.Close 
     
    End Sub