Compartir a través de


Método Database.CreateRelation (DAO)

Se aplica a: Access 2013, Office 2013

Crea un nuevo objeto Relation (solo áreas de trabajo de Microsoft Access). .

Sintaxis

expresión . CreateRelation(Name, Table, ForeignTable, Attributes)

expression Variable que representa un objeto Database.

Parameters

Nombre

Obligatorio/opcional

Tipo de datos

Descripción

Name

Opcional

Variant

Variant (subtipo String) que designa inequívocamente el nuevo objeto Relation. Consulte la propiedad Name para obtener más información sobre los nombres de relación válidos.

Tabla

Opcional

Variant

Variant (subtipo String) que designa la tabla principal de la relación. Si la tabla no existe antes de agregar el objeto Relation, se produce un error en tiempo de ejecución.

ForeignTable

Opcional

Variant

Variant (subtipo String) que designa la tabla externa de la relación. Si la tabla no existe antes de agregar el objeto Relation, se produce un error en tiempo de ejecución.

Atributos

Opcional

Variant

Constante o combinación de constantes que contienen información sobre el tipo de relación. Consulte la propiedad Attributes para obtener más información.

Valor devuelto

Relation

Comentarios

El objeto Relation proporciona información al motor de base de datos de Microsoft Access sobre la relación entre los campos de dos objetos TableDef o QueryDef. Puede implementar integridad referencial mediante la propiedad Attributes.

Si omite uno o varios de los argumentos opcionales cuando utiliza el método CreateRelation, puede usar la instrucción de asignación pertinente para establecer o restablecer la propiedad correspondiente antes de agregar el nuevo objeto a una colección. Después de agregar el objeto, no podrá modificar todos los valores de la propiedad. Vea los temas correspondientes a cada propiedad para obtener información más detallada.

Antes de utilizar el método Append en un objeto Relation, debe agregar los objetos Field correspondientes para definir las tablas principal y externa de la relación.

Si name hace referencia a un objeto que ya es miembro de la colección o si los nombres de objeto Field proporcionados en la colección Fields subordinada no son válidos, se produce un error en tiempo de ejecución cuando se usa el método Append .

No se puede establecer ni mantener una relación entre una tabla replicada y una tabla local.

Para quitar un objeto Relation de la colección Relations, utilice el método Delete en la colección.

Ejemplo

En este ejemplo se utiliza el método CreateRelation para crear una Relation entre el objeto TableDef de empleados y un nuevo objeto TableDef denominado Departments (departamentos). En este ejemplo se muestra también cómo al crear una nueva Relation se crean también los Indexes necesarios en la tabla externa (el índice DepartmentsEmployees de la tabla Employees).

    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