PDA

View Full Version : Uncheck boxes in one group only



drm51
04-30-2013, 05:28 AM
Hi,
I am referring back to a post “Solved: Uncheck Checkboxes if one is checked”, but have additional questions for this post. I am not familiar as how to reopen a “Solved” post, so I will post a new one. I am very limited in my knowledge of VBA.

The code showed in the above post and the one I listed below again, works great for checkboxes 1-5 alone, but I have many sets of checkboxes with 2 to 5 checkboxes in them that I would like to be independent of each other. If you check a checkbox within a set, the other checkboxes in that set will be unchecked, but it would not affect the other sets of checkboxes. I need for it to have only one checkbox in a set to be checked at a time.

I have listed the code presented former post as below:

Private Sub CheckBox1_Change()
SetCheckBoxes 1
End Sub

Private Sub CheckBox2_Change()
SetCheckBoxes 2
End Sub

Private Sub CheckBox3_Change()
SetCheckBoxes 3
End Sub

Private Sub CheckBox4_Change()
SetCheckBoxes 4
End Sub

Private Sub CheckBox5_Change()
SetCheckBoxes 5
End Sub


Private Function SetCheckBoxes(CBIndex As Long)
Dim x As Long

If Me.OLEObjects("CheckBox" & CBIndex).Object.Value Then
For x = 1 To 5
If x <> CBIndex Then
Me.OLEObjects("CheckBox" & x).Object.Value = False
End If
Next x
End If
End Function

Like I stated, this code works great for just ONE set “group” of checkboxes. How do I change this code to work independently for each set of checkboxes for multiple sets of checkboxes each having 2 to 5 checkboxes in them? There would be between 50 to 100 sets of checkboxes having 2-5 checkboxes in each, and up to 8 to 10 sheets.

Thank you very much for any help you can provide.


drm51

SamT
04-30-2013, 07:31 AM
:welcome:

snb
04-30-2013, 07:56 AM
For that purpose you'd better use optionbuttons.

Gingertrees
05-07-2013, 10:13 AM
For that purpose you'd better use optionbuttons.
While that'd be great, I know people print these out, and option buttons have a tendency to leave one option in a group selected regardless.

So... is this still a possibility? With checkboxes?

SamT
05-07-2013, 02:24 PM
You can create your own grouping with a custom collection or Dictionary, where the Index or Key is a checkbox ID and the Item is an unchecker Sub name... When the Checkbox is clicked, pass the ID to a function that uses a Select Case Collection(ID) to choose which unchecker sub to use.

You could just pass the ID to the Select case, but I think the collection/Dictionary is easier code to maintain.