Deleting multiple records using checkboxes as the record selector

Posted on 2010-01-02
Last Modified: 2013-11-28
I have a form with a subform.  The subform is set a as a  continuous form.  I'd like to have a check boxes that acts as a record selector within the subform and one delete button in the header.  I want to allow the user to select multiple records using the check box and delete them.

The checkbox is bound to a field "isSelected"  If a record cannot be deleted because there are child records associated then a message box to show which "checked" record(s) has child records that first need to be deleted.

Question by:latzo4
    LVL 31

    Accepted Solution

    Here is a SQL string that will do what you want; you run it from a command button in the header, substituting your table name (you should Requery the subform afterwards):
    Private Sub cmdDeleteSelectedRecords_Click()
       Dim strSQL As String
       Dim strTable As String
       strTable = "tblContacts"
       strSQL = "DELETE " & strTable & ".*, IsSelected " _
          & "From " & strTable & " WHERE IsSelected=True;"
       Debug.Print "SQL string: " & strSQL
       DoCmd.RunSQL strSQL
    End Sub

    Open in new window

    LVL 31

    Expert Comment

    Dealing with possible linked records is more complicated; see the sample database from my Access Archon #143 (on archiving records) for some code you can modify for your needs, substituting a Yes/No message box for the Debug.Print statement before deleting the "many" records.
    LVL 3

    Assisted Solution

    I found code and a sample db at this link that will delete multiple records with multiple Checkoxes.
    Simply select the multple boxes you want to delete and clik the button to delete them.

    The code that does the job (also from the sample db at the link) is in the code snippet below.
    For your convenience, please find a modified sample db attached below: #21335787
    Cheers !!!

    Private Sub btnRemove_Click()
        Dim strFilter As String
        Dim rs As Recordset, rc As Long
        Dim Msg, Style, Title, Help, Ctxt, Response, MyString
        strFilter = "toBeDeleted=" & True
        Me.Filter = strFilter
        Me.Filter = strFilter
        Me.FilterOn = True
        Set rs = Me.RecordsetClone
        rc = rs.RecordCount
        If rc = 0 Then
            Me.FilterOn = False
            Exit Sub
        End If
        Msg = CStr(rc) & " record/s to be deleted, " & "do you want to continue ?"    ' Define message.
        Style = vbYesNo + vbCritical + vbDefaultButton2    ' Define buttons.
        Title = "Delete Selected Records"    ' Define title.
        Response = MsgBox(Msg, Style, Title, Help, Ctxt)
        If Response = vbYes Then    ' User chose Yes.
            MyString = "Yes"    ' Perform some action.
            DoCmd.DoMenuItem acFormBar, acEditMenu, acSelectRecord
            DoCmd.SetWarnings False
            DoCmd.DoMenuItem acFormBar, acEditMenu, acDelete
            DoCmd.SetWarnings True
        Else    ' User chose No.
            MyString = "No"    ' Perform some action.
        End If
        Set rs = Nothing
        Me.FilterOn = False
    End Sub

    Open in new window


    Author Closing Comment

    Thanks for the help!

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
    When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now