4

By selecting an OptionButton I want all CheckBoxes on the active form to deselect AND UNCHECK.

I can get it to deselect using the For Each Loop but this does not work for unchecking the boxes. I get the error:

chB.Value=Object variable or With block variable not set

Private Sub optB_9201_Click()

Dim ctrl As Control
Dim chB As CheckBox

If Me.optB_9201.Value = True _
  And Me.optB_9251.Value = False _
  And Me.optB_9301.Value = False Then

    Me.img9301_main.Visible = True
    Me.frM9301_View.Caption = "Du har valgt CLX-9201NA med følgende konfigurasjon:"
    Me.frm9301_Equipment.Enabled = True

    For Each ctrl In Me.frm9301_Equipment.Controls
        ctrl.Enabled = False
        chB.Value = False
    Next ctrl

    Me.frM9301_Stand.Enabled = True

    For Each ctrl In Me.frM9301_Stand.Controls
        ctrl.Enabled = True
    Next ctrl

End If

End Sub

How to fix this?

Alternatively:

Is is possible to have a change event on the UserForm that states that if a CheckBox is Enabled= False Then Value=False.

This way I would not have to put in the For Each Loop on every OptionButton. I tried the UserForm_Change Sub and UserForm_Click but nothing seems to work.

2
  • 1
    David's answer below solves your issue. Also, in your initial if statement you don't need to test for True and False explicitly. Instead use: If Me.optB_9201 And Not Me.optB_9251 And Not Me.optB_9301 Then. Commented Jun 14, 2014 at 15:33
  • I wrote a post about checkboxes working together which might be of interest. Commented Jun 14, 2014 at 18:20

1 Answer 1

8

You never Assign anything to chb (and I'm not sure that you need to use that variable at all). You could do:

For Each ctrl In Me.frm9301_Equipment.Controls
    ctrl.Enabled = False
    ctrl.Value = False
Next ctrl

This will only work if all controls are CheckBoxes. If that is not the case, then just add some if/then logic:

For Each ctrl In Me.frm9301_Equipment.Controls
    If TypeName(ctrl) = "CheckBox" Then
        ctrl.Enabled = False
        ctrl.Value = False
    End If
Next ctrl
Sign up to request clarification or add additional context in comments.

7 Comments

That worked like a charm. Thanks David:) I tried your first option before I posted but that did not work for me even though all controls are CheckBoxes. But your second option using the TypeName solved it!!
If the first option didn't work, then the form contains some controls which aren't checkboxes (textboxes, comboboxes, option buttons, listboxes, etc., -- even labels are controls on a userform). Glad it worked for you !
This is a big help, thanks. Make sure to note that the line If TypeName(ctrl) = "CheckBox" is case sensitive!
@PGCodeRider string comparisons are always case sensitive unless you use Option Compare Text.
@PGCodeRider I understand sure, and you're not wrong per se but we can't use every answer as an opportunity to hand-hold every potential problem a novice dev might encounter -- almost, if not every language treats strings distinctly by case. failing to account for this is almost surely among the first lessons that every dev learns, even if self-taught. It's why VBA has an LCASE and UCASE function, why python has a lower() and upper() method, why C# has a ToLower(), etc. :)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.