Excel VBA 将项目添加到动态创建的组合框

标签 excel for-loop collections combobox vba

我在我的用户表单中动态创建组合框,我想将相同的项目添加到表单上的每个组合框。

我创建了一个项目集合(从 sql 语句和 Access DB 中查询)。然后在创建组合框对象后,我执行了 for each集合内每个项目中的语句添加到组合框,但组合框不填充!控件已创建,但组合框为空

我检查了集合,看看我是否得到了值(value)。 (请参阅我查询收集计数的行)并且我得到了 20 个正确的项目。

我究竟做错了什么?

编辑 - 我最近在调用显示表单的父 For Each 循环的末尾添加了代码。这可能是表单无法正确显示的原因......

 Private Sub setVvalues(ByVal myCol as Collection)
    Dim xSel as Object, selName as String
    Dim sItem as Variant, selectItems as Collection, x as Variant
    Dim con as New ADODB.Connection
    Dim rs as New ADODB.Recordset

    ........[code that already works].........

    con.Open connectionStr   '<-- public String declared elsewhere


    Set selectItems = New Collection
    sql = "SELECT [DESCRIP] FROM tbl_setpoints_categories ORDER BY [ORD] ASC;"

    rs.Open sql, con

    If Not (rs.EOF And rs.BOF) Then
       rs.MoveFirst
       Do While Not rs.EOF
          selectItems.Add rs!DESCRIP
          rs.MoveNext
       Loop
    Else
    End If

    rs.Close
    con.Close
    Set rs = Nothing
    Set con = Nothing


    MsgBox(selectItems.Count)  '<--- produces 20 items

    'myCol is a collection (passed in this sub) of object names that will be used to produce controls
    For Each x in myCol

        selName = "sel" & x & "-" & i
        Set xSel = frm_new_setpoints.Controls.Add("Forms.ComboBox.1", selName, True)
        With xSel
            .Width = 120
            .Left = 384
            .Height = 18
            .Top = 44 + (i * 30)

        End With

        For Each sItem In selectItems
            xSel.AddItem sItem
        Next sItem

     i = i + 1
   Next x

   'Show the form with new controls
   frm_new_setpoints.Show

   Set xSel = Nothing


End Sub

最佳答案

您的代码没有任何问题。这是填充动态创建的组合框的正确方法。我相信您的记录集正在将空白项填充到您的集合中,因此您在组合框中得到了空白值。

看这个例子

Private Sub CommandButton1_Click()
    Dim col As New Collection, itm As Variant
    Dim xSel As Object

    col.Add " "
    col.Add "  "
    col.Add "   "
    col.Add " "

    Set xSel = UserForm2.Controls.Add("Forms.ComboBox.1", "Sid", True)

    With xSel
        .Width = 120
        .Left = 384
        .Height = 18
        .Top = 44

        For Each itm In col
            .AddItem itm
        Next itm
    End With

    UserForm2.Show
    Set xSel = Nothing
End Sub

enter image description here

现在如果你更换
    col.Add " "
    col.Add "  "
    col.Add "   "
    col.Add " "

经过
    col.Add "1"
    col.Add "2"
    col.Add "3"
    col.Add "4"

然后您将看到组合框中填充的值。

enter image description here

备注 :

如果换行
selectItems.Add rs!DESCRIP 


If Len(Trim(rs!DESCRIP)) <> 0 Then selectItems.Add rs!DESCRIP

那么你会注意到 MsgBox(selectItems.Count)不会再给你20。

关于Excel VBA 将项目添加到动态创建的组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39042960/

相关文章:

java - Java 中的双三次插值

python - 以相对顺序存储数据的高效数据结构

Java 8 - 就地映射数组/集合

c# - 是否有用于读取 excel 电子表格文件的 .NET Core 兼容库?

vba - 求和公式 VBA

vba - Excel 三重(多个)VLOOKUP 以及两个工作表上的串联 VLOOKUP 值

excel - If Then,满足条件时连接

javascript - JSlint - 在 for 循环内创建函数与评估函数

javascript - For循环,我需要打印出从1到20的数字

java - 这是 Java SynchronizedCollection 类中的错误吗?