Avatar of SteveL13
SteveL13
Flag for United States of America asked on

Delete record if no records exist in sub-form

I have a form with a sub-form.  If the user has not entered any records in the sub-form and clicks the [Close Form], (a customer command button on the main form), I want the main record to be deleted without warning.

How can I do this?
Microsoft Access

Avatar of undefined
Last Comment
Jeffrey Coachman

8/22/2022 - Mon
Ryan Chong

try:

Private Sub  btnClose_Click()
   ID = Me.txtID.Value 'or something you can refer to present the unique main record
   If Me.yourSubform.Form.RecordsetClone.RecordCount = 0 Then
      CurrentDB.execute "Delete from yourMaintable where id = " & ID
  End If
End Sub
ASKER CERTIFIED SOLUTION
Gustav Brock

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Jeffrey Coachman

Note that things like this go against basic referential integrity rules, and may be hard to control when your design becomes more complex.

In a standard situation it is fairly common for a Parent record to be created and not have any Child records.
(The problem arises if you have child records with no parent (orphaned records))
For example, A person will become a Patient and not necessarily make an appointment for a "Visit" for a few weeks.
You can become a student at a school and not register for classes until the next semester.

Your users may wonder why a customer they created yesterday, ...is not showing up today?

Is this new "rule" to be universal with all Parent/Child relationships?
If not, then it will be confusing to users as to when a parent record will persist, and when it will "disappear"

As usual, an explanation of why this is needed is always helpful.
Again, deleting Parent records automatically (and without notice) is something that is not commonly done.

JeffCoachman
SteveL13

ASKER
I do understand that this is highly unusual.  But in this case, if the user starts a record on the main form and does nothing with the child form, he/she wants the main record to be deleted.

Strange but true.

--Steve
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Jeffrey Coachman

Ok, then what the experts above posted, ...should get you on your way...
;-)