MS Access - 使用多选列表框删除记录

时间:2015-02-04 11:33:08

标签: forms vba listbox multi-select

我想在Access(2007)中使用多选Listbox表单控件来删除未绑定表中的多个记录。列表框由日期(dd / mmm / yy)组成,因此我需要编写可以理解日期类型的代码,但我很难找到符合我要求的正确代码。任何建议或意见将不胜感激!

以下是我附加到表单上的“确定命令”按钮的代码,我无法使其工作:

Private Sub cmdOK1_Click()
    Dim vItem       As Variant
    Dim strSet      As String
    Dim i           As Long

    strSet = ""

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

    'Remove the first comma
    strSet = Mid(Trim(strSet), 2, Len(strSet) - 1)

    strSQL = "DELETE FROM tblDataConsolidation WHERE Period IN (" & strSet & ")"

    CurrentDb.Execute strSQL

    For i = 0 To lstPeriod.ListCount - 1
        lstPeriod.Selected(i) = False
    Next

    For i = 0 To lstPeriod.ListCount - 1
        lstPeriod.Selected(i) = False
    Next

    lstPeriod.Requery
    lstPeriod.Requery
End Sub

0 个答案:

没有答案