ListBox在重新查询后保留选择数据

时间:2016-06-03 16:33:07

标签: vba ms-access access-vba access

我目前正在使用一个列表框来删除包含表中所选文本的值。我有以下代码:

Private Sub DeleteEntry_Click()

Dim DeleteTbl As String
Dim Msg As String



If IsNull(Me.lstSolution) = True Then
    MsgBox "Please Select a Entry", vbOKOnly, "No Entry Selected"
Else
    DeleteTbl = MsgBox(Msg, vbYesNo, "Delete Entries?")
    Msg = "Are you sure you wish to delete all entrys containing: "
    Msg = Msg & Me.lstSolution
    DeleteTbl = MsgBox(Msg, vbYesNo, "Delete Entries?")
End If

If DeleteTbl = vbYes Then
    DoCmd.SetWarnings False
    DoCmd.RunSQL "Delete * FROM [Solutions] WHERE [Solutions].SolutionText = '" & Me.lstSolution & "'"
    DoCmd.SetWarnings True
Else
     'Do Nothing
End If

    Me.lstSolution.Requery

End Sub

我遇到的问题是,从表中清除条目后,我再次单击该按钮(不选择任何内容),之前选择的值仍会出现在MsgBox中。我需要清除此值,以便显示一个错误消息框,表示没有选择任何条目

2 个答案:

答案 0 :(得分:0)

我设法通过添加NotSelected变量来解决问题,如下所示:

Private Sub DeleteEntry_Click()

Dim DeleteTbl As String
Dim Msg As String
Dim NotSelected As String

If IsNull(Me.lstSolution) = True Then
    MsgBox "Please Select a Entry", vbOKOnly, "No Entry Selected"
    NotSelected = True
Else
    Msg = "Are you sure you wish to delete all entrys containing: "
    Msg = Msg & Me.lstSolution
    DeleteTbl = MsgBox(Msg, vbYesNo, "Delete Entries?")
    NotSelected = False
End If

If NotSelected = True Then
    End
ElseIf DeleteTbl = vbYes Then
    DoCmd.SetWarnings False
    DoCmd.RunSQL "Delete * FROM [Solutions] WHERE [Solutions].SolutionText = '" & Me.lstSolution & "'"
    DoCmd.SetWarnings True
End If

    Me.lstSolution.Requery


End Sub

答案 1 :(得分:0)

ListBox.Requery不会刷新ListBox.Value,也不会刷新Listbox.Column(index)

您必须在lstSolution.SetFocus之后致电lstSolution.Requery。这样会将新值从当前记录集传播到列表框列。

我不确定,但是我认为这是MS Access中的错误。