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, 10 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
-
To download Win 11 Pro 23H2 ISO. (Awaiting moderation)
by
Eddieloh
1 hour, 41 minutes ago -
Manage your browsing experience with Edge
by
Mary Branscombe
2 hours, 19 minutes ago -
Fewer vulnerabilities, larger updates
by
Susan Bradley
57 minutes ago -
Hobbies — There’s free software for that!
by
Deanna McElveen
2 hours, 21 minutes ago -
Apps included with macOS
by
Will Fastie
2 hours, 22 minutes ago -
Xfinity home internet
by
MrJimPhelps
7 hours, 11 minutes ago -
Convert PowerPoint presentation to Impress
by
RetiredGeek
9 hours, 11 minutes ago -
Debian 12.11 released
by
Alex5723
17 hours, 37 minutes ago -
Microsoft: Troubleshoot problems updating Windows
by
Alex5723
21 hours, 18 minutes ago -
Woman Files for Divorce After ChatGPT “Reads” Husband’s Coffee Cup
by
Alex5723
44 minutes ago -
Moving fwd, Win 11 Pro,, which is best? Lenovo refurb
by
Deo
17 hours, 46 minutes ago -
DBOS Advanced Network Analysis
by
Kathy Stevens
1 day, 14 hours ago -
Microsoft Edge Launching Automatically?
by
healeyinpa
1 day, 4 hours ago -
Google Chrome to block admin-level browser launches for better security
by
Alex5723
1 day, 16 hours ago -
iPhone SE2 Stolen Device Protection
by
Rick Corbett
1 day, 9 hours ago -
Some advice for managing my wireless internet gateway
by
LHiggins
16 hours, 42 minutes ago -
NO POWER IN KEYBOARD OR MOUSE
by
HE48AEEXX77WEN4Edbtm
9 hours, 34 minutes ago -
A CVE-MITRE-CISA-CNA Extravaganza
by
Nibbled To Death By Ducks
2 days, 2 hours ago -
Sometimes I wonder about these bots
by
Susan Bradley
1 day, 22 hours ago -
Does windows update component store “self heal”?
by
Mike Cross
1 day, 12 hours ago -
Windows 11 Insider Preview build 27858 released to Canary
by
joep517
2 days, 16 hours ago -
Pwn2Own Berlin 2025: Day One Results
by
Alex5723
1 day ago -
Windows 10 might repeatedly display the BitLocker recovery screen at startup
by
Susan Bradley
12 hours, 55 minutes ago -
Windows 11 Insider Preview Build 22631.5409 (23H2) released to Release Preview
by
joep517
2 days, 19 hours ago -
Windows 10 Build 19045.5912 (22H2) to Release Preview Channel
by
joep517
2 days, 19 hours ago -
Kevin Beaumont on Microsoft Recall
by
Susan Bradley
2 days, 7 hours ago -
The Surface Laptop Studio 2 is no longer being manufactured
by
Alex5723
3 days, 3 hours ago -
0Patch, where to begin
by
cassel23
2 days, 21 hours ago -
CFPB Quietly Kills Rule to Shield Americans From Data Brokers
by
Alex5723
3 days, 16 hours ago -
89 million Steam account details just got leaked,
by
Alex5723
3 days, 4 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.