By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,515 Members | 1,363 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,515 IT Pros & Developers. It's quick & easy.

Creating table relationships with code

P: n/a
Hello

Does anyone know if it is possible with Access 2000 to
create relationships between tables using code?

Thanks
G.Gerard
Nov 13 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Yes, here is an example:

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("MyMainTableMyRelatedTable")

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

'Add the fields to the relation.
'Field name in primary table.
Set fld = .CreateField("MyMainTableID")
'Field name in related table.
fld.ForeignName = "MyForeignKeyID"
'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.

"GGerard" <gg*****@nbnet.nb.ca> wrote in message
news:UG**********************@ursa-nb00s0.nbnet.nb.ca...
Hello

Does anyone know if it is possible with Access 2000 to
create relationships between tables using code?

Thanks
G.Gerard

Nov 13 '05 #2

P: n/a
Hello and thank you Allen Browne for your reply

As a follow up question can relationships be set between tables that reside
in two different mdb?

example : can a relationship be set between Table1 in First.mdb and Table2
in Second.mdb?
-how about if they are linked to a third mdb; can relationships(cascade
update and delete) be set between
Table1 and Table2 in this third mdb?

Thanks
G.Gerard

"Allen Browne" <Al*********@SeeSig.Invalid> wrote in message
news:41***********************@per-qv1-newsreader-01.iinet.net.au...
Yes, here is an example:

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("MyMainTableMyRelatedTable")

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

'Add the fields to the relation.
'Field name in primary table.
Set fld = .CreateField("MyMainTableID")
'Field name in related table.
fld.ForeignName = "MyForeignKeyID"
'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.

"GGerard" <gg*****@nbnet.nb.ca> wrote in message
news:UG**********************@ursa-nb00s0.nbnet.nb.ca...
Hello

Does anyone know if it is possible with Access 2000 to
create relationships between tables using code?

Thanks
G.Gerard


Nov 13 '05 #3

P: n/a
GGerard wrote:
Hello and thank you Allen Browne for your reply

As a follow up question can relationships be set between tables that reside
in two different mdb?

example : can a relationship be set between Table1 in First.mdb and Table2
in Second.mdb?
-how about if they are linked to a third mdb; can relationships(cascade
update and delete) be set between
Table1 and Table2 in this third mdb?


No. Even if it did do this it is not enforcable and would be easily
circumvented.

--

\\\\\\
\\ \\ Windows is searching
\ \ For your sig.
\ \ Please Wait.
\__\

Nov 13 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.