循环浏览 MS Access 列表框中的值 [英] cycling through values in a MS Access list box
问题描述
我有一个列表框,可以根据用户的选择填充不同的数据集.
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
循环来检查列表框中的每一行,并使用 whatever被选中的行.在此示例中,我显示了 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 列表框中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!