Checkboxes in a form

User avatar
ErikJan
BronzeLounger
Posts: 1244
Joined: 03 Feb 2010, 19:59
Location: Terneuzen, the Netherlands

Checkboxes in a form

Post by ErikJan »

(Excel 2010)

Sometimes I seem to be blind and can't see the obvious

I'm building a form; several check-boxes; two problems:

(1) When I click one checkbox, I want two others to uncheck and vv. So in the "Sub Chk1_Click()", I do: "Myform.Chk2=false"
But the latter triggers the "Sub Chk2_Click()" (where I do the opposite). So I added "Application.EnableEvents=False" before (and True) after the Chk2=false call. That doesn't stop the other sub from triggering however... I must be overlooking something simple...

(2) One of my checkboxes is smaller (the font is the same, but the box is smaller)... where did that come from and how do I undo this?

User avatar
HansV
Administrator
Posts: 78492
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Checkboxes in a form

Post by HansV »

1) Use option buttons (aka radio buttons) instead of check boxes, and assign the same GroupName property. The user will then be able to turn on only one of the option buttons.

2) Hopefully, this problem will be gone when you replace the check boxes with option buttons.
Best wishes,
Hans

User avatar
ErikJan
BronzeLounger
Posts: 1244
Joined: 03 Feb 2010, 19:59
Location: Terneuzen, the Netherlands

Re: Checkboxes in a form

Post by ErikJan »

I did that in another form; for this one I need check boxes as multiple choices are allowed. I need to intervene as in two cases, options don't match. So I need checkboxes. In summary; how do I programmatically switch one off if I select another one and v.v. (without using optionboxes).

Any idea about the size?? It's quite odd... I manually added them and compared all properties; still one check-mark is smaller than the other...

User avatar
HansV
Administrator
Posts: 78492
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Checkboxes in a form

Post by HansV »

Define a Boolean variable at the top of the module and use that to control when the event procedures perform their actions:

Code: Select all

Private blnStop As Boolean

Private Sub Chk1_Click()
    If blnStop Then Exit Sub
    blnStop = True
    Me.Chk2 = False
    blnStop = False
End Sub
    
Private Sub Chk2_Click()
    If blnStop Then Exit Sub
    blnStop = True
    Me.Chk1 = False
    blnStop = False
End Sub
I'd have to see the form to be able to answer your other question.
Best wishes,
Hans

User avatar
ErikJan
BronzeLounger
Posts: 1244
Joined: 03 Feb 2010, 19:59
Location: Terneuzen, the Netherlands

Re: Checkboxes in a form

Post by ErikJan »

Thanks, that works!

The sizes; I'm still playing around...