Loop through checkboxes

A

alex

Loop through checkboxes

Hi,

I’m attempting to loop through a series of checkboxes (not part of
option group) to see if ANY has been checked (a user can check 1 or
all of them). If so, I want to enable a series of textboxes.

Here’s the code:

For Each ctl In Me.Controls
If ctl.Tag = "Grouping1" Then 'first group
If ctl.Value = True Then
'would like the code below to read
'if ctl.tag = "Grouping2" then enable
Me.comSubject.Enabled = True 'one of the textboxes in
group 2
Debug.Print (ctl.Name)
Else
Me.comSubject.Enabled = False
End If
End If
Next ctl

The code seems to work, but somehow gets confused (sometimes the
textbox will be enabled and sometimes not, depending on the order in
which a check box has a value??).

Also, when the user checks a box, this code is in the form’s on
current event, so it won’t immediately fire. If I add a call to the
form’s after update event will it fire then (after a box was checked
or unchecked)? Or will I have to add code to each checkbox’s after
update event?

Thanks,
alex
 
S

Stefan Hoffmann

hi Alex,

The code seems to work, but somehow gets confused (sometimes the
textbox will be enabled and sometimes not, depending on the order in
which a check box has a value??).
I'm not sure that I understand your logic. What is that comment about if
tag equals grouping 2?

Maybe this is what you want:

Dim ctl As Access.Contrl
Dim State As Boolean

State = False
For Each ctl In Me.Controls
If TypeOf ctl Is Access.CheckBox And ctl.Tag = "Grouping1" Then
State = True
Exit For
End If
Next ctl
comSubject.Enabled = State
Also, when the user checks a box, this code is in the form’s on
current event, so it won’t immediately fire. If I add a call to the
form’s after update event will it fire then (after a box was checked
or unchecked)? Or will I have to add code to each checkbox’s after
update event?
You need to wire up all your check boxes.


mfG
--> stefan <--
 
A

Arvin Meyer [MVP]

I can't answer your first question. I'd experiment with the order to see if
that helps.

If you want it to check upon each check box update, you will have to put the
code in each check box, or even easier. I'd make the code a function instead
of a sub, then I'd select all the checkboxes in design view, open the
property sheet, and in the click or after update event, I'd put:

=FunctionName()

That takes far less time then adding code to each checkbox.
--
Arvin Meyer, MCP, MVP
http://www.datastrat.com
http://www.mvps.org/access
http://www.accessmvp.com

Loop through checkboxes

Hi,

I’m attempting to loop through a series of checkboxes (not part of
option group) to see if ANY has been checked (a user can check 1 or
all of them). If so, I want to enable a series of textboxes.

Here’s the code:

For Each ctl In Me.Controls
If ctl.Tag = "Grouping1" Then 'first group
If ctl.Value = True Then
'would like the code below to read
'if ctl.tag = "Grouping2" then enable
Me.comSubject.Enabled = True 'one of the textboxes in
group 2
Debug.Print (ctl.Name)
Else
Me.comSubject.Enabled = False
End If
End If
Next ctl

The code seems to work, but somehow gets confused (sometimes the
textbox will be enabled and sometimes not, depending on the order in
which a check box has a value??).

Also, when the user checks a box, this code is in the form’s on
current event, so it won’t immediately fire. If I add a call to the
form’s after update event will it fire then (after a box was checked
or unchecked)? Or will I have to add code to each checkbox’s after
update event?

Thanks,
alex
 
A

alex

I can't answer your first question. I'd experiment with the order to see if
that helps.

If you want it to check upon each check box update, you will have to put the
code in each check box, or even easier. I'd make the code a function instead
of a sub, then I'd select all the checkboxes in design view, open the
property sheet, and in the click or after update event, I'd put:

=FunctionName()

That takes far less time then adding code to each checkbox.
--
Arvin Meyer, MCP, MVPhttp://www.datastrat.comhttp://www.mvps.org/accesshttp://www.accessmvp.com


Loop through checkboxes

Hi,

I’m attempting to loop through a series of checkboxes (not part of
option group) to see if ANY has been checked (a user can check 1 or
all of them).  If so, I want to enable a series of textboxes.

Here’s the code:

For Each ctl In Me.Controls
    If ctl.Tag = "Grouping1" Then 'first group
        If ctl.Value = True Then
            'would like the code below to read
            'if ctl.tag = "Grouping2" then enable
            Me.comSubject.Enabled = True 'one of the textboxes in
group 2
            Debug.Print (ctl.Name)
        Else
            Me.comSubject.Enabled = False
        End If
    End If
Next ctl

The code seems to work, but somehow gets confused (sometimes the
textbox will be enabled and sometimes not, depending on the order in
which a check box has a value??).

Also, when the user checks a box, this code is in the form’s on
current event, so it won’t immediately fire.  If I add a call to the
form’s after update event will it fire then (after a box was checked
or unchecked)?  Or will I have to add code to each checkbox’s after
update event?

Thanks,
alex

Sorry guys if my question was a little confusing…

I’m trying to disable controls on a form until certain other controls
are given a value.

My first task is to disable all controls on the form (except a few
checkboxes at the top).

Once a checkbox is checked, other controls (with a tag of “Grouping2”)
will become enabled.

Stefan,
Using your code (modified a bit):

