MS Access - 使用 multi-select 列表框删除记录
MS Access - deleting records using multi-select Listbox
我想在 Access (2007) 中使用多 select 列表框表单控件来删除未绑定 Table 中的多条记录。列表框由日期 (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
由于您处理的是日期,因此需要确保日期包含在 #
标签之间,并确保日期在 MM/DD/YYYY 中 格式。所以下面的代码应该可以工作,
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 & "," & Format(.ItemData(vItem), "\#mm\/dd\/yyyy\#")
End If
Next
End With
'Remove the first comma
strSet = Trim(Right(strSet, Len(strSet) - 1))
If MsgBox("You are about to DELETE records from the table. This process is not reversible." & vbCrLf & vbCrLf & _
"Are you sure you want to proceed?", vbYesNo + vbQuestion) = vbNo Then Exit Sub
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
我想在 Access (2007) 中使用多 select 列表框表单控件来删除未绑定 Table 中的多条记录。列表框由日期 (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
由于您处理的是日期,因此需要确保日期包含在 #
标签之间,并确保日期在 MM/DD/YYYY 中 格式。所以下面的代码应该可以工作,
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 & "," & Format(.ItemData(vItem), "\#mm\/dd\/yyyy\#")
End If
Next
End With
'Remove the first comma
strSet = Trim(Right(strSet, Len(strSet) - 1))
If MsgBox("You are about to DELETE records from the table. This process is not reversible." & vbCrLf & vbCrLf & _
"Are you sure you want to proceed?", vbYesNo + vbQuestion) = vbNo Then Exit Sub
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