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
-
Excessive security alerts
by
WSSebastian42
53 minutes ago -
* CrystalDiskMark may shorten SSD/USB Memory life
by
Alex5723
8 hours, 27 minutes ago -
Ben’s excellent adventure with Linux
by
Ben Myers
39 minutes ago -
Seconds are back in Windows 10!
by
Susan Bradley
19 minutes ago -
WebBrowserPassView — Take inventory of your stored passwords
by
Deanna McElveen
5 hours, 31 minutes ago -
OS news from WWDC 2025
by
Will Fastie
9 hours, 41 minutes ago -
Need help with graphics…
by
WSBatBytes
1 hour, 10 minutes ago -
AMD : Out of Bounds (OOB) read vulnerability in TPM 2.0 CVE-2025-2884
by
Alex5723
23 hours, 55 minutes ago -
Totally remove or disable BitLocker
by
CWBillow
22 hours, 48 minutes ago -
Windows 10 gets 6 years of ESU?
by
n0ads
2 hours, 18 minutes ago -
Apple, Google stores still offer China-based VPNs, report says
by
Nibbled To Death By Ducks
1 day, 10 hours ago -
Search Forums only bring up my posts?
by
Deo
1 day, 10 hours ago -
Windows Spotlight broken on Enterprise and Pro for Workstations?
by
steeviebops
1 day, 22 hours ago -
Denmark wants to dump Microsoft for Linux + LibreOffice
by
Alex5723
1 day, 15 hours ago -
How to get Microsoft Defender to honor Group Policy Setting
by
Ralph
1 day, 23 hours ago -
Apple : Paragon’s iOS Mercenary Spyware Finds Journalists Target
by
Alex5723
2 days, 9 hours ago -
Music : The Rose Room – It’s Been A Long, Long Time album
by
Alex5723
2 days, 10 hours ago -
Disengage Bitlocker
by
CWBillow
2 days ago -
Mac Mini M2 Service Program for No Power Issue
by
Alex5723
2 days, 12 hours ago -
New Win 11 Pro Geekom Setup questions
by
Deo
1 day, 10 hours ago -
Windows 11 Insider Preview build 26200.5651 released to DEV
by
joep517
2 days, 19 hours ago -
Windows 11 Insider Preview build 26120.4441 (24H2) released to BETA
by
joep517
2 days, 19 hours ago -
iOS 26,, MacOS 26 : Create your own AI chatbot
by
Alex5723
2 days, 23 hours ago -
New PC transfer program recommendations?
by
DaveBoston
1 day, 4 hours ago -
Windows 11 Insider Preview Build 22631.5545 (23H2) released to Release Preview
by
joep517
3 days, 3 hours ago -
Windows 10 Build 19045.6029 (22H2) to Release Preview Channel
by
joep517
3 days, 3 hours ago -
Best tools for upgrading a Windows 10 to an 11
by
Susan Bradley
2 days, 15 hours ago -
The end of Windows 10 is approaching, consider Linux and LibreOffice
by
Alex5723
1 day, 19 hours ago -
Extended Windows Built-in Disk Cleanup Utility
by
bbearren
2 days, 4 hours ago -
Win 11 24H2 June 2025 Update breaks WIFI
by
dportenlanger
3 days, 22 hours ago
Recent blog posts
- Ben’s excellent adventure with Linux
- Seconds are back in Windows 10!
- WebBrowserPassView — Take inventory of your stored passwords
- OS news from WWDC 2025
- Best tools for upgrading a Windows 10 to an 11
- Master patch listing for June 10, 2025
- 24H2 may not be offered June updates
- June 2025 updates are out
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.