Check all boxes button

D

Debbiedo

I have a command button that selects/deselect all check boxes on a
form.

The problem is when I click it after opening and nothing has been
selected yet (we are at a new record), it selects all but the first
check box and when I click again, it selects the first check box and
deselects the rest.

Also, if even one of the check boxes is checked and then I click the
command button, not all the check boxes are selected, only the ones
not selected prior to clicking. The one(s) that were selected are now
deselected. So it is not a true "Select All Check Boxes" command.

I have five check boxes. The user can select any number of boxes. In
most cases they will be selecting all 5, which is why I want a "select
all" button.

What am I doing wrong? None of my other code refers to the check
boxes. All the properties for the check boxes are the same (except for
the Name, etc.)

Below is my code. I am using Access 2003

Private Sub Command24_Click()
Dim ctl As Control
For Each ctl In Me.Controls
' Check to see if control is check box.
If ctl.ControlType = acCheckBox Then
If ctl.Value = False Then
ctl.Value = True
Else
ctl.Value = False
End If
End If
Next ctl
End Sub

Thanks

Deb
 
O

OldPro

I have a command button that selects/deselect all check boxes on a
form.

The problem is when I click it after opening and nothing has been
selected yet (we are at a new record), it selects all but the first
check box and when I click again, it selects the first check box and
deselects the rest.

Also, if even one of the check boxes is checked and then I click the
command button, not all the check boxes are selected, only the ones
not selected prior to clicking. The one(s) that were selected are now
deselected. So it is not a true "Select All Check Boxes" command.

I have five check boxes. The user can select any number of boxes. In
most cases they will be selecting all 5, which is why I want a "select
all" button.

What am I doing wrong? None of my other code refers to the check
boxes. All the properties for the check boxes are the same (except for
the Name, etc.)

Below is my code. I am using Access 2003

Private Sub Command24_Click()
Dim ctl As Control
For Each ctl In Me.Controls
' Check to see if control is check box.
If ctl.ControlType = acCheckBox Then
If ctl.Value = False Then
ctl.Value = True
Else
ctl.Value = False
End If
End If
Next ctl
End Sub

Thanks

Deb

In your code you have
if ctlValue = False then
 
O

OldPro

I have a command button that selects/deselect all check boxes on a
form.

The problem is when I click it after opening and nothing has been
selected yet (we are at a new record), it selects all but the first
check box and when I click again, it selects the first check box and
deselects the rest.

Also, if even one of the check boxes is checked and then I click the
command button, not all the check boxes are selected, only the ones
not selected prior to clicking. The one(s) that were selected are now
deselected. So it is not a true "Select All Check Boxes" command.

I have five check boxes. The user can select any number of boxes. In
most cases they will be selecting all 5, which is why I want a "select
all" button.

What am I doing wrong? None of my other code refers to the check
boxes. All the properties for the check boxes are the same (except for
the Name, etc.)

Below is my code. I am using Access 2003

Private Sub Command24_Click()
Dim ctl As Control
For Each ctl In Me.Controls
' Check to see if control is check box.
If ctl.ControlType = acCheckBox Then
If ctl.Value = False Then
ctl.Value = True
Else
ctl.Value = False
End If
End If
Next ctl
End Sub

Thanks

Deb

This code sets the value of the chkbox opposite of its current status:
If ctl.Value = False Then
ctl.Value = True
Else
ctl.Value = False
End If
What you want is to force it to true:
If ctl.Value = False Then
ctl.Value = True
End If
 
R

ruralguy via AccessMonster.com

Hi Deb,
You created an "Invert the selection" button. Try this for a "Select All"
button:

Private Sub Command24_Click()
Dim ctl As Control
For Each ctl In Me.Controls
' Check to see if control is check box.
If ctl.ControlType = acCheckBox Then
ctl.Value = True
End If
Next ctl
End Sub
 
G

Guest

The design of your code is such that it is doing exactly what you designed it
to do. It simply says that whatever the value of the control is, set it to
the opposite value.

I would normally think of having two buttons for this. One button would
always select all, while the other would always deselect all. You would then
just have each of these buttons available (enabled) under specific
conditions. You may need a variable to help you with maintaining the
contitions, or you could use another user defined function to check the
status of all of the check boxes.

If you want a truly "change all" command button, then you could use a user
defined function to accomplish that.

Something like this:

Function ChangeAllCheckBoxes(Status as Boolean)
Me.NameOfChkBox1.value = Status
Me.NameOfChkBox2.value = Status
Me.NameOfChkBox3.value = Status
Me.NameOfChkBox4.value = Status
Me.NameOfChkBox5.value = Status
End Function

Then you would then call your function from the Click event of you command
buttons like this:

ChangeAllCheckBoxes True 'this would check all check boxes

or

ChangeAllCheckBoxes false 'this would uncheck all check boxes

If you want to use a variable to track the status of all command buttons,
you would first declare your variable. In this case you could just us a Byte
type veriable because you will never have a value greater than the total
number of checkboxes, which is 5.

At the beginning of your forms module, declare your variable like this:

Dim bolChkStatus as Byte

Then in the after update event of each of your check boxes, use code like
this:

If me.NameOfCheckBox = -1 then
bolChkStatus = bolChkStatus + 1
Else
bolChkStatus = bolChkStatus -1
End If

My assumption is that your form is opened to a status of all check boxes
unchecked. If you are opening your form in a condition where one or more of
these check boxes can be in a status of checked, then you will need some code
in the on current event of your form to detect if any check boxes are checked
and set the "bolChkStatus" variable to equal the total number of check boxes
that are checked. Then your other code would still function as designed.

--

HTH

Mr B
askdoctoraccess dot com
 

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