This example assumes a main table named "tblContractor" with primary key
"ContractorID", and a related table "tblBooking" with a foreign key named
"ContractorID". The result is that one contractor can have many bookings.
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("tblContractortblBooking")
'Define its properties.
With rel
'Specify the primary table.
.Table = "tblContractor"
'Specify the related table.
.ForeignTable = "tblBooking"
'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
--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users -
http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.
"nicolaas" <go****@ngaru.com> wrote in message
news:28*************************@posting.google.co m...
Hi everyone
Is there someone out there who can tell me how to create relationships
between tables using VB???
PS I am using MS ACCESS 2003 and I have read the help...
THANK YOU