Enable button based on multiple checkboxes - Help!

H

hermanko

Hi all,

I have a form based on a query that includes a Yes/No field as
checkboxes. I have a command button on the form footer that is default
at disabled when the form is opened (the checkboxes are all cleared as
well).


I need to enable the button once any checkbox is selected. The code I
have now only works partially. For example if i click any one checkbox,

the button enables fine. When i uncheck, it disables fine. The problem
occurs when I check several boxes, and as soon as I uncheck any one
box, the button will disable, even tho I still have other boxes
checked. Basically I need to fix my code so that when ANY one or more
checkbox is True i need the button to remain enabled. It doesn't sound
too hard to fix but i'm not strong with VB. My code is placed in the
AfterUpdate
event on the checkbox control:

Private Sub Yes_No_AfterUpdate()
If Me![Yes/No] Then
Me!cmd_remove1.Enabled = True
Else
Me!cmd_remove1.Enabled = False
End If
End Sub

I am thinking that it's acting this way BECAUSE i am running the code
at each individual checkbox. But i need a way for the form to see if
ANY one or more checkbox on the entire form is selected, and thus keep
the button enabled.

Any help would be great!
Herman
 
G

Guest

I'd add code to see if the button is already enabled, and leave it alove if
it is, unless ALL checkboxes are cleared.
 
J

Jeff Boyce

Herman

I believe you've identified your solution!

If you need, at a FORM level, to check for the checkboxes, then you could
use a form-level procedure, rather than a procedure behind each checkbox.

You could create a procedure that adds up all the checkboxes -- if the
checkboxes are bound to yes/no fields, a 0 is stored for No and a -1 is
stored for Yes (in Access, other non-zero values for other database
backends). Therefore, if the sum of your checkboxes is not 0, at least one
of them is checked!

You'd need to call this routine in the AfterUpdate event of each checkbox.

Regards

Jeff Boyce
Microsoft Office/Access MVP
 
G

Guest

Herman,

Here is an example of what Jeff is talking about. (I keep forgetting about
being able to add the boolean fields)

I used a little different way than Jeff.

Watch for line wrap!!

' code behind the check boxes
' all of the check boxes need to be like this
'----------------------------------
Private Sub Yes_No_AfterUpdate()
'call the sub
CheckState_1

'or you could use
'CheckState_2
End Sub

Private Sub chk1_AfterUpdate()
CheckState_1

'or you could use
'CheckState_2
End Sub

'--------------------------------

' the sub that changes the enabled property

Sub CheckState_1()
'all check boxes need to be listed here
'change the names to your check boxes
If Me.[Yes_No] = True Or Me.[chk1] = True Or Me.[chk2] = True Or
Me.[chk3] = True Then
Me!cmd_remove1.Enabled = True
Else
Me!cmd_remove1.Enabled = False
End If

End Sub

'-----

'second way - less code but harder to read/understand

Sub CheckState_2()
'change the names to your check boxes
'all check boxes need to be listed here
Me!cmd_remove1.Enabled = Not (Me.[chk1] = False And Me.[chk2] = False
And Me.[chk3] = False)

End Sub

HTH
--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)


Jeff Boyce said:
Herman

I believe you've identified your solution!

If you need, at a FORM level, to check for the checkboxes, then you could
use a form-level procedure, rather than a procedure behind each checkbox.

You could create a procedure that adds up all the checkboxes -- if the
checkboxes are bound to yes/no fields, a 0 is stored for No and a -1 is
stored for Yes (in Access, other non-zero values for other database
backends). Therefore, if the sum of your checkboxes is not 0, at least one
of them is checked!

You'd need to call this routine in the AfterUpdate event of each checkbox.

Regards

Jeff Boyce
Microsoft Office/Access MVP


Hi all,

I have a form based on a query that includes a Yes/No field as
checkboxes. I have a command button on the form footer that is default
at disabled when the form is opened (the checkboxes are all cleared as
well).


I need to enable the button once any checkbox is selected. The code I
have now only works partially. For example if i click any one checkbox,

the button enables fine. When i uncheck, it disables fine. The problem
occurs when I check several boxes, and as soon as I uncheck any one
box, the button will disable, even tho I still have other boxes
checked. Basically I need to fix my code so that when ANY one or more
checkbox is True i need the button to remain enabled. It doesn't sound
too hard to fix but i'm not strong with VB. My code is placed in the
AfterUpdate
event on the checkbox control:

Private Sub Yes_No_AfterUpdate()
If Me![Yes/No] Then
Me!cmd_remove1.Enabled = True
Else
Me!cmd_remove1.Enabled = False
End If
End Sub

I am thinking that it's acting this way BECAUSE i am running the code
at each individual checkbox. But i need a way for the form to see if
ANY one or more checkbox on the entire form is selected, and thus keep
the button enabled.

Any help would be great!
Herman
 
H

hermanko

Thanks guys for your posts

I should mention that my form is a continuous form, and it's based on
an underlying query. That means, the number of records (therefore
available checkboxes bound to my Yes/No field) changes everytime i load
my form based on what's happening in the query.

so the coding u provide works, but only for a fixed number of
checkboxes.
 
J

Jeff Boyce

Then you might need to use a query or a DSum() function to total the
checkboxes.

Regards

Jeff Boyce
Microsoft Office/Access MVP
 
G

Guest

The *number* of ROWS doesn't matter.

Look at the form in design view. Count the number of check box controls.
That is how many check box controls you have on the form, and the number of
check boxes you would have to reference in the code to set the enabled
property of the command button.

I don't understand what you mean by:

"I have a form based on a query that includes a Yes/No field as
checkboxes. "

and

"...check boxes bound to my Yes/No field..."


~~ How many boolean fields do you have in the query?

~~ How many check boxes are on the form?

~~ Are the check boxes bound to different boolean fields? Or all check boxes
bound to the same field? Or are some unbound?
 

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