How to: Loop through all objects and count the checked check boxes 
Author Message
 How to: Loop through all objects and count the checked check boxes

Hi,

I'm having some trouble with the following:

Sub CountChecked()
 Dim i, y
 For i = 0 To document.all.length
  If document.all.item(, i).checked = True Then
   y = y + 1
  End If
 next
 MsgBox "Number of items checked" & y
End Sub

It doesn't work! All I want is to count all the checked
check boxes and see if the user checked the right
amount... How should I do this? Why isn't it working?
I tried the document.MyForm.elements collection also,
but with no luck...

Any Help will be very much appreciated!

Marc (Amsterdam, The Netherlands)



Fri, 23 Aug 2002 03:00:00 GMT  
 How to: Loop through all objects and count the checked check boxes
This might give you some ideas

http://www.aspfree.com/authors/adrian/MultipleCheckboxesClient.asp

--
"Don't try to be like Jackie.  There is only one Jackie...Study computers
instead."
     - Jackie Chan

http://www.aspfaq.com/
http://www.aspfree.com/authors/adrian/



Fri, 23 Aug 2002 03:00:00 GMT  
 How to: Loop through all objects and count the checked check boxes
Thanks Adrian!

I had to modify the code a bit and it's now like
this:

sub checkthenumber()
    for i = 1 to document.all.length
        set objChk = document.all("checkbox" & i)
        if objChk.Checked = True then
            buffer = buffer + 1
        end if
    next
    window.alert(buffer)
end sub

How come the alert method is never fired???
If I place the alert(buffer) in the If ... Then statement
it counts as I expected... Is this a bug??

Marc (Amsterdam, The Netherlands)

Quote:

>This might give you some ideas

>http://www.aspfree.com/authors/adrian/MultipleCheckboxesClient.asp

>--
>"Don't try to be like Jackie.  There is only one Jackie...Study computers
>instead."
>     - Jackie Chan

>http://www.aspfaq.com/
>http://www.aspfree.com/authors/adrian/



Fri, 23 Aug 2002 03:00:00 GMT  
 How to: Loop through all objects and count the checked check boxes
The VBScript version of Alert is MsgBox

MsgBox buffer



Fri, 23 Aug 2002 03:00:00 GMT  
 How to: Loop through all objects and count the checked check boxes
I know... :-)

sub CountChecked()
 for i = 1 to document.all.length
  set objChk = document.all("checkbox" & i)
  if objChk.Checked = True then
   buffer = buffer + 1
  end if
 next
MsgBox buffer
end sub

it still won't trigger the messagebox :-(

Marc (Amsterdam, The Netherlands)

Quote:

>The VBScript version of Alert is MsgBox

>MsgBox buffer



Fri, 23 Aug 2002 03:00:00 GMT  
 How to: Loop through all objects and count the checked check boxes
Oops! Though I was looking at JavaScript and missed the cue! But try the
test anyway.


Fri, 23 Aug 2002 03:00:00 GMT  
 How to: Loop through all objects and count the checked check boxes
Just as a test, try
MsgBox "ABC"  + buffer + "XYZ"
instead of
MsgBox buffer
to see what you get.

In the code shown, the variable buffer isn't initialized: try
initializing it. As I recall, a null variable will suppress MsgBox
output. The "+" operation in JavaScript does both
a) addition of numeric values and
b) concatenation of strings.
So if the code is treating buffer as a string, you will have problems.

Quote:

> I know... :-)

> sub CountChecked()
>  for i = 1 to document.all.length
>   set objChk = document.all("checkbox" & i)
>   if objChk.Checked = True then
>    buffer = buffer + 1
>   end if
>  next
> MsgBox buffer
> end sub

> it still won't trigger the messagebox :-(

> Marc (Amsterdam, The Netherlands)


> >The VBScript version of Alert is MsgBox

> >MsgBox buffer



Fri, 23 Aug 2002 03:00:00 GMT  
 How to: Loop through all objects and count the checked check boxes
Thanks Michael!

