Access 2007年 - 通过值在列表框中VBA骑自行车吗?框中、骑自行车、列表、Access

2023-09-08 10:59:43 作者:6.多次拒绝

所以我有一个列表框,数据的基于用户选择不同的设置填充。

So i have a list box that populates with different sets of data based on user selections.

我如何循环任何给定的值可能会在列表框中?这是一个对于每个语句,还是什么?

How can I cycle through any given values that may be in the list box? Is this a For Each statement, or what?

请帮忙 谢谢 贾斯汀

推荐答案

您可以做你一个循环检查在列表框中的每一行,做的无论的与被选择的行。在这个例子中,我显示来自在 lstLocations所选项目的列表框中的第二列。 (列编号从0开始。)

You can do you a For loop to examine each row in the listbox, and do whatever with the rows which are selected. In this example, I display the second column from selected items in the lstLocations listbox. (Column numbering starts with zero.)

Private Sub cmdShowSelections_Click()
    Dim lngRow As Long
    Dim strMsg As String

    With Me.lstLocations
        For lngRow = 0 To .ListCount - 1
            If .Selected(lngRow) Then
                strMsg = strMsg & ", " & .Column(1, lngRow)
            End If
        Next lngRow
    End With

    ' strip off leading comma and space
    If Len(strMsg) > 2 Then
        strMsg = Mid(strMsg, 3)
    End If
    MsgBox strMsg
End Sub

请注意我假设你想从列表框中选定的项目。如果你想要的所有的项目,选择或没有,你可以使用 .ItemData 作为@DavidRelihan的建议。然而,在这种情况下,你可以从列表框中 .RowSource ,而不是让他们。

Note I assumed you want the selected items from the list box. If you want all items, selected or not, you could use .ItemData as @DavidRelihan suggested. However, in that case, you could get them from the listbox .RowSource instead.