Connecting Tech Pros Worldwide Help | Site Map

relations

  #1  
Old November 12th, 2005, 10:56 PM
nicolaas
Guest
 
Posts: n/a
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
  #2  
Old November 12th, 2005, 10:56 PM
Allen Browne
Guest
 
Posts: n/a

re: relations


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" <google@ngaru.com> wrote in message
news:28ac5d7.0405032021.24756b12@posting.google.co m...[color=blue]
> 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[/color]


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Q: deleting relations G .Net answers 5 August 25th, 2006 12:25 PM
Retrieve info from Dataset with Relations (Dataset,SQL or XML) Joe answers 2 April 4th, 2006 02:15 PM
Walk thru all relations in dataset Joe answers 2 April 4th, 2006 01:45 PM
Multiple Key Relations Randy Fraser answers 1 November 21st, 2005 02:22 PM
Relations not visible Jef De Rycke answers 1 November 12th, 2005 07:51 PM