checkbox code

  • Thread starter Thread starter Thrava
  • Start date Start date
T

Thrava

hi folks,

I want to test whether a checkbox is checked or not,
i've written this code, but no matter whether or not I
check the box or un-check it, it gives me the same message
that it is checked.
Can someone tell me what am I doing wrong?

Private Sub CheckBox1_Click()
If CheckBox1.Value = 1 Then
MsgBox ("NONE SELECTED")
Else: MsgBox ("CHECKED")

End If

End Sub
 
Thrava,

The Value property of the CheckBox is a boolean value, either
True or False. Numerically, True is equal to -1, not 1, and so
your test will always fail, and "CHECKED" will appear. Try

If Me.CheckBox1.Value = True Then
MsgBox "Box is checked"
Else
MsgBox "Box is not checked"
End If


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
Thrava,

Try changing your line of code that reads:

If CheckBox1.Value = 1 Then

to

If CheckBox1.Value = False Then

your code should work as wanted.

Regards

Seamu
 
Got it, Thanks
But out of curiousity, why is true equal to -1? its
usually equal to 1 no?
 
Thrava,
But out of curiousity, why is true equal to -1? its
usually equal to 1 no?

False, or 0, is represented by all bits in the variable set to 0.
The opposite of False, or True, is represented internally by all
bits set to 1, which is -1 in signed integers. Some languages
define True to be 1; it depends on the programming lanugage
design. VB uses -1, while C++ uses +1. Excel, in formulas, uses
+1.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
thank you sir

-----Original Message-----
Thrava,


False, or 0, is represented by all bits in the variable set to 0.
The opposite of False, or True, is represented internally by all
bits set to 1, which is -1 in signed integers. Some languages
define True to be 1; it depends on the programming lanugage
design. VB uses -1, while C++ uses +1. Excel, in formulas, uses
+1.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com






.
 
Back
Top