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?
SteveL13Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongCommented:
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
0
Gustav BrockCIOCommented:
You can just delete the record:
Private Sub Form_Unload(Cancel As Integer)

    If Me!NameOfSubformControl.Form.RecordsetClone.RecordCount = 0 Then
        DoCmd.RunCommand acCmdDeleteRecord
    End If

End Sub

Also, to prevent the dialogue box:

Private Sub Form_BeforeDelConfirm(Cancel As Integer, Response As Integer)

    Response = acDataErrContinue

End Sub

Open in new window

/gustav
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jeffrey CoachmanMIS LiasonCommented:
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
0
SteveL13Author Commented:
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
0
Jeffrey CoachmanMIS LiasonCommented:
Ok, then what the experts above posted, ...should get you on your way...
;-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.