Option Compare Database
Option Explicit
Const adhcErrObjectExists = 3012
'Modified from:
' From Access 2000 Developer's Handbook
' by Getz, Litwin, and Gilbert. (Sybex)
' Copyright 1999. All Rights Reserved.
Function CreateRelationship() As Boolean
Dim db As DAO.Database
Dim rel As DAO.Relation
Dim fld As DAO.Field
Dim strTablePrimary As String
Dim varTableListMany As Variant
Dim varTableListOne As Variant
Dim varTableName As Variant
Dim i As Variant
On Error GoTo CreateRelationship_Err
Set db = CurrentDb()
'List of Tables that have a one to Many relationship with data_WellHeader table
varTableListMany = Array("data_WellContacts", "data_BH_Images", "data_CasingAndCement", "data_DistributionEmails", _
"data_FormationTops", "data_GeoHazard", "data_LogEval", "data_LogEvalDetail", _
"data_LogVendorContacts", "data_MudProgram")
'List of Tables that have a one to one relationsip with data_WellHeader table
varTableListOne = Array("data_PermitsAndRegulatory", "data_DirectionalSpecs", "data_DrillingSpecs", _
"data_Pinedale_PipeSetCriteria ")
strTablePrimary = "data_WellHeader"
'Create the Relationships that have a one to many relationsip with data_WellHeader table
For Each varTableName In varTableListMany
i = varTableName
' Create the new relation object.
Set rel = db.CreateRelation()
With rel
.Name = strTablePrimary & "_" & varTableName
.Table = strTablePrimary
.ForeignTable = varTableName
.Attributes = dbRelationUpdateCascade Or dbRelationDeleteCascade
End With
' Set the relation's field collection.
Set fld = rel.CreateField("API")
' What field does this map to in the OTHER table?
fld.ForeignName = "API"
rel.Fields.Append fld
' You could append more fields, if you needed to.
' Append the relation to the database's
' relations collection.
db.Relations.Append rel
CreateRelationship = True
db.Relations.Refresh
'Debug.Print varTableName, " " & rel.Name & "one to Many"
Next varTableName
For Each varTableName In varTableListOne
i = varTableName
' Create the new relation object.
Set rel = db.CreateRelation()
With rel
.Name = strTablePrimary & "_" & varTableName
.Table = strTablePrimary
.ForeignTable = varTableName
.Attributes = dbRelationUnique Or dbRelationUpdateCascade Or dbRelationDeleteCascade
End With
' Set the relation's field collection.
Set fld = rel.CreateField("API")
' What field does this map to in the OTHER table?
fld.ForeignName = "API"
rel.Fields.Append fld
' You could append more fields, if you needed to.
' Append the relation to the database's
' relations collection.
db.Relations.Append rel
CreateRelationship = True
db.Relations.Refresh
'Debug.Print varTableName, " " & rel.Name & " One to One"
Next varTableName
'keeps from duplicating a table in the relationship view and handles a special realationship
Call RELATIONSHIP_DELETE
Call DATABASE_RELATIONSHIP_ADD
CreateRelationship_Exit:
Exit Function
CreateRelationship_Err:
Select Case Err.Number
Case adhcErrObjectExists
' If the relationship already exists,
' just delete it, and then try to
' append it again.
db.Relations.Delete rel.Name
Resume
Case Else
MsgBox "Error: " & Err.Description & _
" (" & Err.Number & ")"
CreateRelationship = False
Resume CreateRelationship_Exit
End Select
End Function