【问题标题】:VBA set listbox to multiple valuesVBA将列表框设置为多个值
【发布时间】:2013-03-28 02:13:59
【问题描述】:

我在 Access 中有一个列表框,其中多选值设置为 true。我希望能够通过 VBA 代码设置选定的值。我该怎么做呢?

【问题讨论】:

    标签: ms-access vba


    【解决方案1】:

    使用.Selected 方法,并传递您要选择的项目的索引值。

    'Populate the listbox (probably you are doing this elsewhere):
    
    'Select items items 1 and 2 (remembering ListBox is 0-index, so this selects the 2nd and 3rd items in the list:
    ListBox1.Selected(1) = True
    ListBox1.Selected(2) = True
    

    另外,请确保.MultiSelect = fmMultiSelectMulti.MultiSelect = fmMultiSelectExtended

    【讨论】:

    • MS Access 填充的列表框通常是listbox.RowSource="SELECT ID, Item FROM Table"
    • 我也可以抱怨这些常量 :)
    • 太棒了!谢谢@David Zemens,所以如果我想根据内容选择值,我假设我必须遍历所有项目以找到正确的行号,然后在.Selected 方法中使用该值
    • @loveforvdubs 是的。可能有更好的方法我不知道,但我就是这样做的。
    • @DavidZemens,明白了。再次感谢!
    【解决方案2】:

    作为对上述内容的补充说明。假设我有一个区域列表框,用户在其中选择各个区域,那么此代码可用于根据与所选区域匹配的第五列(4,从零开始计数)从多选列表框 HospCounty 中选择医院。

    For Each itm In Me.Region.ItemsSelected
        For i = 0 To Me.HospCounty.ListCount - 1
            If Trim(Me.HospCounty.Column(4, i)) = Trim(Me.Region.Column(0, itm)) Then
                Me.HospCounty.Selected(i) = True
            End If
        Next
    Next
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多