Processing Multiple Check Boxes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jim in Arizona

    #1

    Processing Multiple Check Boxes

    I'm having dificulty figuring out how to process multiple check boxes on a
    web form.

    Let's say I have three check boxes:
    cbox1
    cbox2
    cbox3

    The only way I can think of to code the possibilities is something like:

    If cbox1.checked = true then
    ..........
    End if

    If cbox2.checked = true then
    .......
    End If

    If cbox3.checked = true then
    .......
    End if

    If cbox1.checked = true and cbox2.checked = true then
    .......
    end if

    If cbox1.checked = true and cbox3.checked = true then
    .......
    end if

    And the If/End IFs go on forever!

    As you can see, if I have around 8 check boxes, doing it this way could lead
    to tremendous amounts of coding. I'm sure there's a better, easier way,
    right? I thought of using a select case but that wouldn't be much better I
    don't think.

    The application is a query that runs against a single table in an access
    database. Each check box represents each field they could chose from to show
    up in a table or other style report.

    TIA,
    Jim



  • Terry Olsen

    #2
    Re: Processing Multiple Check Boxes

    Why not do something like:

    dim c as integer = 0
    if cbox3.Checked = True then c = 1
    if cbox2.Checked = True then c += 2
    if cbox1.Checked = True then c+=4

    Select Case c
    Case 0 'No checks
    Case 1 'cbox 3
    Case 2 'cbox 2
    Case 3 'cbox 2 & 3
    Case 4 'cbox 1
    Case 5 'cbox 1 & 3
    Case 6 'cbox 1 & 2
    Case 7 'cbox 1,2, & 3
    End Select

    Yeah, it's archaic...but I come from an Applesoft Basic background :)

    "Jim in Arizona" <tiltowait@hotm ail.com> wrote in message
    news:OWKhKiLjFH A.3332@TK2MSFTN GP10.phx.gbl...[color=blue]
    > I'm having dificulty figuring out how to process multiple check boxes on a
    > web form.
    >
    > Let's say I have three check boxes:
    > cbox1
    > cbox2
    > cbox3
    >
    > The only way I can think of to code the possibilities is something like:
    >
    > If cbox1.checked = true then
    > .........
    > End if
    >
    > If cbox2.checked = true then
    > ......
    > End If
    >
    > If cbox3.checked = true then
    > ......
    > End if
    >
    > If cbox1.checked = true and cbox2.checked = true then
    > ......
    > end if
    >
    > If cbox1.checked = true and cbox3.checked = true then
    > ......
    > end if
    >
    > And the If/End IFs go on forever!
    >
    > As you can see, if I have around 8 check boxes, doing it this way could
    > lead to tremendous amounts of coding. I'm sure there's a better, easier
    > way, right? I thought of using a select case but that wouldn't be much
    > better I don't think.
    >
    > The application is a query that runs against a single table in an access
    > database. Each check box represents each field they could chose from to
    > show up in a table or other style report.
    >
    > TIA,
    > Jim
    >
    >
    >[/color]


    Comment

    • Jan Hyde

      #3
      Re: Processing Multiple Check Boxes

      "Jim in Arizona" <tiltowait@hotm ail.com>'s wild thoughts
      were released on Tue, 19 Jul 2005 16:21:09 -0700 bearing the
      following fruit:
      [color=blue]
      >I'm having dificulty figuring out how to process multiple check boxes on a
      >web form.
      >
      >Let's say I have three check boxes:
      >cbox1
      >cbox2
      >cbox3
      >
      >The only way I can think of to code the possibilities is something like:
      >
      >If cbox1.checked = true then
      >.........
      >End if
      >
      >If cbox2.checked = true then
      >......
      >End If
      >
      >If cbox3.checked = true then
      >......
      >End if
      >
      >If cbox1.checked = true and cbox2.checked = true then
      >......
      >end if
      >
      >If cbox1.checked = true and cbox3.checked = true then
      >......
      >end if
      >
      >And the If/End IFs go on forever!
      >
      >As you can see, if I have around 8 check boxes, doing it this way could lead
      >to tremendous amounts of coding. I'm sure there's a better, easier way,
      >right? I thought of using a select case but that wouldn't be much better I
      >don't think.
      >
      >The application is a query that runs against a single table in an access
      >database. Each check box represents each field they could chose from to show
      >up in a table or other style report.[/color]

      Then why do you need conditions such as

      If cbox1.checked = true and cbox2.checked = true then...

      Can't you just build up your query by checking each checkbox
      in turn?




      Jan Hyde (VB MVP)

      --
      Occult: A young horse. (Keith Nance)

      [Abolish the TV Licence - http://www.tvlicensing.biz/]

      Comment

      • Jim in Arizona

        #4
        Re: Processing Multiple Check Boxes

        [color=blue]
        > Then why do you need conditions such as
        >
        > If cbox1.checked = true and cbox2.checked = true then...
        >
        > Can't you just build up your query by checking each checkbox
        > in turn?
        >
        >
        > Jan Hyde (VB MVP)
        >
        > --
        > Occult: A young horse. (Keith Nance)
        >
        > [Abolish the TV Licence - http://www.tvlicensing.biz/][/color]

        I'm still pretty new to programming so still trying to get some coding
        techniques established. I've never worked with check boxes.

        When you say "Can't you just build up your query by checking each checkbox
        in turn?", I'm not sure what you mean. Can you give me an example?

        Thanks Jan,
        Jim


        Comment

        • Jim in Arizona

          #5
          Re: Processing Multiple Check Boxes

          "Terry Olsen" <tolsen64@hotma il.com> wrote in message
          news:eh3mjFMjFH A.3656@TK2MSFTN GP09.phx.gbl...[color=blue]
          > Why not do something like:
          >
          > dim c as integer = 0
          > if cbox3.Checked = True then c = 1
          > if cbox2.Checked = True then c += 2
          > if cbox1.Checked = True then c+=4
          >
          > Select Case c
          > Case 0 'No checks
          > Case 1 'cbox 3
          > Case 2 'cbox 2
          > Case 3 'cbox 2 & 3
          > Case 4 'cbox 1
          > Case 5 'cbox 1 & 3
          > Case 6 'cbox 1 & 2
          > Case 7 'cbox 1,2, & 3
          > End Select
          >
          > Yeah, it's archaic...but I come from an Applesoft Basic background :)[/color]

          That's an interesting approach. My head is swimming a bit looking at it
          though. I like the idea and, if I can't find something else I can fuse into
          my head easier, I will definately give it a go.

          Thanks Terry,
          Jim


          Comment

          • Jim in Arizona

            #6
            Re: Processing Multiple Check Boxes

            "Terry Olsen" <tolsen64@hotma il.com> wrote in message
            news:eh3mjFMjFH A.3656@TK2MSFTN GP09.phx.gbl...[color=blue]
            > Why not do something like:
            >
            > dim c as integer = 0
            > if cbox3.Checked = True then c = 1
            > if cbox2.Checked = True then c += 2
            > if cbox1.Checked = True then c+=4
            >
            > Select Case c
            > Case 0 'No checks
            > Case 1 'cbox 3
            > Case 2 'cbox 2
            > Case 3 'cbox 2 & 3
            > Case 4 'cbox 1
            > Case 5 'cbox 1 & 3
            > Case 6 'cbox 1 & 2
            > Case 7 'cbox 1,2, & 3
            > End Select[/color]

            Hi Again Terry.

            Going over your example and putting it to work with 7 check boxes, I would
            get this then?

            if cbox7.checked = true then c = 1
            if cbox6.checked = true then c += 2
            if cbox5.checked = true then c += 4
            if cbox4.checked = true then c += 8
            if cbox3.checked = true then c += 16
            if cbox2.checked = true then c += 32
            if cbox1.checked = true then c += 64

            So, there would be a possible .. how many CASEs is that, 128? :) That's a
            cool way of doing it although a still a bit tedius. I can't imagine any way
            that isn't tedius though!

            I'm just trying to get the math down. In your example, are you doubeling
            each box down the line (ie: 1, 2, 4, 8 ...)? That seems right to me.

            Any idea of how I could more easily code 128 Cases?

            The Case statement would be executed when a submit button is clicked. The
            first thing that would happen is a connection to a database. The next would
            be the command object/sql statement. Each case would have to be its own SQL
            statement "SELECT " & cbox7.text " FROM Table1". Would the be some kind of
            easier way .. like, I don't know, making up some kind of array and looping
            through it? I'm just shooting in the dark here.

            A co-worker and I are trying to work up an idea using stored procedures but
            I'm not sure if we're going to succeed there or not.

            If there is no other way, then I'll just have to code all 128 of 'em. :)

            Thanks,
            Jim


            Comment

            • Al Reid

              #7
              Re: Processing Multiple Check Boxes

              "Jim in Arizona" <tiltowait@hotm ail.com> wrote in message news:OnBfkjUjFH A.708@TK2MSFTNG P09.phx.gbl...[color=blue]
              >
              > The Case statement would be executed when a submit button is clicked. The
              > first thing that would happen is a connection to a database. The next would
              > be the command object/sql statement. Each case would have to be its own SQL
              > statement "SELECT " & cbox7.text " FROM Table1". Would the be some kind of
              > easier way .. like, I don't know, making up some kind of array and looping
              > through it? I'm just shooting in the dark here.
              >
              > A co-worker and I are trying to work up an idea using stored procedures but
              > I'm not sure if we're going to succeed there or not.
              >
              > If there is no other way, then I'll just have to code all 128 of 'em. :)
              >
              > Thanks,
              > Jim
              >
              >[/color]

              Here is what I would do. First, I would place all of the CheckBoxes in a GroupBox, perhaps named grpFields. I would then place the
              field name in the Tag property of the CheckBox, but you could just use the Text. I would then loop through all of the controls on
              the GroupBox and process each one that is a CheckBox with the Tag net an empty string and dynamically build the select statement.
              It would look somerthing like:

              Dim strSQL As String = "SELECT "
              Dim bFirst As Boolean = True

              For Each ctl As Control In grpFields.Contr ols
              If TypeOf ctl Is CheckBox Then
              If ctl.Tag <> "" Then
              strSQL &= CStr(IIf(bFirst , "", ",")) & ctl.Tag
              bFirst = False
              End If
              End If
              Next

              strSQL &= " FROM Table1"

              At this point you can execut the SQL and return a recordset with the selected fields.

              I hope this helps.

              --
              Al Reid


              Comment

              • Jim in Arizona

                #8
                Re: Processing Multiple Check Boxes

                > Here is what I would do. First, I would place all of the CheckBoxes in a[color=blue]
                > GroupBox, perhaps named grpFields. I would then place the
                > field name in the Tag property of the CheckBox, but you could just use the
                > Text. I would then loop through all of the controls on
                > the GroupBox and process each one that is a CheckBox with the Tag net an
                > empty string and dynamically build the select statement.
                > It would look somerthing like:
                >
                > Dim strSQL As String = "SELECT "
                > Dim bFirst As Boolean = True
                >
                > For Each ctl As Control In grpFields.Contr ols
                > If TypeOf ctl Is CheckBox Then
                > If ctl.Tag <> "" Then
                > strSQL &= CStr(IIf(bFirst , "", ",")) & ctl.Tag
                > bFirst = False
                > End If
                > End If
                > Next
                >
                > strSQL &= " FROM Table1"
                >
                > At this point you can execut the SQL and return a recordset with the
                > selected fields.
                >
                > I hope this helps.
                >
                > --
                > Al Reid
                >[/color]

                Hi Al.

                Since I'm doing a web application (web form), a group box, which is part of
                system.windows. forms is not an option. Too bad I couldn't import the
                namespace and use it anyway huh?

                Thanks anyway though.

                Jim


                Comment

                • Terry Olsen

                  #9
                  Re: Processing Multiple Check Boxes

                  >[color=blue]
                  > Since I'm doing a web application (web form), a group box, which is part
                  > of system.windows. forms is not an option. Too bad I couldn't import the
                  > namespace and use it anyway huh?[/color]

                  Well, you could use a Panel or Placeholder I believe...


                  Comment

                  • Terry Olsen

                    #10
                    Re: Processing Multiple Check Boxes

                    [color=blue]
                    > I'm just trying to get the math down. In your example, are you doubeling
                    > each box down the line (ie: 1, 2, 4, 8 ...)? That seems right to me.
                    >[/color]

                    Yes. Think Binary...

                    0 = 0
                    1 = 1
                    01 = 2
                    11 = 3
                    100 = 4
                    101 = 5
                    110 = 6
                    111 = 7

                    But for efficiency and the least amount of code, I like the other suggestion
                    of putting the controls in a Panel or Placeholder and then looping through
                    each control.


                    Comment

                    • Jan Hyde

                      #11
                      Re: Processing Multiple Check Boxes

                      "Jim in Arizona" <tiltowait@hotm ail.com>'s wild thoughts
                      were released on Wed, 20 Jul 2005 08:41:34 -0700 bearing the
                      following fruit:
                      [color=blue]
                      >[color=green]
                      >> Then why do you need conditions such as
                      >>
                      >> If cbox1.checked = true and cbox2.checked = true then...
                      >>
                      >> Can't you just build up your query by checking each checkbox
                      >> in turn?
                      >>
                      >>
                      >> Jan Hyde (VB MVP)
                      >>
                      >> --
                      >> Occult: A young horse. (Keith Nance)
                      >>
                      >> [Abolish the TV Licence - http://www.tvlicensing.biz/][/color]
                      >
                      >I'm still pretty new to programming so still trying to get some coding
                      >techniques established. I've never worked with check boxes.
                      >
                      >When you say "Can't you just build up your query by checking each checkbox
                      >in turn?", I'm not sure what you mean. Can you give me an example?
                      >[/color]

                      Here is the sort if thing I imagine is going on in a
                      simplistic way

                      Dim s as string

                      If cbox1.checked = true then
                      s = "abc"
                      End

                      If cbox2.checked = true then
                      s = "123"
                      End

                      If cbox1.checked = true and cbox2.checked = true then
                      s = "abc123"
                      End

                      But a simpler way is

                      Dim s as string = ""

                      If cbox1.checked = true then
                      s &= "abc"
                      End

                      If cbox2.checked = true then
                      s &= "123"
                      End

                      If your building up an SQL the same reasoning applies.



                      Jan Hyde (VB MVP)

                      --
                      The tidal wave came as a big surf rise. (John Fenn)

                      [Abolish the TV Licence - http://www.tvlicensing.biz/]

                      Comment

                      Working...