Results 1 to 11 of 11

Thread: Why doesn't this work?!

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2000
    Location
    the UK
    Posts
    265

    Post

    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

  2. #2
    Addicted Member Razzle's Avatar
    Join Date
    Jan 2000
    Location
    Berlin, Germany
    Posts
    161

    Post

    instead of
    If chkbox1.value = true then
    write
    If chkbox1.value = 0 then

    ------------------
    Razzle
    ICQ#: 31429438

  3. #3
    Addicted Member Razzle's Avatar
    Join Date
    Jan 2000
    Location
    Berlin, Germany
    Posts
    161

    Post

    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

  4. #4
    New Member
    Join Date
    Feb 2000
    Posts
    12

    Post

    Dont know why. Weird.
    Try this instead, it works.
    txtbox1.Enabled = IIf(chkbox1.Value = 1, True, False)

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2000
    Location
    the UK
    Posts
    265

    Post

    Thanks gents!

    Razzle - that worked great.

    Simon

  6. #6
    Addicted Member Razzle's Avatar
    Join Date
    Jan 2000
    Location
    Berlin, Germany
    Posts
    161

    Post

    the problem was that only option boxes have True or False as Value.
    Checkboxes have 1 and 0

    ------------------
    Razzle
    ICQ#: 31429438

  7. #7
    Hyperactive Member
    Join Date
    Jun 1999
    Posts
    308

    Post

    Well PVB.
    In real live true is none Zero value, just in case.

  8. #8
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Post

    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



  9. #9
    Hyperactive Member
    Join Date
    Sep 1999
    Location
    Cleveland, Ohio
    Posts
    263

    Post

    Even simpler code!

    Text1.Enabled = Check1.Value

  10. #10
    Guest

    Post

    Phobic, very nice one liner.

  11. #11
    Guest

    Post

    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
  •  



Click Here to Expand Forum to Full Width