Toggle choices within a user form

R

richardb

I have introduced a user form within a macro with two check boxes to give
users a choice. I want Selection of one check box to deselect the other
(without writing a lot of code to do this). I tried placing the check boxes
within a frame, hoping this would behave as with Access VBA. I also tried to
assign both check boxes to the same GroupName (which was also the name of the
frame) both in the property window and using code in the UserFrom Activate
Sub. These did not work and I could not find better on-line help. I would
appreciate someone's help with this. Thank you.
 
M

Marc Adams [marcmyword.com]

If you have two checkboxes only:

Private Sub CheckBox1_Click()
Me.CheckBox2.Value = Not Me.CheckBox1.Value
End Sub


Private Sub CheckBox2_Click()
Me.CheckBox1.Value = Not Me.CheckBox2.Value
End Sub

Basically when one checkbox is on, it evaluates to the opposite of the other
textbox.

Hope this helps.
 
R

richardb

Thank you. That works for two choices, but I was hoping for something that
works more like Access, where a frame takes on the value of alternate choices
within.
 
J

Jay Freedman

Is there a reason you're using check boxes instead of option buttons,
which normally behave exclusively?

A frame in a userform doesn't take on the value of anything -- it
doesn't even have a .Value property -- but you can use it as a
container. If you put any number of option buttons in a frame on a
userform, you can run code similar to this:

Private Sub CommandButton1_Click()
Dim i As Integer
For i = 0 To Frame1.Controls.Count - 1
If Frame1.Controls(i).Value Then
' do something with the selected one
MsgBox Frame1.Controls(i).Caption
Exit For
End If
Next i
Unload Me
End Sub
 
R

richardb

Jay, I should have referred to an "Option Group" as in Access, not a frame. I
don't find an option group in Word VBA. I also have not figured out how to
put toggle buttons on a user form so they alternate. I'd be grateful for a
tip and otherwise the solutions already provided will work nicely thank you.
 
J

Jay Freedman

There isn't an "option group" control in Word VBA as such, but there are two
ways to create something that works the same way. They're described in the
VBA Help topic "
Ways to create an option group
". One way is to use a frame, together with code like that I already showed.
The other way is to use the GroupName property of each option button.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top