简体   繁体   English

Excel VBA在多列组合框中选择项目

[英]Excel VBA Select Item in Multi Column ComboBox

I have a combobox that gets populated with information sourced from an Access Database. 我有一个组合框,其中装有来自Access数据库的信息。 I have a method that opens up records using a ADO Recordset. 我有一种使用ADO Recordset打开记录的方法。 I have a multicolumn combobox that I want to select a value from the populated list on the form. 我有一个多列组合框,我想从表单上的填充列表中选择一个值。 For single column combobox's, the value that gets shown on the form is populated using the value property as shown below. 对于单列组合框,使用value属性填充在表单上显示的值,如下所示。

 frmDataEntry.txtProcessID = sourceRS.Fields("ProcessID").Value

I am having issues populating a multicolumn combobox as the Value property does not work. 由于Value属性不起作用,我在填充多列组合框时遇到问题。

Do Until y >= frmDataEntry.cmbProcess.listCount
     If frmDataEntry.cmbProcess.List(y, 0) = sourceRS.Fields("ProcessID").Value Then
        'This is where i'd like to set the value
    End If
    y = y + 1
Loop

In the code above, if this were a listbox, i would use .Selected(y) = True property but that is not available for comboboxes. 在上面的代码中,如果这是一个列表框,我将使用.Selected(y) = True属性,但不适用于组合框。 Does anybody have any suggestions? 有人有什么建议吗? I have searched everywhere but cannot seem to find a way to do this without wiping out the populated list of the combobox. 我到处搜索过,但似乎无法找到一种方法来不删除组合框的填充列表。

我不确定您要做什么,但是如果您的问题显示的是已添加的值,则frmDataEntry.cmbProcess.ListIndex = y可以完全满足0 <= y < ListCount

Normally this is what I do. 通常这就是我要做的。 I put the values into an array then do this: 我将值放入数组,然后执行以下操作:

Dim MyArray() As String 
'Here you want to populate MyArray
With Me.combobox1 
    .ColumnCount = 2 
    .BoundColumn = 2 
    .ColumnWidths = "2.5 in; 0 in" 
    .List = MyArray 
End With 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM