简体   繁体   中英

Delete multiple selected record from a multiselect listbox (Access)

I need to delete multiple records selected from a listbox in Access. I have SQL and VBA to delete one selected record from a non-multiselect listbox, but I can't seem to adapt the code to delete multiple selected records from a multiselect listbox. Here's the code as it stands; it's attached to a button On_Click event and the listbox is called listboxname:

Private Sub DeleteSelected_Click()
    Dim lngID As Long
    Dim strSQL As String

    If IsNull(ListBoxName) Then
        Exit Sub
    End If

    With Me.ListBoxName

    lngID = ListBoxName.Value

    strSQL = "DELETE * FROM [tablename] WHERE [tablefieldname] =" & lngID

    CurrentDb.Execute strSQL

    ListBoxName.Requery

    End With
End Sub

I tried setting

lngID = ListBoxName.Value

to

lngID = ListBoxName.ItemsSelected(0)

or

lngID = ListBoxName.Columns(0)

to no avail. It seems like the SQL must be at fault, but I haven't a clue how to fix it. Any ideas?

EDIT: That's very cool :) Cheers Belial. Here is the updated code

    Private Sub DeleteSelected_Click()
    Dim strSQL As String
    Dim vItem As Variant
    Dim strSet As Long

    If IsNull(ListBoxName) Then
        Exit Sub
    End If

    With Me.ListBoxName
      For Each vItem In .ItemsSelected
                If Not IsNull(vItem) Then
                    strSet = strSet & "," & .ItemData(vItem)
                End If
            Next
        End With

    strSQL = "DELETE FROM Carers WHERE Carer_ID IN (" & strSet & ")"

    CurrentDb.Execute strSQL

    ListBoxName.Requery

    End Sub

Hope I'm doing this right

Here is one SQL error in your query:

strSQL = "DELETE FROM [tablename] WHERE [tablefieldname] =" & lngID

There's no need for * .

Anyway, here is another way you can do:

Get select items into a String separated by , comma, then use IN operator to delete all at once.

Dim vItem as Variant
Dim strSet as String
//Loop through the ItemsSelected in the list box    
    With Me.ListBoxName
        For Each vItem In .ItemsSelected
            If Not IsNull(vItem) Then
                strSet = strSet & "," & .ItemData(vItem) 
            End If
        Next
    End With
strSQL = "DELETE FROM [tablename] WHERE [tablefieldname] IN (" & strSet & ")"

Remove the * from the DELETE Statement.

strSQL = "DELETE FROM [tablename] WHERE [tablefieldname] =" & lngID

to iterate through the selected items use something like

For i = 0 To ListBox1.ListCount - 1
      If ListBox1.Selected(i) Then
         Msg = Msg & ListBox1.List(i) & vbNewLine
     End If
Next i

see

iteration: http://www.java2s.com/Code/VBA-Excel-Access-Word/Forms/GettheselecteditemsinaListBox.htm

delete statement: http://msdn.microsoft.com/en-us/library/office/ff845201(v=office.14).aspx

does it work now?

EDIT

i think nothing happens because this is always true in my opinion (tried it).

If IsNull(ListBoxName) Then
    Exit Sub
End If

try this

Dim strSQL As String
Dim vItem As Variant

With Me.ListBoxName
    For Each vItem In .ItemsSelected
        If Not IsNull(vItem) And Not CStr(vItem) = vbNullString Then
            strSQL = "DELETE FROM Carers WHERE Carer_ID = " & CLng(vItem)
            CurrentDb.Execute strSQL
        End If
    Next
End With

ListBoxName.Requery

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM