次の方法で共有


Database.CreateRelation メソッド (DAO)

適用先: Access 2013、Office 2013

新しい Relation オブジェクトを作成します (Microsoft Access ワークスペースのみ)。 .

構文

。CreateRelation(Name, Table, ForeignTable, Attributes)

Database オブジェクトを表す変数です。

パラメーター

名前

必須かどうか

データ型

説明

名前

省略可能

バリアント型 (Variant)

新しい Relation オブジェクトの一意の名前を表す、サブタイプが文字列型 (String) であるバリアント型 (Variant) の値。 有効なリレーション名の詳細については、Name プロパティを参照してください。

省略可能

バリアント型 (Variant)

リレーションにおける主テーブルの名前を表す、サブタイプが文字列型 (String) であるバリアント型 (Variant) の値。 Relation オブジェクトを追加する前にこのテーブルが存在していないと、実行時エラーが発生します。

ForeignTable

省略可能

バリアント型 (Variant)

リレーションにおける外部キー側のテーブルの名前を表す、サブタイプが文字列型 (String) であるバリアント型 (Variant) の値。 Relation オブジェクトを追加する前にこのテーブルが存在していないと、実行時エラーが発生します。

属性

省略可能

バリアント型 (Variant)

リレーションシップの種類に関する情報を格納している定数 (または定数の組み合わせ)。 詳細については、 Attributes プロパティを参照してください。

戻り値

Relation

注釈

Relation オブジェクトは、2 つの TableDef オブジェクトまたは QueryDef オブジェクト間のリレーションシップに関する情報を Microsoft Access データベース エンジンに渡します。 Attributes プロパティを使用して、参照整合性を実装できます。

CreateRelation メソッドの使用時に省略可能な引数を省略した場合は、新しいオブジェクトをコレクションに追加する前に適切な代入ステートメントを使用して、対応するプロパティを設定またはリセットできます。 オブジェクトの追加後は、プロパティの設定は一切変更できません。 詳細については、各プロパティのトピックを参照してください。

Relation オブジェクトで Append メソッドを使用する前に、適切な Field オブジェクトを追加して、主キーと外部キーのリレーションシップ テーブルを定義する必要があります。

name が既にコレクションのメンバーであるオブジェクトを参照している場合、または下位の Fields コレクションで指定された Field オブジェクト名が無効な場合は、Append メソッドを使用すると実行時エラーが発生します。

レプリケートされたテーブルとローカル テーブルの間にリレーションシップを設定したり、設定を変更することはできません。

Relations コレクションから Relation オブジェクトを削除するには、コレクションの Delete メソッドを使用します。

次の使用例は、 CreateRelation メソッドを使用して、Employees テーブルの TableDef オブジェクトと Departments と呼ばれる新しい TableDef オブジェクトの間に Relation オブジェクトを作成します。 また、新しい Relation オブジェクトを作成すると、必要な Indexes オブジェクト (社員テーブルの 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