But still no luck :-(

Here's all the code:
'##############################################################
<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY>
<Form Name=Form>

<INPUT type="checkbox" id=checkbox1 name=checkbox1 checked><BR>
<INPUT type="checkbox" id=checkbox2 name=checkbox1><BR>
<INPUT type="checkbox" id=checkbox3 name=checkbox1><BR>
<INPUT type="checkbox" id=checkbox4 name=checkbox1><BR>
<INPUT type="checkbox" id=checkbox5 name=checkbox1><BR>
<INPUT type="checkbox" id=checkbox6 name=checkbox1><BR>
<INPUT type="checkbox" id=checkbox7 name=checkbox1><BR>
<INPUT type="checkbox" id=checkbox8 name=checkbox1><BR>
<INPUT type="checkbox" id=checkbox9 name=checkbox1><BR>
<INPUT type="checkbox" id=checkbox10 name=checkbox1><BR>
<INPUT type="button" value="Button" id=button1 name=button1
 Language=VbScript onclick="CountChecked()">

<Script Language=VbScript>
sub CountChecked()
 for i = 1 to document.all.length
  set objChk = document.all("checkbox" & i)
  if objChk.Checked = True then
   buffer = buffer + 1
  end if
 next
MsgBox "Whats up? " & buffer
end sub
</Script>
</Form>
</BODY>
</HTML>
'##############################################################

Figured maybe someone want's to try this
and see if he/she can get it to work...

btw I can get all this to work in javascript, no
problem! One of my students is just starting of
with asp and vbscript so I figured: let him start
with "simple" client side vbscript :-(

Marc (Amsterdam, The Netherlands)

Quote:

>Just as a test, try
>MsgBox "ABC"  + buffer + "XYZ"
>instead of
>MsgBox buffer
>to see what you get.

>In the code shown, the variable buffer isn't initialized: try
>initializing it. As I recall, a null variable will suppress MsgBox
>output. The "+" operation in JavaScript does both
>a) addition of numeric values and
>b) concatenation of strings.
>So if the code is treating buffer as a string, you will have problems.


>> I know... :-)

>> sub CountChecked()
>>  for i = 1 to document.all.length
>>   set objChk = document.all("checkbox" & i)
>>   if objChk.Checked = True then
>>    buffer = buffer + 1
>>   end if
>>  next
>> MsgBox buffer
>> end sub

>> it still won't trigger the messagebox :-(

>> Marc (Amsterdam, The Netherlands)


>> >The VBScript version of Alert is MsgBox

>> >MsgBox buffer



Sat, 24 Aug 2002 03:00:00 GMT  
 How to: Loop through all objects and count the checked check boxes
Oops that should have been:

'####################################################
<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY>
<Form Name=Form>

<INPUT type="checkbox" id=checkbox1 name=checkbox1 checked><BR>
<INPUT type="checkbox" id=checkbox2 name=checkbox2><BR>
<INPUT type="checkbox" id=checkbox3 name=checkbox3><BR>
<INPUT type="checkbox" id=checkbox4 name=checkbox4><BR>
<INPUT type="checkbox" id=checkbox5 name=checkbox5><BR>
<INPUT type="checkbox" id=checkbox6 name=checkbox6><BR>
<INPUT type="checkbox" id=checkbox7 name=checkbox7><BR>
<INPUT type="checkbox" id=checkbox8 name=checkbox8><BR>
<INPUT type="checkbox" id=checkbox9 name=checkbox9><BR>
<INPUT type="checkbox" id=checkbox10 name=checkbox10><BR>
<INPUT type="button" value="Button" id=button1 name=button1
 Language=VbScript onclick="CountChecked()">

<Script Language=VbScript>
sub CountChecked()
 for i = 1 to document.all.length
  set objChk = document.all("checkbox" & i)
  if objChk.Checked = True then
   buffer = buffer + 1
  end if
 next
MsgBox "Whats up? " & buffer
end sub
</Script>
</Form>
</BODY>
</HTML>
'####################################################



Sat, 24 Aug 2002 03:00:00 GMT  
 How to: Loop through all objects and count the checked check boxes
This worked fine for me

sub CountChecked()

 for i = 1 to 10
  set objChk = document.all("checkbox" & i)
  if objChk.Checked = True then
   buffer = buffer + 1
  end if
 next
MsgBox "Whats up? " & buffer
end sub

You were using
for i = 1 to all.length
which is everything (27 elements).  So it looked for checkbox1..checkbox27
but there are only 10 checkboxes.



Sat, 24 Aug 2002 03:00:00 GMT  
 How to: Loop through all objects and count the checked check boxes
THANKS ADRIAN!!!! WEHEEEE................
that was stupid of me .....

(Trying to do more things at a time,
multitasking... )

Marc (Amsterdam, The Netherlands)

(had to give an excuse.... sorry, I was just stupid)

Quote:

>This worked fine for me

>sub CountChecked()

> for i = 1 to 10
>  set objChk = document.all("checkbox" & i)
>  if objChk.Checked = True then
>   buffer = buffer + 1
>  end if
> next
>MsgBox "Whats up? " & buffer
>end sub

>You were using
>for i = 1 to all.length
>which is everything (27 elements).  So it looked for checkbox1..checkbox27
>but there are only 10 checkboxes.



Sat, 24 Aug 2002 03:00:00 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. Need help with script to show a hidden text box if a check box is checked

2. Counting check boxes on form

3. Check Box being checked if database value is equal to ON

4. check box form fields count

5. VBA Check and Option Check Boxes

6. Placing a check in check mark boxes and have it add prices

7. checked listview, background color, and the stupid check boxes

8. Check for checked values in List Box

9. Checking a check box in Word with VBS

10. Checking a check box in Word with VBS

11. Checking a check box in Word with VBS

12. Check boxes will not check! vb5

 

 
Powered by phpBB® Forum Software