Checkboxes with Userform

D

Dao N

I've created a userform (in 2007) with checkboxes and 2 command buttons. To
activate the form I've created an icon on the Developer's menu.

For some reasons when I activate the form from the icon the checkboxes don't
work when I click on them. It appears as though they've been disabled. The
command buttons are the OK and Cancel buttons. They worked.

The checkbox code contains a basic toggle function, eg:

Sub CheckBox1_Click()
If CheckBox1.Value = True Then
CheckBox1.Value = False
Else
CheckBox1.Value = True
End If
End Sub

The bizzare thing is that when I activate the form from VB Editor the
checkboxes worked.

What am I doing wrong?

Pls help - I've been looking at codes for so such a long time and still
can't work out why it's not working!

Thank you in advanced.

Dao
 
G

Gordon Bentley-Mix

Dao,

Assuming that the code you posted is actually the code that's behind your
UserForm, then the problem is clear. Look at it again. The code - as I
interpret it - does the following:

When you click on the CheckBox1, it checks the CheckBox's value. If that
value is True, then the code immediately sets it back to False again
(CheckBox1.Value = False). In addition, if the value is False, it sets it
back to True - although this state could never be encountered unless the
UserForm was initialised with the value set to False.

I suspect that you intended to change the value of a different CheckBox
(CheckBox2?) so that they work something like OptionButtons where selecting
one automatically de-selects the other. If I might make a couple of
recommendations.

First, try using descriptive names for your UserForm controls. This may help
you to avoid making mistakes like this as the intended control becomes much
more obvious when the name is clear.

Second, if you have similar code for the Click event for CheckBox2, then I'd
suggest using OptionButtons rather than CheckBoxes. Even multiple
OptionButtons on the same UserForm can still be made to behave in the way
that you appear to be trying to get these CheckBoxes to work. Simply place
them in a Frame control. The border on the Frame can be made invisible so it
doesn't spoil the look of your UserForm.

As for why this code seemed to work in the VBE is anybody's guess, but I
can't see how it would work regardless of how it was invoked.
--
Cheers!

Gordon Bentley-Mix
Word MVP

Please post all follow-ups to the newsgroup.

Read the original version of this post in the Office Discussion Groups - no
membership required!
 
I

in_paris_not_a_programmer

Dao,

I struggled with the same problem. The answer's easy -- you don't need any
of that code, the form handles the check/uncheck for you! You just read off
the value when you're ready (e.g. when the user presses OK)

Regards.
 

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