VBA / MS Access,清除新记录中的选择项目

时间:2011-04-04 18:25:00

标签: ms-access vba

我有一个列表框,我用表/查询填充,并用一些VBA代码“选择”多个条目。在Form_Current上,我打算让代码选择该列表框中为当前记录选择的任何项目(保存在另一个表中)。

单击以转到下一条记录时,仍会选择先前选择的列表框项目。我该如何清除它们?我本以为新的记录会自动为我做这一切。

我刚刚12年来第一次回到VBA,所以我不是大师。

谢谢, 汉斯

1 个答案:

答案 0 :(得分:1)

试试这个(found here):

Function ClearList(lst As ListBox) As Boolean
On Error GoTo Err_ClearList
    'Purpose:   Unselect all items in the listbox.
    'Return:    True if successful
    'Author:    Allen Browne. http://allenbrowne.com  June, 2006.
    Dim varItem As Variant

    If lst.MultiSelect = 0 Then
        lst = Null
    Else
        For Each varItem In lst.ItemsSelected
            lst.Selected(varItem) = False
        Next
    End If

    ClearList = True

Exit_ClearList:
    Exit Function

Err_ClearList:
    Call LogError(Err.Number, Err.Description, "ClearList()")
    Resume Exit_ClearList
End Function