|
-
Feb 5th, 2000, 08:47 PM
#1
Thread Starter
Hyperactive Member
Sorry for the cryptic subject line - didn't how to describe it!
I have a checkbox on a form. When the checkbox is checked I want a textbox underneath to become enabled and when the checkbox is NOT checked, I want the textbox to be NOT enabled. Any idea why this won't work:?
Private Sub chkbox1_Click()
If chkbox1.value = true then
txtbox1.Enabled = True
else
txtbox1.Enabled = false
End If
End Sub
Simon Pearce
Network Manager
Higher Side School
-
Feb 5th, 2000, 09:02 PM
#2
Addicted Member
instead of
If chkbox1.value = true then
write
If chkbox1.value = 0 then
------------------
Razzle
ICQ#: 31429438
-
Feb 5th, 2000, 09:04 PM
#3
Addicted Member
sorry, got it backwards
this enables the textbox when the checkbox is disabled
write
If chkbox1.value = 1 then
instead of
If chkbox1.value = 0 then
SORRY
------------------
Razzle
ICQ#: 31429438
-
Feb 5th, 2000, 09:07 PM
#4
New Member
Dont know why. Weird.
Try this instead, it works.
txtbox1.Enabled = IIf(chkbox1.Value = 1, True, False)
-
Feb 5th, 2000, 09:37 PM
#5
Thread Starter
Hyperactive Member
Thanks gents!
Razzle - that worked great.
Simon
-
Feb 5th, 2000, 09:54 PM
#6
Addicted Member
the problem was that only option boxes have True or False as Value.
Checkboxes have 1 and 0
------------------
Razzle
ICQ#: 31429438
-
Feb 6th, 2000, 03:52 AM
#7
Hyperactive Member
Well PVB.
In real live true is none Zero value, just in case.
-
Feb 6th, 2000, 06:14 AM
#8
If you do a check in the following manner:
If chkbox1.Value = True Then
You are specifically checking if the Value propert equals True (-1) this is not the case though. You could however do this:
If chkbox1.Value Then
The above statement is true if the value property is not false (i.e any value besides zero).
So LG in real life a statement that is not zero is also not false. But the value property of a checkbox will never be equal to -1 so checking for True will newer be true. Checking for False will work however since the value False equals zero and the Value property of a checkbox can be set to zero.
------------------
Joacim Andersson
[email protected]
[email protected]
www.YellowBlazer.com
-
Feb 6th, 2000, 06:19 AM
#9
Hyperactive Member
Even simpler code!
Text1.Enabled = Check1.Value
-
Feb 6th, 2000, 10:06 AM
#10
Phobic, very nice one liner.
-
Feb 6th, 2000, 12:23 PM
#11
Clarification. In Visual Basic, True has a value of -1 and False has a value of 0. check your reference books if you don't want to believe me.
Remember that a checkbox has 3 states, none of them directly correlating to True or False. The Value property of the checkbox can be:
vbUnChecked - which is 0 (which happens to also be the value for False)
vbChecked - which is 1
vbGrayed - which is 2
if all you care about is finding out whether the checkbox has a check in it, meaning it's in either a checked or grayed state, then the following would work:
IIf(Check1, True, False)
if the checkbox(named Check1) value is 1(vbChecked which is not the value of True or vbGrayed which has a value of 2) then Check1(without the .Value) will return True. If Check1 has no check and is not in the grayed state then Check1 returns False.
This is kind of confusing to explain, my point is that don't confuse True/False(-1/0) values with the possible values for the state of a Checkbox.
You can set the .Value property of a checkbox to vbUnchecked which is equal to zero or False because the value of False is also zero. You cannot set the .Value property of a checkbox to True because there are two true possibilities, vbChecked and vbGrayed(both are checked) and VB won't pick one of them for you. VB returns True if there is any check in the checkbox because it knows that the value is not zero. If zero is False then not zero is True.
so in answer to the original post, this would work:
Private Sub Check1_Click()
If Check1 Then
txtbox1.Enabled = True
Else
txtbox1.Enabled = False
End If
End Sub
this is kind of a quirky vb thing, but you cannot test to see if check1 or check1.value is equal to true. that would be like testing to see if the value of check1 is equal to -1 which is not a possible value for the Value property of a checkbox. take out the assignment operator(=), now vb is not concerned with the actual possible values for the the value property, vb is concerned with 0(False) and not 0(True).
[This message has been edited by pvb (edited 02-06-2000).]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|