VBA listbox_click 事件 - itemsselected 未注册

问题描述 投票:0回答:1

我在列表框的“单击”事件中有此代码。当用户在列表框中选择一个项目时,该过程会运行,但是 ctrl.ItemsSelected.Count = 0。由于某种原因,Access 没有注册列表框中选择了一个项目,因此 For Each 被跳过。

Private Sub lstPositionID_Click()
Dim PositionID As Integer, RS As Recordset, SQLstmt As String, ctrl As ListBox
Set frm = Forms![Enter Candidate Duties]
Set ctrl = frm!lstPositionID
For Each varItm In ctrl.ItemsSelected 'get position ID
    PositionID = ctrl.Column(0, varItm)
Next varItm
End Sub

如何单击列表框中的项目然后运行此过程?

vba ms-access listbox
1个回答
0
投票

使用 AfterUpdate 事件 - 如以下演示代码所示:

Private Sub TestList_AfterUpdate()

    Dim Items()     As Variant
    
    Dim ItemsList   As String
    Dim Item        As Variant
    Dim Index       As Long
    Dim Count       As Long
    
    Count = Me!TestList.ItemsSelected.Count
    If Count > 0 Then
        ReDim Items(Count - 1)
        For Each Item In Me.TestList.ItemsSelected
            Items(Index) = Item
            Index = Index + 1
        Next
        ItemsList = Join(Items(), ";")
    End If
    
    ' Save list of selected items.
    Me!ItemsList.Value = ItemsList

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.