Link to home
Start Free TrialLog in
Avatar of smoore6809
smoore6809

asked on

Dynamic controls on form

I have created a set of dynamic controls (textboxes, comboboxes) based on what a user has selected within an option frame. For example, a user selects Items, which dynamically creates 3 combo boxes and 3 text boxes on another form. How do I now reference these controls to fill them with data? I want to fill the combo box but I don't know how to reference the name of it.
Avatar of c0ldfyr3
c0ldfyr3
Flag of Ireland image

If the combo is called Combo1 you can just use Combo1(1).Property or Combo1.Item(1).Property
Avatar of smoore6809
smoore6809

ASKER

I have named each one based on a recordset, please review the code. The controls are all there but say I want to load the first cbobox with records from a recordset. How do I reference that first cbobox if there are three on my form?
With rs
    Do Until .EOF
    
        'want to keep track of how many fields are on form so they can be aligned properly
        If j = 3 Then
            numTop = 500
            numLeft = 5000
        End If
        If j = 6 Then
            numTop = 500
            numLeft = 10000
        End If
        
        Select Case !strFieldType
            Case "cboBox"
                 Set cboBox = frmDataView.Controls.Add("VB.combobox", strFieldName, frmDataView) 'Dynamicaly add the combobox
                 Set lblLabel = frmDataView.Controls.Add("VB.Label", "Label" & i, frmDataView) 'Dynamicaly add the label
                    With lblLabel 'Setup properties...
                        .Top = numTop
                        .Left = numLeft
                        .Width = 1000
                        .Height = 315
                        .Caption = strFieldName
                        .Visible = True
                    End With
                    
                    With cboBox 'Setup properties...
                        .Top = numTop
                        .Left = numLeft + 900
                        .Width = 3000
                        .Visible = True
                    End With
                    i = i + 1
            Case "txtBox"
                 Set txtBox = frmDataView.Controls.Add("VB.Textbox", strFieldName, frmDataView) 'Dynamicaly add the textbox
                 Set lblLabel = frmDataView.Controls.Add("VB.Label", "Label" & i, frmDataView) 'Dynamicaly add the label
                    With lblLabel 'Setup properties...
                        .Top = numTop
                        .Left = numLeft
                        .Width = 1000
                        .Height = 315
                        .Caption = strFieldName
                        .Visible = True
                        numCmdTop = .Top
                    End With
                    
                    With txtBox 'Setup properties...
                        .Top = numTop
                        .Left = numLeft + 800
                        .Width = 3000
                        .Visible = True
                        .Height = 315
                    End With
                    Select Case strFieldName
                        Case "BeginDt"
                                Set cmdDynamic = frmDataView.Controls.Add("VB.CommandButton", "cmd" & strFieldName, frmDataView)
                            With cmdDynamic
                                .Top = numTop
                                .Left = numLeft + 4000
                                .Width = 300
                                .Visible = True
                                .Height = 300
                            End With
                        Case "EndDt"
                                Set cmdDynamic = frmDataView.Controls.Add("VB.CommandButton", "cmd" & strFieldName, frmDataView)
                            With cmdDynamic
                                .Top = numTop
                                .Left = numLeft + 4000
                                .Width = 300
                                .Visible = True
                                .Height = 300
                            End With
                        Case "CreateDt"
                                Set cmdDynamic = frmDataView.Controls.Add("VB.CommandButton", strFieldName, frmDataView)
                            With cmdDynamic
                                .Top = numTop
                                .Left = numLeft + 4000
                                .Width = 300
                                .Visible = True
                                .Height = 300
                            End With
                    End Select
                    
                    i = i + 1
                    
        End Select
        'strFieldName = !strFieldName
        numTop = numTop + 500
   .MoveNext
        If Not .EOF Then
          strFieldName = !strFieldName
        End If
            j = j + 1
   Loop
End With

Open in new window

Me.Controls(strFieldName)
I have named one of the cboboxes "Items". I want to then do something under private sub item_Click()
but the event does not fire.
ASKER CERTIFIED SOLUTION
Avatar of c0ldfyr3
c0ldfyr3
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial