Microsoft Access
--
Questions
--
Followers
Top Experts
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.
Code similar to this:
Sample db is attached
JeffCoachman
Database97.mdb
       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
Sample db is attached
JeffCoachman
Database97.mdb
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
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Works like a charm! Thank you very much for your help. I really appreciate it!






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
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.
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
--
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.