473,387 Members | 3,787 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

Nov 21 '05 #1
10 15900
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" <ti*******@hotmail.com> wrote in message
news:OW**************@TK2MSFTNGP10.phx.gbl...
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

Nov 21 '05 #2
"Jim in Arizona" <ti*******@hotmail.com>'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)

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

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

Nov 21 '05 #3
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/]


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
Nov 21 '05 #4
"Terry Olsen" <to******@hotmail.com> wrote in message
news:eh**************@TK2MSFTNGP09.phx.gbl...
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
Nov 21 '05 #5
"Terry Olsen" <to******@hotmail.com> wrote in message
news:eh**************@TK2MSFTNGP09.phx.gbl...
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
Nov 21 '05 #6
"Jim in Arizona" <ti*******@hotmail.com> wrote in message news:On*************@TK2MSFTNGP09.phx.gbl...

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.

--
Al Reid
Nov 21 '05 #7
> 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.

--
Al Reid


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
Nov 21 '05 #8
>
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...
Nov 21 '05 #9
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.
Nov 21 '05 #10
"Jim in Arizona" <ti*******@hotmail.com>'s wild thoughts
were released on Wed, 20 Jul 2005 08:41:34 -0700 bearing the
following fruit:
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/]


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)

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

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

Nov 21 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Adrian Parker | last post by:
I have a PHP generated page which displays X many records. Each record has a checkbox preceding it. The user checks several checkboxes, and hits a delete button. All the corresponding records...
1
by: Harish Vaidya | last post by:
hi, i am trying to use multiple checkboxes. what is happening is once select check box its variable is not getting set. below is the code. what could be the problem? i am using python 2.0 thanks...
3
by: Tim | last post by:
Hello All! I'm having a difficult problem and I'm wondering if anyone can help. Here's the deal. I have a webpage with multiple forms on it. All of the forms have drop down boxes that when...
2
by: Craig M | last post by:
Hi, I have a form, frmInvoices and a subform, frmInvoicesSub. On the parent form, i have a "print report" button, that prints a report depending on an ID on the parent form. Each record in...
2
by: Cin | last post by:
I have a one-to-many relationship that I would like to convert to an unbound checkbox or radio buttons within a form. On form (pulls from tblEquipment) user can select one or all three choices...
1
by: Jim in Arizona | last post by:
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...
1
by: projectjakecs | last post by:
Hi, I am working on a ms access database and I am having trouble using a form to create new records in an associative table. Here is the breakdown of my database: Main Table - Computer...
3
by: LuCnt | last post by:
I am using Visual basic 6 and i want to be able to select more that one response in a option group with check boxes. As it is now i can only select one response at a time. the code looks like this....
3
by: Search & You Will Find | last post by:
I have a database in Access 2000 that I need some help on. I have three tables: PROJECTS, SYSTEMS, & SYSTEMSREF. They possess the following fields: -----------------------------------...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.