473,769 Members | 2,382 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Creating table relationships with code

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
3 11732
Yes, here is an example:

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("MyMainTable MyRelatedTable" )

'Define its properties.
With rel
'Specify the primary table.
.Table = "MyMainTabl e"
'Specify the related table.
.ForeignTable = "MyRelatedTable "
'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("M yMainTableID")
'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.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.

"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
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(c ascade
update and delete) be set between
Table1 and Table2 in this third mdb?

Thanks
G.Gerard

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

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("MyMainTable MyRelatedTable" )

'Define its properties.
With rel
'Specify the primary table.
.Table = "MyMainTabl e"
'Specify the related table.
.ForeignTable = "MyRelatedTable "
'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("M yMainTableID")
'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.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.

"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
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(c ascade
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5139
by: Jeremy Weiss | last post by:
Thanks to much help from everyone in my previous thread, I've made it a pretty fair ways into my billing/invoicing db. I'm now needing a way to cycle through all the records in a table and create invoice records for them in another table, but I have no idea where to begin. Here's the relevant tables: ============== Table: Customer_tbl
2
2227
by: Andrea | last post by:
I'm having some difficulty creating a report in Access and I need some suggestions. My company issues "Return Authorizations" when customers need to return products. A customer calls in and we provide the number. The customer then returns items for a refund or for an exchange. Here is the basic set up of the tables in question. -ReturnAuthorizationTable- ReturnNumber_PK CustomerID ReturnType
8
4329
by: Brian S. Smith | last post by:
Hi gang, Please help. I've been through the Access help, searched the Web, and I can't seem to get a straight answer. As the Subject line suggests, I want to run a fairly simple VB/Access Sub Function/Module that creates relationships for my tables. The problem is that I need to provide for some tables that may have > 32 relationships (which is apparently the limit on Indexes that Access can support). How can I prevent Access from...
4
2027
by: Ronnie | last post by:
Ok let me just say first that I am a newbie in Access and I don't know much of SQL or VB programming. But I am trying to create this contact database using Access 97. I have created 2 tables, one Personal (it has all the personal information) and the other one Organization (this one has details of diff organizations). One person can belong to more than any (upto 5 in this case) organizations. So I included orgid1, orgid2, orgid3, orgid4...
5
4703
by: Mike Turco | last post by:
What is the difference between creating relationships in the front-end vs. the back-end database? I was trying to create a relationship in a database front-end and noticed that I could not check the referential integrity box. What gives? Continuing on with that line of thinking, I understand what do the relationships do for you in a database, but what do they do physically to the tables? Thanks,
2
6125
by: Gandalf | last post by:
I'm creating relationships between tables using VBA and ADOX. I can create one-to-one relationships with an inner join, but I can't figure out how to create these relationships with an outer join (specifically a left outer join). I'm including the code that creates the relationships with the inner join. Any help or suggestions would be greatly appreciated. Thanks! <----- CODE FOLLOWS ----->
117
18572
by: phil-news-nospam | last post by:
Is there really any advantage to using DIV elements with float style properies, vs. the old method of TABLE and TR and TD? I'm finding that by using DIV, it still involves the same number of elements in the HTML to get everything just right. When you consider the class attribute on the DIV elements, there's not much size savings anymore for using DIV. There are other disadvantages to not using TABLE/TR/TD, such as the lack of ability...
5
1864
by: Kosmos | last post by:
Hey :) hopefully someone can help me with this...I decided to take on the task of programming an access database for my legal co-op/internship...I'm studying law and music production on the side...most of the background I have in programming has to do with music production... The program I'm creating pulls data from an excel sheet (with defined fields) and brings them into an access database. The data being pulled is the following: ...
10
4456
by: Richard | last post by:
Hi folks, thanks for taking the time to read this (and hopefully point our where I'm going wrong). The scenario: I have a local Access2007 database which links in several read only mySql tables via ODBC. The problem:
0
9586
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10043
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9990
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7406
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3956
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2814
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.