Article ID: 129879
Article Last Modified on 12/9/2003
Private Sub Command1_Click()
'-------------------------------------------------------------------
' PURPOSE: Clear all relations from destination table.
' This is used to later demonstrate importing relations.
'-------------------------------------------------------------------
Dim ThisDb As Database
Dim i As Integer
Set ThisDb = DBEngine.Workspaces(0)_
.OpenDatabase("C:\access\sampapps\nwind.mdb")
' Loop through all existing relations in that database:
For i = ThisDb.Relations.Count - 1 To 0 Step -1
Debug.Print i, ThisDb.Relations(i).Name
' Clear all relations to later demonstrate importing them:
ThisDb.Relations.Delete ThisDb.Relations(i).Name
Next
Debug.Print "#Relations on "; ThisDb.Name; " = ";_
ThisDB.Relations.Count
End Sub
Private Sub Command2_Click()
Call ImportRelations("C:\access\sampapps\nwind2.mdb")
End Sub
Sub ImportRelations(DBName As String)
'-------------------------------------------------------------------
' PURPOSE: Import relations where tablenames and fieldnames match.
' ACCEPTS: name of the database to import from as string.
' RETURNS: Number of relations imported as integer.
'--------------------------------------------------------------------
Dim ThisDb As Database, ThatDB As Database
Dim ThisRela As Relation, ThatRela As Relation
Dim ThisField As Field, ThatField As 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 = DBEngine.Workspaces(0)_
.OpenDatabase("C:\access\sampapps\nwind.mdb")
Set ThatDB = DBEngine.Workspaces(0).OpenDatabase(DBName$)
Debug.Print "Before import ..."
Debug.Print " "; ThisDb.Name; " has "; _
ThisDb.Relations.Count; " relations defined."
Debug.Print " "; ThatDB.Name; " has "; _
ThatDB.Relations.Count; " relations defined."
' Loop through all existing relations in that database:
For i = 0 To ThatDB.Relations.Count - 1
Set ThatRela = ThatDB.Relations(i)
' Create 'ThisRela' using values from 'ThatRela':
Set ThisRela = ThisDb.CreateRelation(ThatRela.Name, _
ThatRela.Table, ThatRela.ForeignTable, ThatRela.Attributes)
' Set bad field flag to false:
ErrBadField = False
' Loop through all fields in that relation:
For j = 0 To ThatRela.Fields.Count - 1
Set ThatField = ThatRela.Fields(j)
' Create 'ThisField' using values from 'ThatField':
Set ThisField = ThisRela.CreateField(ThatField.Name)
ThisField.ForeignName = ThatField.ForeignName
Next j
' If any field of this relation caused an error,
' then don't add this relation:
If ErrBadField = True Then
' Something went wrong with the fields.
' Don't do anything.
Else
' Try to append the relation:
On Error Resume Next
ThisDb.Relations.Append ThisRela
If Err <> False Then
' Something went wrong with the relation.
' Skip it.
Else
' Keep count of successful imports
RCount = RCount + 1
End If
On Error GoTo 0
End If
Next i
Debug.Print "After import ..."
Debug.Print " "; ThisDb.Name; " has "; _
ThisDb.Relations.Count; " relations defined."
Debug.Print " "; ThatDB.Name; " has "; _
ThatDB.Relations.Count; " relations defined."
' Close databases:
ThisDb.Close
ThatDB.Close
End SubAdditional query words: 4.00 vb4win vb4all
Keywords: kbcode KB129879