FOR EACH...NEXT help

  • Thread starter Thread starter Brian
  • Start date Start date
B

Brian

I have a form with about 50 checkboxes on it. The user is
allowed to check as many boxes as he/she wishes UNLESS
they check this one box. If they check this one box, I
want all the other boxes to have their .ENABLE property
changed to FALSE. Now I can write one massive IF
statement to do this, but I was thinking that there might
be a way to do this with a FOR EACH...NEXT statement.
Anyone out there know how to write such a statement? The
MS Access help is no help!

Thanks is advance
 
Brian said:
I have a form with about 50 checkboxes on it. The user is
allowed to check as many boxes as he/she wishes UNLESS
they check this one box. If they check this one box, I
want all the other boxes to have their .ENABLE property
changed to FALSE. Now I can write one massive IF
statement to do this, but I was thinking that there might
be a way to do this with a FOR EACH...NEXT statement.
Anyone out there know how to write such a statement? The
MS Access help is no help!

Well, in all fairness, that's not the kind of thing you could expect to
find explicit instructions for in any help file. But I won't try to
argue that the help file is much good in recent versions, at least when
t comes to actually finding what you're looking for.

So long as there's a way to identify the check boxes you're interested
in, you can use a For ... Next loop to identify and manipulate them. I
can think of the following ways the check boxes might be identifiable:

(1) These are the only check boxes on the form:

Dim ctl As Access.Control

For Each ctl In Me.Controls
If ctl.ControlType = acCheckBox Then
If ctl.Name <> "TheSpecialOne" Then
ctl.Enabled = False
End If
End If
Next ctl

(2) They are named according to a pattern, such as "Check1" to
"Check50", or they all contain a particular string in the name:

Dim I As Integer

For I = 2 To 50
Me.Controls("Check" & I).Enabled = False
Next I

---- OR ----

Dim ctl As Access.Control

For Each ctl In Me.Controls
If ctl.Name Like "*MyPattern*" Then
ctl.Enabled = False
End If
Next ctl

(3) You can identify them yourself by setting all their Tag properties
(on the Other tab of the property sheet) to the same value:

Dim ctl As Access.Control

For Each ctl In Me.Controls
If ctl.Tag = "EnableDisable" Then
ctl.Enabled = False
End If
Next ctl
 
Back
Top