Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question about check box in vb

Status
Not open for further replies.

Longia

Programmer
Dec 8, 2000
23
US
Is there a way to control the value of multiple checkboxes simultaneously?
I have searched internet for sample code, but found none that worked. I
have the code below but very slow and I imagine can be done simpler.
Is there a way to create an array of checkboxes? Could you suggest things
I can try?

Also, how do I 'upgrade' Excel spreadsheet with Macro 4.0 to Excel 2000?
I removed all the shapes/controls, changed some code, but still have
Macro 4.0 signature somewhere. What are the things I need to look out for?

MPCheckBox1 to 9 are checkboxes with no caption in Excel.


Private Sub ToggleAllPeriods_Click()
Dim toggle As Boolean

toggle = MPCheckBox1.Value
toggle = Not toggle

MPCheckBox1.Value = toggle
MPCheckBox2.Value = toggle
MPCheckBox3.Value = toggle
MPCheckBox4.Value = toggle
MPCheckBox5.Value = toggle
MPCheckBox6.Value = toggle
MPCheckBox7.Value = toggle
MPCheckBox8.Value = toggle
MPCheckBox9.Value = toggle
End Sub
 
You could also create a control array of checkboxes. By naming them the same name and referencing them with an index. This will allow you to control the checkboxes as one, rather that separate entities.

 
But would that have the downside of tying all of them
together, so he could'nt check just the ones he wanted?
 
THOMASNG
Of course he can check individual controls of a control array. That is what INDEX is for. e.g.
MPCheckBox(4).Value = toggle
Better read the docs more carefully.

'==============================
SARPREET
Problems.
1. You mentioned EXCEL so a Control Array is OUT...not supported.
2. A VB Checkbox (assuming VBA is the same) does not use True/False in the value. It has values 0 = vbUnChecked and 1 = vbChecked

So use...
toggle = MPCheckBox1.Value
If toggle = vbChecked then
toggle = vbUnChecked
else
toggle = vbChecked
End if


Generate Forms/Controls Resizing/Tabbing Class
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Using a control array and the indexes and looping through them in this case is best, if it is a control array, or using the For/Each may be even better.

Or, if you do not what to change the CheckBoxes to a cotrol array you could loop through the controls:

Dim ctl As Control
For each ctl In Me.Controls
If TypeOf ctl Is CheckBox Then
If InStr(ctl.Name,"MPCheckBox") Then'This could be changed to use the Left$ function
ctl.Value = Abs(Not(CBool(Check1.Value)))
End If
end if
Next ctl


Or you could use the ScriptControl object - but at this level I don't think it is a good idea....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top