Processing Multiple Check Boxes

  • Thread starter Thread starter Jim in Arizona
  • Start date Start date
J

Jim in Arizona

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
 
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" <[email protected]>'s wild thoughts
were released on Tue, 19 Jul 2005 16:21:09 -0700 bearing the
following fruit:
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.

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)
 
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)

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
 
Terry Olsen said:
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 :)

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
 
Terry Olsen said:
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

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
 
Jim in Arizona said:
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

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.Controls
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.
 
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.Controls
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.

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
 
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?

Well, you could use a Panel or Placeholder I believe...
 
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.

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.
 
"Jim in Arizona" <[email protected]>'s wild thoughts
were released on Wed, 20 Jul 2005 08:41:34 -0700 bearing the
following fruit:
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?

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top