Dim ctl as Control
Dim State As Boolean
State = False
For Each ctl In Me.Controls
If ctl.Tag = "Grouping1" And Not IsNull(ctl) Then 'first grouping
has a value
State = True
Debug.Print (ctl.Name)
Exit For
End If
Next ctl
Me.comSubject.Enabled = State

The code above does not throw an error, but it doesn’t work. If a
checkbox does not have a value (but its background is white) it’s
evaluating to not null. If the checkbox does not have a value (but
its background is grey…when you start a new record) it IS evaluating
to null (and the code works).

What I need to do is change the variable [State] to False if the
ctl.Tag = “Grouping1” And any of the controls with a Tag of
“Grouping1” has a value. I even tried ctl.Value = True, but that
didn’t work either. I'm confused about what value an "empty" checkbox
possesses. Apparently it's not null.

Regarding your question about “Grouping2”…
Instead of Me.comSubject.Enabled = State, I’d like to be able to
change the enabled property of any control with a Tag of “Grouping2.”

Hope that helps.
Thanks again,
alex
 
S

Stefan Hoffmann

hi Alex,

The code above does not throw an error, but it doesn’t work.
Basically some sort of short-cut :)

You should have spotted it, the inner body of the If must be:

State = Nz(ctl.Value, True)
If State Then
Exit For
End If


mfG
--> stefan <--
 
A

alex

I can't answer your first question. I'd experiment with the order to see if
that helps.
If you want it to check upon each check box update, you will have to put the
code in each check box, or even easier. I'd make the code a function instead
of a sub, then I'd select all the checkboxes in design view, open the
property sheet, and in the click or after update event, I'd put:

That takes far less time then adding code to each checkbox.
"alex" <[email protected]> wrote in message
Loop through checkboxes

I’m attempting to loop through a series of checkboxes (not part of
option group) to see if ANY has been checked (a user can check 1 or
all of them).  If so, I want to enable a series of textboxes.
Here’s the code:
For Each ctl In Me.Controls
    If ctl.Tag = "Grouping1" Then 'first group
        If ctl.Value = True Then
            'would like the code below to read
            'if ctl.tag = "Grouping2" then enable
            Me.comSubject.Enabled = True 'one of the textboxes in
group 2
            Debug.Print (ctl.Name)
        Else
            Me.comSubject.Enabled = False
        End If
    End If
Next ctl
The code seems to work, but somehow gets confused (sometimes the
textbox will be enabled and sometimes not, depending on the order in
which a check box has a value??).
Also, when the user checks a box, this code is in the form’s on
current event, so it won’t immediately fire.  If I add a call to the
form’s after update event will it fire then (after a box was checked
or unchecked)?  Or will I have to add code to each checkbox’s after
update event?
Thanks,
alex

Sorry guys if my question was a little confusing…

I’m trying to disable controls on a form until certain other controls
are given a value.

My first task is to disable all controls on the form (except a few
checkboxes at the top).

Once a checkbox is checked, other controls (with a tag of “Grouping2”)
will become enabled.

Stefan,
Using your code (modified a bit):

Dim ctl as Control
Dim State As Boolean
State = False
For Each ctl In Me.Controls
    If ctl.Tag = "Grouping1" And Not IsNull(ctl) Then 'first grouping
has a value
        State = True
        Debug.Print (ctl.Name)
        Exit For
    End If
Next ctl
Me.comSubject.Enabled = State

The code above does not throw an error, but it doesn’t work.  If a
checkbox does not have a value (but its background is white) it’s
evaluating to not null.  If the checkbox does not have a value (but
its background is grey…when you start a new record) it IS evaluating
to null (and the code works).

What I need to do is change the variable [State] to False if the
ctl.Tag = “Grouping1” And any of the controls with a Tag of
“Grouping1” has a value.  I even tried ctl.Value = True, but that
didn’t work either.  I'm confused about what value an "empty" checkbox
possesses.  Apparently it's not null.

Regarding your question about “Grouping2”…
Instead of Me.comSubject.Enabled = State, I’d like to be able to
change the enabled property of any control with a Tag of “Grouping2.”

Hope that helps.
Thanks again,
alex- Hide quoted text -

- Show quoted text -

Not sure why, but I had to drop the ctl value to another if statement
like so:
For Each ctl In Me.Controls
If ctl.Tag = "Grouping1" Then 'first group
If ctl.Value = -1 Then 'has value
State = True
'Debug.Print (ctl.Value) '(ctl.Name)
Exit For
End If
End If
Next ctl

The code above seems to work fine. I'm still working on the
"Grouping2" question.
thanks,
alex
 
S

Stefan Hoffmann

hi Alex,

Regarding your question about “Grouping2”…
Instead of Me.comSubject.Enabled = State, I’d like to be able to
change the enabled property of any control with a Tag of “Grouping2.”
This should be obivious:

For Each ctl In Me.Controls
If And ctl.Tag = "Grouping2" Then
ctl.Enabled = State
End If
Next ctl

mfG
--> stefan <--
 
A

alex

hi Alex,

On 17.12.2009 17:03, alex wrote:> Regarding your question about “Grouping2”…

This should be obivious:

For Each ctl In Me.Controls
   If And ctl.Tag = "Grouping2" Then
     ctl.Enabled = State
   End If
Next ctl

mfG
--> stefan <--

Thanks Stefan for your help!
alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top