【问题标题】:Access VBA: How to count items of a ComboBox?Access VBA:如何计算组合框的项目?
【发布时间】:2018-06-04 01:21:54
【问题描述】:

在我的ComboBox 中,我已经选择了一些项目。

我想用 VBA 来计算它们。我期待找到类似以下字符串的内容,但我得到了Compile error: Argument not optional

Me.<ComboBox_name>.ItemData.Count

我也想过使用以下字符串,但它给了我0 项目:

Me.<ComboBox_name>.ItemSelected.Count

【问题讨论】:

    标签: ms-access combobox count vba selection


    【解决方案1】:

    组合框通常用于选择或显示单个项目的选择,而列表框自然支持多项选择。

    也就是说,如果您将一个多值* 表字段链接到一个组合框,例如,您可以有一个包含多个选择的组合框。如果是这种情况,只有当 Combobox 具有焦点并被下拉时,.ItemsSelected 属性中的值才可用。

    解决此问题的一种方法是将组合框的.Value 属性分配给一个数组。该数组将包含选定的值。您可以通过取数组的上限并添加 1 来计算它们:

    Dim comboitems() as Variant
    Dim count as Long
    
    comboitems = yourcombobox.Value
    
    ' array is 0-based so add one to get the count
    count = UBound(comboitems) + 1
    

    如果数组是多维的,你可以这样读取值:

    ' array is 0-based so add one to get the count
    count = UBound(comboitems, [dimension]) + 1
    
    ' where [dimension] is a 1-based index equivalent to the 'column' of the data
    

    希望对你有帮助!

    *注意:多值字段通常是不明智的,因为它们很差 Access 支持,通常意味着您应该规范化您的 表,即将多值字段拆分为另一个表。

    【讨论】:

      【解决方案2】:

      要计算选项,它是:

      Me!<ComboBox_name>.ListCount
      

      或者更准确地说,如果您使用列标题:

      Me!<ComboBox_name>.ListCount - Abs(Me!<ComboBox_name>.ColumnHeads)
      

      【讨论】:

      • 谢谢 :-) 你救了我。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多