I have a Multiselect Listbox (listScanned) that shows records from a Table (TempDelivery). The Column shown in the ListBox is named ParcelID.
I want to be able to select items in this list and press a “Delete Button” to delete these records both from the listbox and the table.
Everytime I try to run the code I get the message:
Run-Time Error 3075: Syntax Error (Missing Operator) in Query Expression ‘[ParcelID]=’.
What am I doing wrong?
I’ve tried the following code, trying different variations of ParcelID but I can’t get it to work.
Private Sub deleteListItem_Click()
Dim Var As Variant
If Me.listScanned.ItemsSelected.Count > 0 Then
For Each Var In Me.listScanned.ItemsSelected
CurrentDb.Execute "Delete * FROM TempDelivery WHERE [ParcelID]= " & Me.listScanned.Column(2, Var)
Next
Me.listScanned.Requery
End If
End Sub
Branco is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
You can loop the listbox and delete in one go:
Private Sub DeleteListItem_Click()
Dim Ids As String
Dim Index As Long
If Me!ListScanned.ItemsSelected.Count > 0 Then
For Index = 0 To Me!ListScanned.ListCount - 1
If Me!ListScanned.Selected(Index) Then
If Ids <> "" Then
Ids = Ids & ","
End If
Ids = Ids & Me!ListScanned.Column(2, Index)
' Clear selection
Me!ListScanned.Selected(Index) = False
End If
Next
Debug.Print Ids
CurrentDb.Execute "Delete * From TempDelivery Where PracelID In (" & Ids & ")"
Me.Requery
Me!ListScanned.Requery
End If
End Sub
2