RELATION.ForeignTable 属性 (DAO)

适用于:Access 2013、Office 2013

设置或返回仅) (Microsoft Access 工作区的关系中的外部表的名称。 .

语法

表达式 。ForeignTable

表达 一个代表 Relation 对象的变量。

备注

对于尚未追加到集合的新 Relation 对象,该属性是可读写。的;对于 Relations 集合中的现有 Relation 对象,该属性是只读的。

Relation 对象的 ForeignTable 属性设置是代表外表或查询的 TableDefQueryDef 对象的 Name 属性设置; Table 属性设置是代表主表或查询的 TableDefQueryDef 对象的 Name 属性设置。

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

在此例中,ValidParts 表是主表,所以 Relation 对象的 Table 属性将设置为 ValidParts, Relation 对象的 ForeignTable 属性将设置为 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