问题描述
我有一个列表框,根据用户的选择填充不同的数据集.
I have a list box that populates with different sets of data based on user selections.
如何循环显示列表框中的任何给定值?这是For Each
语句还是什么?
How can I cycle through any given values that may be in the list box? Is this a For Each
statement, or what?
推荐答案
您可以执行For
循环以检查列表框中的每一行,并对选定的行执行任何操作.在此示例中,我显示了 lstLocations 列表框中所选项目的第二列. (列编号从零开始.)
You can do 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.
这篇关于循环浏览MS Access列表框中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!