Access VBA: 如何统计ComboBox的项目数?
Access VBA: How to count items of a ComboBox?
在我的 ComboBox
中,我已经选择了一些项目。
我想用VBA来计算它们。我期待找到类似以下字符串的内容,但我得到 Compile error: Argument not optional
.
Me.<ComboBox_name>.ItemData.Count
我还想使用以下字符串,但它给了我 0
项:
Me.<ComboBox_name>.ItemSelected.Count
组合框通常用于选择或显示单个项目的选择,而列表框自然支持多项选择。
也就是说,如果您将一个多值* table 字段链接到一个组合框,例如,您可以有一个包含多个选择的组合框。如果是这种情况,则值在 .ItemsSelected
属性 中可用的唯一时间是当 Combobox 具有焦点并被下拉时。
解决此问题的一种方法是将 Combobox 的 .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
希望对您有所帮助!
*Note: multivalued fields are usually ill-advised, as they are poorly
supported by Access and usually mean you should be normalizing your
tables, i.e. breaking out the multivalued field into another table.
算上选项是:
Me!<ComboBox_name>.ListCount
或者更准确地说,如果您使用列标题:
Me!<ComboBox_name>.ListCount - Abs(Me!<ComboBox_name>.ColumnHeads)
在我的 ComboBox
中,我已经选择了一些项目。
我想用VBA来计算它们。我期待找到类似以下字符串的内容,但我得到 Compile error: Argument not optional
.
Me.<ComboBox_name>.ItemData.Count
我还想使用以下字符串,但它给了我 0
项:
Me.<ComboBox_name>.ItemSelected.Count
组合框通常用于选择或显示单个项目的选择,而列表框自然支持多项选择。
也就是说,如果您将一个多值* table 字段链接到一个组合框,例如,您可以有一个包含多个选择的组合框。如果是这种情况,则值在 .ItemsSelected
属性 中可用的唯一时间是当 Combobox 具有焦点并被下拉时。
解决此问题的一种方法是将 Combobox 的 .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
希望对您有所帮助!
*Note: multivalued fields are usually ill-advised, as they are poorly supported by Access and usually mean you should be normalizing your tables, i.e. breaking out the multivalued field into another table.
算上选项是:
Me!<ComboBox_name>.ListCount
或者更准确地说,如果您使用列标题:
Me!<ComboBox_name>.ListCount - Abs(Me!<ComboBox_name>.ColumnHeads)