Use VBA to check for existing relationship record (1 Viewer)

Saphirah

Active member
Local time
Today, 12:16
Joined
Apr 5, 2020
Messages
163
Hey everyone,

we all know that little message that pops up when you want to delete a record, but the record is connected/in a relationship with another existing record.
"The record cannot be deleted or changed, because table X includes related records".

Now for this i imagine access doing some checks, and these are usually really fast.

I have a table X with records, and i want to the user be able to press a button to delete this record in a form. Once in a while there is the popup message refered above.
Now my boss gave me the task to hide this button, whenever the record is not deletable.
Is there any way in vba to easily check if there is a related record using my current record? I do not want to use DCount or similar, because the Database may change in the future and i do not want to hardcode this, so there are not any problems in the future

Thank you very much
 

smtazulislam

Member
Local time
Today, 13:16
Joined
Mar 27, 2020
Messages
806
Your X table are relationship to others (tblY) :
its happens when a tblX ID (PK) to connection tblY (FK). and if your have SUBFORM added.
But you can try to delete same form where both table connection with subform.
And best ways wait for an experts.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 03:16
Joined
Oct 29, 2018
Messages
21,454
Hi. What do you mean by not wanting to use DCount()? How else can you "count" if there are any child records? If you simply want to "delete" all child records when you delete the parent record, then you could set Cascade Delete with your RI.
 

plog

Banishment Pending
Local time
Today, 05:16
Joined
May 11, 2011
Messages
11,638
i do not want to hardcode

Using a constant that you must update is hardcoding. Using a built in function is simply coding. Can you better explain your reservation about DCount?
 

Users who are viewing this thread

Top Bottom