Member Avatar for big_ch98

I keep getting that error when I open my form in which I have control that are dynamically created.

ERROR 340: Control array element 0 doesn't exist

Member Avatar for big_ch98

I have 3 controls being dynamically created

'
        'TTCbo
        '
        ClsControls1.Index = 0
        ClsControls1.Name = "_TTCbo_0"
        ClsControls2.Index = 1
        ClsControls2.Name = "_TTCbo_1"
        ClsControls3.Index = 2
        ClsControls3.Name = "_TTCbo_2"
        ClsControls4.Index = 3
        ClsControls4.Name = "_TTCbo_3"
        Me.TTCbo.ControlsCollection.Add(ClsControls1)
        Me.TTCbo.ControlsCollection.Add(ClsControls2)
        Me.TTCbo.ControlsCollection.Add(ClsControls3)
        Me.TTCbo.ControlsCollection.Add(ClsControls4)
        '
        'cmdCommand
        '
        ClsControls5.Index = 0
        ClsControls5.Name = "_cmdCommand_0"
        ClsControls6.Index = 1
        ClsControls6.Name = "_cmdCommand_1"
        Me.cmdCommand.ControlsCollection.Add(ClsControls5)
        Me.cmdCommand.ControlsCollection.Add(ClsControls6)
        '
        'optOption
        '
        ClsControls7.Index = 0
        ClsControls7.Name = "_optOption_0"
        ClsControls8.Index = 1
        ClsControls8.Name = "_optOption_1"
        Me.optOption.ControlsCollection.Add(ClsControls7)
        Me.optOption.ControlsCollection.Add(ClsControls8)

I'm betting that if you step through that code with F8, you will find your error at the line control.index = 0, because at that point, it has not been created yet...

Also, if you want to create controls and have them in an array, then you need to set the control on the form at design time and set its index to zero and load copies from there...

Another Also, the reason we have control arrays is because several controls can have the "SAME" name, i.e. Label1 and each is then referenced by its index number (0), and the reason I bring this up is because you are naming controls ttbco_0, ttbco_1 and as such they are not considered an array because each control as a seperate and unique identifier...

Good Luck

'Simple example of adding controls dynamically at runtime.
'Be sure a CommandButton with name = "Command1" and Index=0 is on the form prior to running code.

Private Sub Command1_Click(Index As Integer)
    MsgBox "This command button's index in the array is " & Index & "."
End Sub

Private Sub Form_Load()
    addRTC  'this will be control with index = 1
    addRTC  'this will be control with index = 2
    'Remember control with index = 0 must already exist
End Sub
  
Public Sub addRTC()
'Add control dynamically at run time
Dim ctlCount As Integer
Dim NewIndex As Integer
Dim I As Integer
    'Check to see that first control exists
    ctlCount = -1
    For I = 0 To Me.Controls.Count - 1
        If Me.Controls(I).Name = "Command1" Then
            ctlCount = ctlCount + 1
        End If
    Next I
    NewIndex = ctlCount + 1
    Command1(NewIndex - 1).Visible = True
    'Create New Control and Set Properties
    Load Command1(NewIndex)
    With Command1(NewIndex)
        .Caption = Command1(0).Name & "(" & Str(NewIndex) & ")"
        .Top = Command1(NewIndex - 1).Top + Command1(NewIndex - 1).Height + 100
        .Left = Command1(NewIndex - 1).Left
        .Width = Command1(NewIndex - 1).Width
        .Visible = True
    End With
End Sub

'This should get you started ...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.