Displaying text based on multiple checkboxes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Xenver
    New Member
    • Jul 2014
    • 20

    Displaying text based on multiple checkboxes

    Hello All,

    So I've got a form with multiple check boxes on it, the user makes selections on this form then clicks next, this opens a second form, on this form i would like to display text in an unbound text box based on the boxes the user has selected.

    so if the user selects check box 1,5,and 7 i would like the text box to display:

    1,5,7

    Is this possible?

    Thanks.
  • twinnyfo
    Recognized Expert Moderator Specialist
    • Nov 2011
    • 3662

    #2
    Xenver,

    Yes, this is possible, but you would have to have a specific naming convention for your Check boxes, or have the number for the check box entered into the .Tag property of the checkbox.

    You would simply build the string for your unbound text box by cycling through your checkboxes to see if they are checked:

    Code:
    Dim strValues As String
    
    strValues = ""
    
    If Me.chkBox1 then
        If strValues = "" then
            strValues = Me.chkBox1.Tag
        Else
            strValues = strValues & "," & Me.chkBox1.Tag
        End If
    end if
    
    If Me.chkBox2 then
        If strValues = "" then
            strValues = Me.chkBox2.Tag
        Else
            strValues = strValues & "," & Me.chkBox2.Tag
        End If
    end if
    
    . . . .
    
    Me.txtBox = strValues
    Hope this gets you moving in the right direction.

    Comment

    • Xenver
      New Member
      • Jul 2014
      • 20

      #3
      Hello,

      Thanks yes this is very helpful. I think i can use this to do what i want to do but... this is a little embarrassing but i don't know where to enter the code... whenever I've used code before it's been in the code builder for an event (on click etc.) Thanks for the quick response too, everyone on this forum is so helpful!

      Comment

      • Xenver
        New Member
        • Jul 2014
        • 20

        #4
        Ok,so I'm using the code in an OnLoad event, and translated to use my forms names it looks like this:

        Code:
        Private Sub Report_Load()
        
        Dim strValues As String
         
        strValues = ""
         
        If [Forms]![frm_Reports Credit]![SS 1] Then
            If strValues = "" Then
                strValues = [Forms]![frm_Reports Credit]![SS 1]
            Else
                strValues = strValues & "," & [Forms]![frm_Reports Credit]![SS 1].Tag
            End If
        End If
         
        If [Forms]![frm_Reports Credit]![SS 2] Then
            If strValues = "" Then
                strValues = [Forms]![frm_Reports Credit]![SS 2].Tag
            Else
                strValues = strValues & "," & [Forms]![frm_Reports Credit]![SS 2].Tag
            End If
        End If
         
         
        Me.DisplayCredits = strValues
        
        End Sub
        but i get an error on line 15, it says :

        "run-time error 94 invalid use of null"

        Thanks.

        Comment

        • twinnyfo
          Recognized Expert Moderator Specialist
          • Nov 2011
          • 3662

          #5
          Check to make sure that your check box has the Triple State Property set to No. This property allows a Check box to have three values "True", "False" and "Null" (when a value has not been specifically assigned to it.

          Comment

          • Xenver
            New Member
            • Jul 2014
            • 20

            #6
            Hey,

            I checked, and all the check boxes had their triple state property set to No, but for some reason when the form was first opened, until they were clicked they started with a blue fill, I'm guessing that indicated a null value because i went through and set all of their default values to 0 and the blue fill went away and everything works fine now. Thanks a lot!

            Comment

            • twinnyfo
              Recognized Expert Moderator Specialist
              • Nov 2011
              • 3662

              #7
              Glad we could be of some help. Let us know if you have any other questions.

              Comment

              Working...