How to create relationship in VBA?

  • Thread starter Thread starter Tran Hong Quang
  • Start date Start date
T

Tran Hong Quang

Hi,
Could someone give me a code sample to create a one-to-
many relationship between 2 tables?

Thks
Quang
 
you can look at example for Relation Object or CreateRelation Method section
of online help:

Set relNew = .CreateRelation("EmployeesDepartments", _
tdfNew.Name, tdfEmployees.Name, _

dbRelationUpdateCascade)
 
Sub CreateRelationDAO()
Dim db As DAO.Database
Dim rel As DAO.Relation
Dim fld As DAO.Field

'Initialize
Set db = CurrentDb()

'Create a new relation.
Set rel = db.CreateRelation("tblDaoContractortblDaoBooking")

'Define its properties.
With rel
'Specify the primary table.
.Table = "tblDaoContractor"
'Specify the related table.
.ForeignTable = "tblDaoBooking"
'Specify attributes for cascading updates and deletes.
.Attributes = dbRelationUpdateCascade + dbRelationDeleteCascade

'Add the fields to the relation.
'Field name in primary table.
Set fld = .CreateField("ContractorID")
'Field name in related table.
fld.ForeignName = "ContractorID"
'Append the field.
.Fields.Append fld

'Repeat for other fields if a multi-field relation.

End With

'Save the newly defined relation to the Relations collection.
db.Relations.Append rel

'Clean up
Set fld = Nothing
Set rel = Nothing
Set db = Nothing
Debug.Print "Relation created."
End Sub
 
Could someone give me a code sample to create a one-to-
many relationship between 2 tables?

There is a DDL solution in m.p.a.tablesdbdesign.

By the way, if you think you have to ask a question in more than one NG,
please use crossposting (sending one message to all the groups) rather than
multiposting (sending separate messages to each one) because it saves
everybody time.

Tim F
 
Back
Top