我有一个多选列表框(listScanned),它显示表(TempDelivery)中的记录。列表框中显示的列名为 ParcelID。
我希望能够选择此列表中的项目,然后按“删除按钮”以从列表框和表格中删除这些记录。
每次我尝试运行代码时都会收到消息:
运行时错误 3075:查询表达式“[ParcelID]=”中存在语法错误(缺少运算符)。
我做错了什么?
我尝试了以下代码,尝试了 ParcelID 的不同变体,但我无法让它工作。
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
谢谢!
建议的解决方案
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
您可以循环列表框并在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