This example assumes a main table named "tblContrac tor" with primary key
"Contractor ID", and a related table "tblBooking " with a foreign key named
"Contractor ID". The result is that one contractor can have many bookings.
Sub CreateRelationD AO()
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.CreateRelati on("tblContract ortblBooking")
'Define its properties.
With rel
'Specify the primary table.
.Table = "tblContrac tor"
'Specify the related table.
.ForeignTable = "tblBooking "
'Specify attributes for cascading updates and deletes.
.Attributes = dbRelationUpdat eCascade + dbRelationDelet eCascade
'Add the fields to the relation.
'Field name in primary table.
Set fld = .CreateField("C ontractorID")
'Field name in related table.
fld.ForeignName = "Contractor ID"
'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.Ap pend 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.c om> wrote in message
news:28******** *************** **@posting.goog le.com...
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