Looping based on control naming convention

  • Thread starter Thread starter Marcel K.
  • Start date Start date
M

Marcel K.

Greetings - can you offer suggestion on how to loop
through controls - control arrays don't exist in Access
97. I made naming convention to test for value in
checkbox control if not false, then enable the
corresponding combobox. There is no relation from
checkbox to combobox other than through naming
convention...
chk_1.....cbo_1...

I want to loop and increment reference to control but am
not having luck...

Regards,

Marcel K.

If (Len(Me.chk_1 & vbNullString) = 0 Or Me.chk_1 = False)
Then
Me.cbo_1.Enabled = False
Me.cbo_1.BackColor = 16777215
Else
Me.cbo_1.Enabled = True
Me.cbo_1.BackColor = 8454143
End If

If (Len(Me.chk_2 & vbNullString) = 0 Or Me.chk_2 = False)
Then
Me.cbo_2.Enabled = False
Me.cbo_2.BackColor = 16777215
Else
Me.cbo_2.Enabled = True
Me.cbo_2.BackColor = 8454143
End If

If (Len(Me.chk_3 & vbNullString) = 0 Or Me.chk_3 = False)
Then
Me.cbo_3.Enabled = False
Me.cbo_3.BackColor = 16777215
Else
Me.cbo_3.Enabled = True
Me.cbo_3.BackColor = 8454143
End If
 
There is are good approaches. One is simply to make each "set" of controls
have the same "tag" function. You can then either load up that list of
controls into a collection (on the forms on-load event), and you get much
something like a control array (you still don't get shared events...but you
do get a collection).

for i = 1 to me.Controls.Count

if me(i).Tab = "CheckBoxGroup1" then
myCol.Add me(i)
end if
next i

You can also simply loop using a string value, so in your case:

for i = 1 to 3
strMyContorl = "cbo_" & i
If (Len(Me(strMyControl) & vbNullString) = 0 Or me(strMyContorl) = False)

etc..
 
Marcel K. said:
Greetings - can you offer suggestion on how to loop
through controls - control arrays don't exist in Access
97. I made naming convention to test for value in
checkbox control if not false, then enable the
corresponding combobox. There is no relation from
checkbox to combobox other than through naming
convention...
chk_1.....cbo_1...

I want to loop and increment reference to control but am
not having luck...

Regards,

Marcel K.

If (Len(Me.chk_1 & vbNullString) = 0 Or Me.chk_1 = False)
Then
Me.cbo_1.Enabled = False
Me.cbo_1.BackColor = 16777215
Else
Me.cbo_1.Enabled = True
Me.cbo_1.BackColor = 8454143
End If

If (Len(Me.chk_2 & vbNullString) = 0 Or Me.chk_2 = False)
Then
Me.cbo_2.Enabled = False
Me.cbo_2.BackColor = 16777215
Else
Me.cbo_2.Enabled = True
Me.cbo_2.BackColor = 8454143
End If

If (Len(Me.chk_3 & vbNullString) = 0 Or Me.chk_3 = False)
Then
Me.cbo_3.Enabled = False
Me.cbo_3.BackColor = 16777215
Else
Me.cbo_3.Enabled = True
Me.cbo_3.BackColor = 8454143
End If

Based on your naming convention this should give you what you need

Dim i As Integer
For i = 1 To 10 'Or however many controls you have
If (Len(Me("chk_" & i) & vbNullString) = 0 Or Me("chk_" & i) = False) Then
Me("cbo_" & i).Enabled = False
Me("cbo_" & i).BackColor = 16777215
Else
Me("cbo_" & i).Enabled = True
Me("cbo_" & i).BackColor = 8454143
End If
Next i

HTH
Steve C
 
Back
Top