【问题标题】:Excel VBA Add Items to Dynamically Created ComboBoxExcel VBA 将项目添加到动态创建的组合框
【发布时间】:2016-08-19 15:38:26
【问题描述】:

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

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

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

我做错了什么?

EDIT - 我最近在调用显示表单的父 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

【问题讨论】:

  • 我刚刚在一个新的用户表单和一个自定义集合中测试了你的代码。你的代码对我有用。
  • 嗯...让我添加更多代码(我认为它不会影响它)。
  • 你从哪里运行代码?
  • 该子程序正在从不同的表单运行。我刚刚添加了调用要显示的新表单的其余代码。这就是组合框没有填充的原因吗?
  • 不。它仍然对我有用:)

标签: excel for-loop collections combobox vba


【解决方案1】:

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

看这个例子

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

现在如果你替换

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

通过

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

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

注意

如果换行

selectItems.Add rs!DESCRIP 

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

然后你会注意到MsgBox(selectItems.Count) 将不再给你 20。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-20
    • 1970-01-01
    • 2013-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多