Consulting

Results 1 to 8 of 8

Thread: Controls array

  1. #1
    VBAX Regular
    Joined
    Jul 2011
    Posts
    33
    Location

    Controls array

    Hello, I'm having a problem trying to create two arrays of userform controls. The first one (frames) is a one-dimensional array of Frames, and for each frame there is a row of two elements on the txtbox array that should contain the textboxes on it. Debugging the code, in both cases it seems that I'm storing the value of the control, not the control itself. Here is the code:

    numFrames = 1
    For Each co In Me.datos.Controls
        If TypeName(co) = "Frame" Then
            ReDim frames(1 To numFrames)
            ReDim txtbox(1 To 2, 1 To numFrames)
            frames(numFrames) = co.Object
            j = 1
            For Each txt In co.Controls
                If TypeName(txt) = "TextBox" Then
                    txtbox(j, numFrames) = txt.Object
                    j = j + 1
                End If
            Next txt
            numFrames = numFrames + 1
        End If
    Next co
    "datos" is another Frame wich contains several frames.

    Thanks in advance
    Last edited by Aussiebear; 04-16-2023 at 02:32 PM. Reason: Adjusted the code tags

  2. #2
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    You need to use Set if you are assigning an object to an array (or indeed to any other variable)
    Be as you wish to seem

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Also, does that work if you have more than one frame? You don't preserve the array, and there seems no control on the 1st dimension..
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    This does more than you need but should illustrate the concept.

    In a Module:
    Public cCB() As Control, cTB() As Control
    
    Private Sub CommandButton1_Click()
      Dim ct As Control
      For Each ct In Controls
        If TypeName(ct) = "TextBox" Then
            ct.Value = ""
        End If
      Next ct
    End Sub
    
    'Public cCB() As Control, cTB() As Control 'In a Module
    Private Sub UserForm_Initialize()
      FillControlArrays
    End Sub
    
    Private Sub ComboBox1_Change()
      FillTBs
    End Sub
    
    Private Sub ComboBox2_Change()
      FillTBs
    End Sub
    
    Private Sub ComboBox3_Change()
      FillTBs
    End Sub
    
    Private Sub FillControlArrays()
      Dim iCB As Integer, iTB As Integer, c As Control
    For Each c In Me.Controls
        Select Case TypeName©
            Case "TextBox"
                iTB = iTB + 1
                ReDim Preserve cTB(1 To iTB)
                Set cTB(iTB) = c
           Case "ComboBox"
                iCB = iCB + 1
                ReDim Preserve cCB(1 To iCB)
                Set cCB(iCB) = c
        End Select
        Next c
    End Sub
    
    Private Sub FillTBs()
    Dim offset As Integer, i As Integer, j As Integer
    'Set offsets for textbox controls based on combobox
      Select Case ActiveControl
        Case ComboBox1
            offset = 0
        Case ComboBox2
            offset = 5
        Case ComboBox3
            offset = 10
        Case Else
            offset = -1
      End Select
    If offset = -1 Then Exit Sub
    'Fill TextBox controls will values based on combobox that is active and offset
      For i = 1 To 5
        j = i + offset
        cTB(j) = ActiveControl.Value & i
      Next i
    End Sub
    Last edited by Aussiebear; 04-16-2023 at 02:35 PM. Reason: Adjusted the code tags

  5. #5
    VBAX Regular
    Joined
    Jul 2011
    Posts
    33
    Location
    Thanks very much, I've already use the set instruction but still got a problem. In this code I create the control arrays:
    In a module:
    Public frames() As Control, txtbox() As Control
    
    Private Sub UserForm_Initialize()
    Dim co As Control, txt As Control
    numFrames = 1
    For Each co In Me.datos.Controls
        If TypeName(co) = "Frame" Then
            ReDim frames(1 To numFrames)
            ReDim txtbox(1 To 2, 1 To numFrames)
            Set frames(numFrames) = co
            j = 1
            For Each txt In co.Controls
                If TypeName(txt) = "TextBox" Then
                    Set txtbox(j, numFrames) = txt
                    j = j + 1
                End If
            Next txt
            numFrames = numFrames + 1
        End If
    Next co
    arreglo_frames = Split("0,0,0,0,0,0,0", ",")
    habilitar arreglo_frames
    End Sub
    Then, in the following habilitar subroutine I got an error that says something like "object variable or With block not established" on the line "frames(i).Enabled = True" on the first iteration of the For.
    Public Sub habilitar(ByRef fs() As String)
    Dim i, j, n As Integer
    n = UBound(fs)
    For i = 1 To n
        If fs(i) = "1" Then
            frames(i).Enabled = True
            Else
            frames(i).Enabled = True
            For j = 1 To 2
                txtbox(j, i).Value = 0
            Next j
            frames(i).Enabled = False
        End If
    Next i
    End Sub
    I don't think the problem is on the creation of the arrays because the same line inside the Userform_Initialize sub worked fine.

    Thanks again
    Last edited by Aussiebear; 04-16-2023 at 03:12 PM. Reason: Adjusted the code tags

  6. #6
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    When you are not in the Private Sub for the userform object, you need to prefix the controls with the userform object name.

  7. #7
    VBAX Regular
    Joined
    Jul 2011
    Posts
    33
    Location
    If you mean this:
    ingresar_ciclico.frames(i).Enabled = True
    or

    Me.frames(i).Enabled = True
    "ingresar_ciclico" is the userform name
    It selects ".frames" and throws : "Method or data member not found"
    Last edited by Aussiebear; 04-16-2023 at 03:13 PM. Reason: Adjusted the code tags

  8. #8
    VBAX Regular
    Joined
    Jul 2011
    Posts
    33
    Location
    Hey, I hadn't seen xld's comment. It was the Preserve command missing. Thanks again everybody

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •