Database.CreateRelation 方法 (DAO)
适用于:Access 2013、Office 2013
仅) (Microsoft Access 工作区创建新的 Relation 对象。 .
语法
表达式 。CreateRelation (名称、 表、 ForeignTable、 特性)
表达式 一个表示 Database 对象的变量。
参数
名称 |
必需/可选 |
数据类型 |
说明 |
---|---|---|---|
Name |
可选 |
Variant |
一个 Variant(String 子类型),用于对新的 Relation 对象进行唯一命名。 有关有效关系名称的详细信息,请参阅 Name 属性。 |
Table |
可选 |
Variant |
一个 Variant(String 子类型),用于命名关系中的主表。 如果在追加 Relation 对象之前该表不存在,将发生运行时错误。 |
ForeignTable |
可选 |
Variant |
一个 Variant(String 子类型),用于命名关系中的外表。 如果在追加 Relation 对象之前该表不存在,将发生运行时错误。 |
Attributes |
可选 |
Variant |
一个常量或常量组合,包含有关关系类型的信息。 有关详细信息,请参阅 Attributes 属性。 |
返回值
Relation
备注
Relation 对象向 Microsoft Access 数据库引擎提供有关两个 TableDef 对象或 QueryDef 对象中的字段之间的关系的信息。 可以通过使用 Attributes 属性来实现参照完整性。
如果使用 CreateRelation 方法时省略了一个或多个可选部分,则可以在将新对象追加到集合之前,使用适当的赋值语句设置或重置相应的属性。 追加对象后,不能改动此对象的任何属性设置。 有关详细信息,请参阅各个属性主题。
在对 Relation 对象使用 Append 方法之前,必须追加相应的 Field 对象以定义主键和外键关系表。
如果 name 引用的对象已是集合的成员,或者如果从属 Fields 集合中提供的 Field 对象名称无效,则使用 Append 方法时将发生运行时错误。
不能在复制表和本地表之间建立或维持关系。
若要从 Relations 集合中删除 Relation 对象,请对集合使用 Delete 方法。
示例
以下示例使用 CreateRelation 方法在 Employees TableDef 和名为 Departments 的新的 TableDef 之间创建一个 Relation。 该示例还演示创建新的 Relation 将如何同时在外部表中创建任何需要的 Indexes(Employees 表中的 DepartmentsEmployees 索引)。
Sub CreateRelationX()
Dim dbsNorthwind As Database
Dim tdfEmployees As TableDef
Dim tdfNew As TableDef
Dim idxNew As Index
Dim relNew As Relation
Dim idxLoop As Index
Set dbsNorthwind = OpenDatabase("Northwind.mdb")
With dbsNorthwind
' Add new field to Employees table.
Set tdfEmployees = .TableDefs!Employees
tdfEmployees.Fields.Append _
tdfEmployees.CreateField("DeptID", dbInteger, 2)
' Create new Departments table.
Set tdfNew = .CreateTableDef("Departments")
With tdfNew
' Create and append Field objects to Fields
' collection of the new TableDef object.
.Fields.Append .CreateField("DeptID", dbInteger, 2)
.Fields.Append .CreateField("DeptName", dbText, 20)
' Create Index object for Departments table.
Set idxNew = .CreateIndex("DeptIDIndex")
' Create and append Field object to Fields
' collection of the new Index object.
idxNew.Fields.Append idxNew.CreateField("DeptID")
' The index in the primary table must be Unique in
' order to be part of a Relation.
idxNew.Unique = True
.Indexes.Append idxNew
End With
.TableDefs.Append tdfNew
' Create EmployeesDepartments Relation object, using
' the names of the two tables in the relation.
Set relNew = .CreateRelation("EmployeesDepartments", _
tdfNew.Name, tdfEmployees.Name, _
dbRelationUpdateCascade)
' Create Field object for the Fields collection of the
' new Relation object. Set the Name and ForeignName
' properties based on the fields to be used for the
' relation.
relNew.Fields.Append relNew.CreateField("DeptID")
relNew.Fields!DeptID.ForeignName = "DeptID"
.Relations.Append relNew
' Print report.
Debug.Print "Properties of " & relNew.Name & _
" Relation"
Debug.Print " Table = " & relNew.Table
Debug.Print " ForeignTable = " & _
relNew.ForeignTable
Debug.Print "Fields of " & relNew.Name & " Relation"
With relNew.Fields!DeptID
Debug.Print " " & .Name
Debug.Print " Name = " & .Name
Debug.Print " ForeignName = " & .ForeignName
End With
Debug.Print "Indexes in " & tdfEmployees.Name & _
" TableDef"
For Each idxLoop In tdfEmployees.Indexes
Debug.Print " " & idxLoop.Name & _
", Foreign = " & idxLoop.Foreign
Next idxLoop
' Delete new objects because this is a demonstration.
.Relations.Delete relNew.Name
.TableDefs.Delete tdfNew.Name
tdfEmployees.Fields.Delete "DeptID"
.Close
End With
End Sub