Hi, it’s your backward student again.
My latest assignment is to ” Delete all of the data in the _________ table.”
Well — I can’t do that because the table is in a relationship with other tables.
Can someone give me a hint on where to even start looking for a way to nullify the relationships so I can delete the records?
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
Changing Relationships Programmatically (2002)
Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Changing Relationships Programmatically (2002)
- This topic has 7 replies, 3 voices, and was last updated 20 years, 11 months ago.
AuthorTopicWSladygnome
AskWoody LoungerJune 28, 2004 at 11:37 pm #406747Viewing 1 reply threadAuthorReplies-
WSHansV
AskWoody LoungerJune 29, 2004 at 6:19 am #845700Although it is possible to change relationships in code, you must ask yourself if it is desirable. Let’s say that Table A has a primary key that is related to a foreign key in Table B. If you were to delete the relationship, then delete all records in Table A, the records in Table B would become orphaned: they have a foreign key that does not refere to any existing record in Table A. In most real-life situations that is undesirable.
One option is to first delete all records in Table B, and in all other tables with a foreign key that refers to the primary key in Table A. Only then delete the records in Table A.
Another option is to set Cascading Deletes for the relationships joining Table A to other tables. When you delete records from Table A, related records in other tables will automatically be deleted too.
If you need to manipulate relationships in code, the easiest way is to set a reference to the Microsoft DAO 3.6 Object Library, and work with the CurrentDb.Relations collection. -
WSladygnome
AskWoody LoungerJune 29, 2004 at 9:30 am #845740Hans darlin’
We aren’t talking about the real world. This is a class assignment. It is MOST desirable for me to be able to complete it. I have the code that will delete the table, and the code that would delete all records, but they fail because of the relationship, I really really need to break the relationship. Your absolutly correct – Microsoft DAO library and the CurrentDb is exactly what I’m using. — Wait a minute. You said CurrentDb Relations collection. — I’m off to research, Thank you. -
WSMarkD
AskWoody LoungerJune 29, 2004 at 3:02 pm #845834If this is for a class, isn’t asking for answer online cheatin’?? (joke)
Anyway for example of code to delete relationship(s) for a specified table, see attached text file. The DeleteTable sub tries to delete table, if Error 3281 results, table cannot be deleted because is used in relationship. In this event DeleteRelation sub called from error handler:
Public Sub DeleteRelation(ByRef TableName As String)
On Error GoTo Err_Handler
Dim db As DAO.Database
Dim n As Long
Dim i As Integer
Dim strMsg As String
Set db = CurrentDb
' Relations collection is zero-based
For n = db.Relations.Count - 1 To 0 Step -1
If db.Relations(n).Table = TableName Or _
db.Relations(n).ForeignTable = TableName Then
db.Relations.Delete db.Relations(n).Name
i = i + 1
End If
Next n
' Test msg:
If i > 0 Then
MsgBox i & " relations for " & TableName & " table have been deleted.", _
vbInformation, "RELATIONS DELETED"
Else
MsgBox "No relations found for this table (" & TableName & ").", vbInformation, "NO RELATIONS FOUND"
End If
Exit_Sub:
Set db = Nothing
Exit Sub
Err_Handler:
strMsg = "Error No " & Err.Number & ": " & Err.Description
MsgBox strMsg, vbExclamation, "DELETE RELATION ERROR MSG"
Debug.Print strMsg
Resume Exit_Sub
End Sub
This code deletes any relationship where table is involved as either primary or “foreign” table in relationship, as indicated by Relation object Table and ForeignTable properties. Run EnumRelations sub to list all relations in current db and table names (note Relation name is usually a composite of the names of two tables involved, but not always, so do not recommend use Relation name as basis for deletion). Note also, for this type of thing, step thru the collection from last item to first, or else things will get out of whack when you delete object & the remaining objects are re-indexed.
HTH
-
WSladygnome
AskWoody LoungerJune 29, 2004 at 8:39 pm #845972Ordinarily I would agree with this. Which is why I only asked for a hint. However, for this class — There are no lectures, there are no other students, the instructor responds little or not at all, (online class). Not all of the information needed is included in the assigned or previously assigned material. We are apparently supposed to research it and figure it out.
Besides, my friend, who is a supervisor at Microsoft (really), told me that this is the way programmers work in real life. They do what they know how to do, hit the books, and if they can’t find it there, ask other programmers. So I am not only learning VBA, I’m learning to work like a real programmer/
I’m apparently learning a little advanced rationalization as well.
Seriously, if my boss hadn’t let me take the Developers Handbook home, and I didn’t own one other reference besides the textbook, and if I hadn’t gotten serious hints and help from the lounge, this would have been total disaster.
Thanks guys, and Charlotte. -
WSladygnome
AskWoody LoungerJune 29, 2004 at 8:39 pm #845973Ordinarily I would agree with this. Which is why I only asked for a hint. However, for this class — There are no lectures, there are no other students, the instructor responds little or not at all, (online class). Not all of the information needed is included in the assigned or previously assigned material. We are apparently supposed to research it and figure it out.
Besides, my friend, who is a supervisor at Microsoft (really), told me that this is the way programmers work in real life. They do what they know how to do, hit the books, and if they can’t find it there, ask other programmers. So I am not only learning VBA, I’m learning to work like a real programmer/
I’m apparently learning a little advanced rationalization as well.
Seriously, if my boss hadn’t let me take the Developers Handbook home, and I didn’t own one other reference besides the textbook, and if I hadn’t gotten serious hints and help from the lounge, this would have been total disaster.
Thanks guys, and Charlotte.
-
-
-
WSladygnome
AskWoody LoungerJune 29, 2004 at 9:30 am #845741Hans darlin’
We aren’t talking about the real world. This is a class assignment. It is MOST desirable for me to be able to complete it. I have the code that will delete the table, and the code that would delete all records, but they fail because of the relationship, I really really need to break the relationship. Your absolutly correct – Microsoft DAO library and the CurrentDb is exactly what I’m using. — Wait a minute. You said CurrentDb Relations collection. — I’m off to research, Thank you.
-
-
WSHansV
AskWoody LoungerJune 29, 2004 at 6:19 am #845701Although it is possible to change relationships in code, you must ask yourself if it is desirable. Let’s say that Table A has a primary key that is related to a foreign key in Table B. If you were to delete the relationship, then delete all records in Table A, the records in Table B would become orphaned: they have a foreign key that does not refere to any existing record in Table A. In most real-life situations that is undesirable.
One option is to first delete all records in Table B, and in all other tables with a foreign key that refers to the primary key in Table A. Only then delete the records in Table A.
Another option is to set Cascading Deletes for the relationships joining Table A to other tables. When you delete records from Table A, related records in other tables will automatically be deleted too.
If you need to manipulate relationships in code, the easiest way is to set a reference to the Microsoft DAO 3.6 Object Library, and work with the CurrentDb.Relations collection.
Viewing 1 reply thread -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
Trump signs Take It Down Act
by
Alex5723
3 hours, 11 minutes ago -
Do you have a maintenance window?
by
Susan Bradley
3 hours, 36 minutes ago -
Freshly discovered bug in OpenPGP.js undermines whole point of encrypted comms
by
Nibbled To Death By Ducks
4 hours, 44 minutes ago -
Success Knows No End in the World of Online Slots (Awaiting moderation)
by
isobelhartmann1
4 hours, 53 minutes ago -
Cox Communications and Charter Communications to merge
by
not so anon
6 hours, 30 minutes ago -
Help with WD usb driver on Windows 11
by
Tex265
11 hours, 40 minutes ago -
hibernate activation
by
e_belmont
15 hours, 25 minutes ago -
Red Hat Enterprise Linux 10 with AI assistant
by
Alex5723
19 hours, 13 minutes ago -
Windows 11 Insider Preview build 26200.5603 released to DEV
by
joep517
22 hours, 18 minutes ago -
Windows 11 Insider Preview build 26120.4151 (24H2) released to BETA
by
joep517
22 hours, 19 minutes ago -
Fixing Windows 24H2 failed KB5058411 install
by
Alex5723
18 seconds ago -
Out of band for Windows 10
by
Susan Bradley
1 day, 3 hours ago -
Giving UniGetUi a test run.
by
RetiredGeek
1 day, 10 hours ago -
Windows 11 Insider Preview Build 26100.4188 (24H2) released to Release Preview
by
joep517
1 day, 17 hours ago -
Microsoft is now putting quantum encryption in Windows builds
by
Alex5723
3 hours, 40 minutes ago -
Auto Time Zone Adjustment
by
wadeer
1 day, 22 hours ago -
To download Win 11 Pro 23H2 ISO.
by
Eddieloh
1 day, 19 hours ago -
Manage your browsing experience with Edge
by
Mary Branscombe
1 day ago -
Fewer vulnerabilities, larger updates
by
Susan Bradley
12 hours, 47 minutes ago -
Hobbies — There’s free software for that!
by
Deanna McElveen
19 hours, 12 minutes ago -
Apps included with macOS
by
Will Fastie
17 hours, 3 minutes ago -
Xfinity home internet
by
MrJimPhelps
13 hours, 51 minutes ago -
Convert PowerPoint presentation to Impress
by
RetiredGeek
1 day, 15 hours ago -
Debian 12.11 released
by
Alex5723
2 days, 19 hours ago -
Microsoft: Troubleshoot problems updating Windows
by
Alex5723
2 days, 22 hours ago -
Woman Files for Divorce After ChatGPT “Reads” Husband’s Coffee Cup
by
Alex5723
2 days, 2 hours ago -
Moving fwd, Win 11 Pro,, which is best? Lenovo refurb
by
Deo
6 hours, 1 minute ago -
DBOS Advanced Network Analysis
by
Kathy Stevens
3 days, 15 hours ago -
Microsoft Edge Launching Automatically?
by
healeyinpa
3 days, 6 hours ago -
Google Chrome to block admin-level browser launches for better security
by
Alex5723
1 day, 3 hours ago
Recent blog posts
Key Links
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.