• Triple State Checkbox (VB6)

    Author
    Topic
    #367209

    I know you can have triple state check boxes in Access but I don’t see a way to have this in VB. I want to have a ‘true’, ‘false’ or ‘not selected’ (greyed out) status.

    Anyone know how to do this? Do I need to get a 3rd party control?

    Viewing 2 reply threads
    Author
    Replies
    • #571902

      In VBA you set the set the TripleState property to true, and whilst I do not have VB6 available to me just know, I woul de surprised it was any different.

      Try CheckBox1.TripleState = True, in your FormLoad event.

      Andrew C

    • #571909

      VB does not act at all like Access. If you have a CheckBox, it can have a value of vbChecked or vbUnchecked (corresponding to checked or not), plus it has an Enabled property which is either True or False (corresponding to grayed-out or not), so it it like a QuadState! See this thread for some more info on transition from Access to VB.

      Try this example: put an array of four checkboxes on a new form along with a single command button and enter this code:

      Option Explicit
      Dim InCheckEvent
      
      Private Sub Check1_Click(Index As Integer)
          If InCheckEvent Then Exit Sub
          InCheckEvent = True
          Check1((Index +1) Mod 4).Enabled = True
          Check1((Index+1) Mod 4).Value = vbUnchecked
          Check1((Index +2) Mod 4).Enabled = False
          Check1((Index+2) Mod 4).Value = vbChecked
          Check1((Index +3) Mod 4).Enabled = False
          Check1((Index+3) Mod 4).Value = vbUnchecked
          DoEvents
          InCheckEvent = False
      End Sub
      
      Private Sub Command1_Click()
      Dim cb As CheckBox
          InCheckEvent = True
          For Each cb in Check1
              cb.Enabled = True
              cb.Value = vbUnchecked
          Next cb
          InCheckEvent = False
      End Sub
      • #571930

        Sam,
        I recall reading somewhere (no idea where though!) that checkboxes in VB could have values of 0 (unchecked), 1 (checked) or 2 (dimmed). Is my memory failing me? (I hope not – I’m too young for that to be kicking in already! grin)

        • #571938

          hairy You are absolutely correct, Rory! I never knew it, but there is a vbGrayed. If you set the Value of a checkbox to vbGrayed, it is checked and dimmed and the Enabled property is unchanged, so a checkbox can have six different states.

          Now I have a question, what use is a dimmed checkbox and what does a user do to dim a checkbox?

          • #571943

            Never having used VB proper, only VBA, I couldn’t say for sure but I assume it’s the same as an Access triplestate checkbox – you have Yes/No/Unspecified(Null in Access). If you don’t want a default value for a checkbox, you’d set its triplestate property to true and then you know whether it was specifically UNchecked rather than ignored. I may, of course, be entirely wrong!
            I don’t think there is actually a way for a user to dim it other than not touching it at all.

            **Edit**
            Not sure what I was thinking of – in Access at any rate, clicking on a triplestate checkbox cycles through all three possible states. Perfect for Yes/No/Don’t know questionnaires!

            • #571960

              Having had a chance to check out the VB Checkbox, it can have one of 3 possible value initially, but once clicked can then only alternate between True and False.

              It seems it cannot be given any value other tan 0 or 1 from code, and 2 (Grayed) has to set at design time.

              So I would suggest the best use for it is to accept a global default if left in grayed state, otherwise True or False to apply a specific value.

              VBA Checkboxes do cycle through 3 possible values if set to TripleState, Null, 0 and 1

              Andrew C

            • #571982

              But you could easily use the click event to cycle thru the three states, just make sure you use a global varable to “disable” the click event when you click it in code like I did in the example above.

        • #572080

          Yes, VB checkboxes can have three states.
          Look at the Value property.

    • #572048

      There’s been a lot of discussion on how to do this and not a single question as to why. The null state of a checkbox normally indicates that it hasn’t been touched, which is why you can’t cycle through the states easily. True or False indicate that a value has been set. What are you trying to achieve with a triple-state checkbox anyhow?

      • #572368

        The reason I wanted a triple state check box is for when users set options to download data from a datasource – e.g. if there is a boolean field ‘sold’ , I want to be able to select items that are set to TRUE (check box TRUE), FALSE (check box FALSE), or both (check box unselected).

        So basically I want to be able to select true, false or either – like you can do in Access.

        Thanks

        • #572377

          But if you don’t set a default stste for the control or you set the default to Null, wouldn’t that serve the same purpose without requiring any “cycling”? You wouldn’t ordinarily include a criteria condition in a query for a field where you wanted all records, regardless of value.

          • #572799

            I am creating an ‘advanced search criteria’ form and I would like users to be able to cycle through the three states on one of the check boxes.

            • #572859

              Here is the click event that you need to cycle:

              Option Explicit
              Dim InCheckEvent As Boolean
              Dim Check2Value As CheckBoxConstants
              Private Sub Check2_Click()
                  If InCheckEvent Then Exit Sub
                  InCheckEvent = True
                  Check2Value = (Check2Value + 1) Mod 3
                  Check2.Value = Check2Value
                  DoEvents
                  InCheckEvent = False
              End Sub
            • #572987

              I came up with this:

              ‘//cycle through True, False & vbGrayed
              If chkIncDel(Index) = vbTrue Then
              i = i + 1
              Else
              If chkIncDel(Index) = vbFalse Then
              i = i + 1
              End If
              End If

              If i = 2 Then
              chkIncDel(Index) = vbGrayed
              i = 0
              End If

              i is an integer set to 0 on form load.

    Viewing 2 reply threads
    Reply To: Triple State Checkbox (VB6)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: