2016-08-19 28 views
1

私は自分のユーザーフォームにコンボボックスを動的に作成しています。フォーム上のすべてのコンボボックスに同じ項目を追加したいと思います。Excel VBA動的に作成されたコンボボックスに項目を追加

私はアイテムのコレクションを作成しました(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 
+0

私はあなたのコードを新しいuserformとカスタマイズされたコレクションでテストしました。あなたのコードは私のために働く。 –

+0

うーん...もう少しコードを追加してみましょう(私はそれがそれに影響していないと思った)。 – Sanya

+0

どこからコードを実行していますか? –

答えて

1

これは...かもしれません。これは、動的に作成されたコンボボックスに移入する正しい方法です。私はあなたのレコードセットがあなたのコレクションに空白の項目を埋めると考えているので、あなたはコンボボックスで空白の値を取得しています。

あなたは

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

によって

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

を交換するなら、あなたは値がコンボボックスに移入表示されます今

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

この例を参照してください。 es。

enter image description here

あなたは

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

にライン

selectItems.Add rs!DESCRIP 

を変更するなら、あなたはMsgBox(selectItems.Count)は、もはやあなたに20を与えないだろうことがわかります。

関連する問題