PDA

View Full Version : [SOLVED] Controls array



pepe90
08-11-2011, 06:55 AM
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

Aflatoon
08-11-2011, 06:59 AM
You need to use Set if you are assigning an object to an array (or indeed to any other variable)

Bob Phillips
08-11-2011, 08:29 AM
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..

Kenneth Hobs
08-11-2011, 09:46 AM
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

pepe90
08-11-2011, 01:48 PM
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

Kenneth Hobs
08-11-2011, 01:54 PM
When you are not in the Private Sub for the userform object, you need to prefix the controls with the userform object name.

pepe90
08-11-2011, 02:48 PM
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"

pepe90
08-12-2011, 05:17 AM
Hey, I hadn't seen xld's comment. It was the Preserve command missing. Thanks again everybody