PDA

View Full Version : [SOLVED] Select - deselect chkboxes



ilyaskazi
05-31-2005, 05:20 AM
I hv 5 chkboxes on my form.

Apart from 4, 1 is for selecting or deselecting all the rest 4 chkboxes.

If the value of this is true, select all else select none.

Also if i select any of the other 4 chkbox then the select-all chkbox should be reflected.

Means, if the value of any of the 4 chkbox is false then set select-all chkbox as false.

but keep the values of the other 3 same.

Bob Phillips
05-31-2005, 05:40 AM
Option Explicit

Private mEnableEvents As Boolean

Private Sub CheckBox1_Click()
With Me
If mEnableEvents Then
mEnableEvents = False
If .CheckBox1.Value Then
.CheckBox2.Value = True
.CheckBox3.Value = True
.CheckBox4.Value = True
.CheckBox5.Value = True
Else
.CheckBox2.Value = False
.CheckBox3.Value = False
.CheckBox4.Value = False
.CheckBox5.Value = False
End If
mEnableEvents = True
Me.Repaint
End If
End With
End Sub

Private Sub CheckBox2_Click()
With Me
If mEnableEvents Then
mEnableEvents = False
If Not .CheckBox2.Value Then
.CheckBox1.Value = False
ElseIf .CheckBox3.Value And .CheckBox4.Value And .CheckBox5.Value Then
.CheckBox1.Value = True
End If
mEnableEvents = True
End If
End With
End Sub

Private Sub CheckBox3_Click()
With Me
If mEnableEvents Then
mEnableEvents = False
If Not .CheckBox3.Value Then
.CheckBox1.Value = False
ElseIf .CheckBox2.Value And .CheckBox4.Value And .CheckBox5.Value Then
.CheckBox1.Value = True
End If
mEnableEvents = True
End If
End With
End Sub

Private Sub CheckBox4_Click()
With Me
If mEnableEvents Then
mEnableEvents = False
If Not .CheckBox4.Value Then
.CheckBox1.Value = False
ElseIf .CheckBox2.Value And .CheckBox3.Value And .CheckBox5.Value Then
.CheckBox1.Value = True
End If
mEnableEvents = True
End If
End With
End Sub

Private Sub CheckBox5_Click()
With Me
If mEnableEvents Then
mEnableEvents = False
If Not .CheckBox5.Value Then
.CheckBox1.Value = False
ElseIf .CheckBox2.Value And .CheckBox3.Value And .CheckBox4.Value Then
.CheckBox1.Value = True
End If
mEnableEvents = True
End If
End With
End Sub

Private Sub UserForm_Activate()
mEnableEvents = True
End Sub

Richie(UK)
05-31-2005, 06:29 AM
Hi xld,

Do we need the second If statement for Checkbox1? Could we not use:


Private Sub CheckBox1_Click()
With Me
If mEnableEvents Then
mEnableEvents = False
.CheckBox2.Value = .CheckBox1.Value
.CheckBox3.Value = .CheckBox1.Value
.CheckBox4.Value = .CheckBox1.Value
.CheckBox5.Value = .CheckBox1.Value
mEnableEvents = True
Me.Repaint
End If
End With
End Sub

Nothing wrong with the functionality of the original, this just seems a little more intuitive. What do you think?

Bob Phillips
05-31-2005, 06:51 AM
Hi Richie,


Nothing wrong with the functionality of the original, this just seems a little more intuitive. What do you think?

My view, for what it is worth, is that whilst I don't think it is more intuitive, I do feel it is tighter, which makes it better.

BTW the repaint isn't necessary. I put it in when testing and one part didn't work, but it wasn't the cause or the solution. Thus it should be removed because it is a performance drag.

It c an be simplified from a maintenance point as well by


Option Explicit

Private mEnableEvents As Boolean

Private Sub CheckBox1_Click()
With Me
If mEnableEvents Then
mEnableEvents = False
mEnableEvents = False
.CheckBox2.Value = .CheckBox1.Value
.CheckBox3.Value = .CheckBox1.Value
.CheckBox4.Value = .CheckBox1.Value
.CheckBox5.Value = .CheckBox1.Value
mEnableEvents = True
mEnableEvents = True
End If
End With
End Sub

Private Sub CheckBox2_Click()
With Me
If mEnableEvents Then
SetCheckboxes .CheckBox2
End If
End With
End Sub

Private Sub CheckBox3_Click()
With Me
If mEnableEvents Then
SetCheckboxes .CheckBox3
End If
End With
End Sub

Private Sub CheckBox4_Click()
With Me
If mEnableEvents Then
SetCheckboxes .CheckBox4
End If
End With
End Sub

Private Sub CheckBox5_Click()
With Me
If mEnableEvents Then
SetCheckboxes .CheckBox5
End If
End With
End Sub

Private Sub UserForm_Activate()
mEnableEvents = True
End Sub

Private Sub SetCheckboxes(thisCb As MSForms.CheckBox)
With Me
mEnableEvents = False
If Not thisCb.Value Then
.CheckBox1.Value = False
ElseIf .CheckBox2.Value And .CheckBox3.Value And _
.CheckBox4.Value And .CheckBox5.Value Then
.CheckBox1.Value = True
End If
mEnableEvents = True
End With
End Sub

ilyaskazi
06-01-2005, 02:08 AM
xld,

what was the different between your first posted code and this new one??

both seems to be working fine.

Bob Phillips
06-01-2005, 02:40 AM
what was the different between your first posted code and this new one??

Two things.

I incorporated Richie's suggestions.

I used a separate procedure to do the setting/unsetting, rather than do it in each checkbox event. In the checkbox event code I pass the checkbox object to that shared procedure.

That is from



Private Sub CheckBox2_Click()
With Me
If mEnableEvents Then
mEnableEvents = False
If Not .CheckBox2.Value Then
.CheckBox1.Value = False
ElseIf .CheckBox3.Value And .CheckBox4.Value And .CheckBox5.Value Then
.CheckBox1.Value = True
End If
mEnableEvents = True
End If
End With
End Sub

to


Private Sub CheckBox2_Click()
With Me
If mEnableEvents Then
SetCheckboxes .CheckBox2
End If
End With
End Sub

and the extra procedure.

The advantage to this is that if you want to change what happens, you only have to do it in one place, not every checkbox event.

Also, see the post with stapuff on usefrom pseudo control arrays http://www.vbaexpress.com/forum/showthread.php?t=3335&page=2

ilyaskazi
06-01-2005, 03:43 AM
alright, thanku.

I hv used the next code and working perfect.

ilyaskazi
06-03-2005, 01:00 AM
xld,

is it possible if any of this chkbox value is true then enable the "START" button else disable.

stapuff
06-03-2005, 07:24 AM
ilyaskazi (http://www.vbaexpress.com/forum/member.php?u=1717) vbmenu_register("postmenu_28695", true);

A suggestion for you (that I use). Do not show the "Start" button until a checkbox is selected.

On userform initialize -
set the buttons visible to false then add a 1 liner to xld's code that shows the button.


Me.CommandButton1.Visible = False


If mEnableEvents Then
SetCheckboxes .CheckBox2
Me.CommandButton1.Visible = True
End If



Kurt