Microsoft KB Archive/210302

= ACC2000: How to Use DAO in Visual Basic Code to Import Relationships =

Article ID: 210302

Article Last Modified on 6/24/2004

-

APPLIES TO


 * Microsoft Access 2000 Standard Edition

-



This article was previously published under Q210302



Advanced: Requires expert coding, interoperability, and multiuser skills.

This article applies only to a Microsoft Access database (.mdb).



SUMMARY
This article shows you how to use Data Access Objects (DAO) in Visual Basic for Applications code to read the relationships from an external Microsoft Access database and to import those relationships that can be successfully appended to the current database.



MORE INFORMATION
NOTE: The sample code in this article uses Microsoft Data Access Objects. For this code to run properly, you must reference the Microsoft DAO 3.6 Object Library. To do so, click References on the Tools menu in the Visual Basic Editor, and make sure that the Microsoft DAO 3.6 Object Library check box is selected.

When you try to append relationships in the current database, you can append only those relationships that have table names and field names that match those in the current database.

The following example demonstrates how to use the Relation objects in DAO to import the appropriate relationships into the current database:  Create a new database called ImpRel.mdb. Import the following tables one at a time from the sample database Northwind.mdb to the ImpRel database:  Products Customers Orders Order Details</li></ul>

NOTE: If you import the tables in one operation, Microsoft Access automatically imports the relationship between the tables as well.

</li> Create a new module, and then type the following line in the Declarations section if it is not already there:

Option Explicit

</li> On the Tools menu, click References, and then click to select the Microsoft DAO 3.6 Object Library check box. Click OK.</li>  Type or paste the following procedure: Function ImportRelations(DbName As String) As Integer '-- ' PURPOSE: Imports relationships where table names and field names '         match. ' ACCEPTS: The name of the external database as a string. ' RETURNS: The number of relationships imported as an integer. '--

Dim ThisDb As DAO.Database, ThatDB As DAO.Database Dim ThisRel As DAO.Relation, ThatRel As DAO.Relation Dim ThisField As DAO.Field, ThatField As DAO.Field Dim Cr As String, i As Integer, cnt As Integer, RCount As Integer Dim j As Integer Dim ErrBadField As Integer

Cr$ = Chr$(13) RCount = 0

Set ThisDb = CurrentDb Set ThatDB = DBEngine.Workspaces(0).OpenDatabase(DbName$)

' Loop through all existing relationships in the external database. For i = 0 To ThatDB.Relations.Count - 1 Set ThatRel = ThatDB.Relations(i)

' Create 'ThisRel' using values from 'ThatRel'. Set ThisRel = ThisDb.CreateRelation(ThatRel.Name, _     ThatRel.table, ThatRel.foreigntable, ThatRel.Attributes)

' Set bad field flag to false. ErrBadField = False

' Loop through all fields in that relation. For j = 0 To ThatRel.Fields.Count - 1 Set ThatField = ThatRel.Fields(j)

' Create 'ThisField' using values from 'ThatField'. Set ThisField = ThisRel.CreateField(ThatField.Name) ThisField.foreignname = ThatField.foreignname

' Check for bad fields. On Error Resume Next ThisRel.Fields.Append ThisField If Err <> False Then ErrBadField = True On Error GoTo 0 Next j

' If any field of this relationship caused an error, ' do not add this relationship. If ErrBadField = True Then ' Something went wrong with the fields. ' Do not do anything. Else ' Try to append the relation. On Error Resume Next ThisDb.Relations.Append ThisRel If Err <> False Then ' Something went wrong with the relationship. ' Skip it. Else ' Keep count of successful imports. RCount = RCount + 1 End If     On Error GoTo 0 End If Next i

' Close databases. ThisDb.Close ThatDB.Close

' Return number of successful imports. ImportRelations = RCount

End Function </li> Save the module as DAOExample and close it.</li>  Create the following new macro:

NOTE: In the following macro expression, an underscore (_) at the end of a line is used as a line-continuation character. Remove the underscore from the end of the line when re-creating this macro.

<pre class="fixed_text">     Macro Name           Macro Actions --     DAOImportRelations   RunCode MsgBox

DAOImportRelations Actions and Action Arguments ---     RunCode Function Name: ImportRelations("C:\Program Files\Microsoft _                       Office\Office\Samples\Northwind.mdb") MsgBox Message: All Done.

NOTE: If your copy of the sample database Northwind.mdb is not installed in the Program Files\Microsoft Office\Office\Samples folder (directory) on drive C, substitute the correct drive and path in the Function Name argument in the macro.

</li> Save the macro as DAOImportRelations, and then close it.</li> Run the macro. The three relationships defined among the four tables that you imported from the Northwind database are imported. The other four relationships in the Northwind database are not imported.</li> To see the relationships in the current database, select the Database window, and then on the Tools menu, click Relationships. All the tables and relationships are displayed in the window. The relationships among the tables are drawn automatically.</li></ol>

<div class="references_section">