我有一个绑定到表的列表框(tblTransferDetail)。主键是“ TransNum”(类型INT),列表框绑定到此作为第一列。
列表框的双击事件(lstScannedItems)执行以下代码:
CurrentDb.Execute "DELETE FROM tblTransferDetail WHERE TransNum = " & Me.lstScannedItems, dbSeeChanges
但是,当我双击列表框中的一条记录时,我不断收到“ VBA运行时错误3075”,并且上面的行被突出显示。
我要去哪里错了?
答案 0 :(得分:1)
您可以简单地遍历.ItemsSelected
,然后使用每个选定项目的项目数据执行查询。
Dim v As Variant
For Each v In Me.lstScannedItems.ItemsSelected
CurrentDb.Execute "DELETE FROM tblTransferDetail WHERE TransNum = " & Me.lstScannedItems.ItemData(v)
Next
答案 1 :(得分:0)
Me.lstScannedItems
仅在值绑定列时有效。如果您需要使用选择项,然后遍历选择项集合以找出选择了哪个项。
Dim opValue As String
Dim x As Long
With Me.lstScannedItems
For x = 0 To .ListCount
If .Selected(x) = True Then
opValue = .List(x)
Exit For
End If
Next x
End With
CurrentDb.Execute "DELETE FROM tblTransferDetail WHERE TransNum = " & opValue, dbSeeChanges