Link to home
Create AccountLog in
Microsoft Access

Microsoft Access

--

Questions

--

Followers

Top Experts

Avatar of Lawrence Salvucci
Lawrence Salvucci🇺🇸

Delete All Records on a Continuous Subform via delete button on main form
I need to create some vba code for my command button on my main form that will delete all records on my continuous subform. Can anyone shed some light on how I can do this via VBA code? I don't want to delete the record on my main form, just all the records on the subform.

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of Jeffrey CoachmanJeffrey Coachman🇺🇸

Code similar to this:

       If MsgBox("Delete all subform records, ...are you sure?", vbQuestion + vbYesNo) = vbYes Then
        DoCmd.RunCommand acCmdSaveRecord
        CurrentDb.Execute "DELETE * FROM YourChildTable WHERE CustID = " & Forms!YourParentForm!txtCustID, dbFailOnError
        Forms!YourParentForm![YourChildForm].Requery
    Else
        Exit Sub
    End If

Open in new window


Sample db is attached

JeffCoachman
Database97.mdb

Avatar of Jeffrey CoachmanJeffrey Coachman🇺🇸

This code would be a bit neater:

   'Save the current record, ...to be safe
    DoCmd.RunCommand acCmdSaveRecord
    'User confirmation
    If MsgBox("Delete all subform records, ...are you sure?", vbQuestion + vbYesNo) = vbYes Then
        'Delete the records
        CurrentDb.Execute "DELETE * FROM YourChildTable WHERE CustID = " & Forms!YourParentForm!txtCustID, dbFailOnError
        'Requery the subform to avoid displaying "DELETED" for the deleted records
        Forms!YourParentForm![YourChildForm].Requery
    Else
        Exit Sub
    End If

ASKER CERTIFIED SOLUTION
Avatar of Jeffrey CoachmanJeffrey Coachman🇺🇸

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of Lawrence SalvucciLawrence Salvucci🇺🇸

ASKER

Works like a charm! Thank you very much for your help. I really appreciate it!

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of Jeffrey CoachmanJeffrey Coachman🇺🇸

Great thanks,

To me the key is the confirmation.

Remember that in Access when you delete records, ...there is no undo.
So you need to force users to confirm the deletion.
Microsoft Access

Microsoft Access

--

Questions

--

Followers

Top Experts

Microsoft Access is a rapid application development (RAD) relational database tool. Access can be used for both desktop and web-based applications, and uses VBA (Visual Basic for Applications) as its coding language.