473,466 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How do I Iterate through checkboxes three at a time

2 New Member
I have a form that lists nine children as labels and has three checkboxes for each child denoting whether they ate breakfast, lunch and/or snack.

If they ate any meal, I am logging that to a CSV file. I need to know how to loop through each child and see if they ate any meals.

Basically, child1 gets logged if chkbox1 or chkbox2 or chkbox3 is checked. If no checkbox is checked, the child is not logged. Then on to the next child.

I don't need help with the logging, just the loop through the checkboxes.

Thanks.
Jun 2 '15 #1

✓ answered by Seth Schrock

If you name them to be
Expand|Select|Wrap|Line Numbers
  1. chkChild1Breakfast, chkChild1Lunch, chkChild1Snack
  2. chkChild2Breakfast, chkChild2Lunch, chkChild2Snack
  3. etc.
then you can do a For Next loop very easily.
Expand|Select|Wrap|Line Numbers
  1. Dim i As Integer
  2. Dim chkBreakfast As Checkbox
  3. Dim chkLunch As Checkbox
  4. Dim chkSnack As Checkbox
  5.  
  6. For i = 1 To 9
  7.     chkBreakfast = CType(Me.Controls("chkChild" & i.ToString() & "Breakfast", Checkbox)
  8.     chkLunch = CType(Me.Controls("chkChild" & i.ToString() & "Lunch", Checkbox)
  9.     chkSnack = CType(Me.Controls("chkChild" & i.ToString() & "Snack", Checkbox)
  10.  
  11.     If chkBreakfast.Checked Or chkLunch.Checked Or chkSnack.Checked Then
  12.         'At least one meal was eaten
  13.  
  14.     Else
  15.         'No meal was eaten
  16.  
  17.     End If
  18. Next

3 1571
Seth Schrock
2,965 Recognized Expert Specialist
If you name them to be
Expand|Select|Wrap|Line Numbers
  1. chkChild1Breakfast, chkChild1Lunch, chkChild1Snack
  2. chkChild2Breakfast, chkChild2Lunch, chkChild2Snack
  3. etc.
then you can do a For Next loop very easily.
Expand|Select|Wrap|Line Numbers
  1. Dim i As Integer
  2. Dim chkBreakfast As Checkbox
  3. Dim chkLunch As Checkbox
  4. Dim chkSnack As Checkbox
  5.  
  6. For i = 1 To 9
  7.     chkBreakfast = CType(Me.Controls("chkChild" & i.ToString() & "Breakfast", Checkbox)
  8.     chkLunch = CType(Me.Controls("chkChild" & i.ToString() & "Lunch", Checkbox)
  9.     chkSnack = CType(Me.Controls("chkChild" & i.ToString() & "Snack", Checkbox)
  10.  
  11.     If chkBreakfast.Checked Or chkLunch.Checked Or chkSnack.Checked Then
  12.         'At least one meal was eaten
  13.  
  14.     Else
  15.         'No meal was eaten
  16.  
  17.     End If
  18. Next
Jun 3 '15 #2
Squank
2 New Member
Other than adding a paren before the ", Checkbox", it worked as expected. Thank you very much!
Jun 8 '15 #3
Seth Schrock
2,965 Recognized Expert Specialist
Oops. My bad. For the sake of anyone else trying to do something similar, here is the corrected code:
Expand|Select|Wrap|Line Numbers
  1. Dim i As Integer
  2. Dim chkBreakfast As Checkbox
  3. Dim chkLunch As Checkbox
  4. Dim chkSnack As Checkbox
  5.  
  6. For i = 1 To 9
  7.     chkBreakfast = CType(Me.Controls("chkChild" & i.ToString() & "Breakfast"), Checkbox)
  8.     chkLunch = CType(Me.Controls("chkChild" & i.ToString() & "Lunch"), Checkbox)
  9.     chkSnack = CType(Me.Controls("chkChild" & i.ToString() & "Snack"), Checkbox)
  10.  
  11.     If chkBreakfast.Checked Or chkLunch.Checked Or chkSnack.Checked Then
  12.         'At least one meal was eaten
  13.  
  14.     Else
  15.         'No meal was eaten
  16.  
  17.     End If
  18. Next
Glad it helped.
Jun 8 '15 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: DSmith | last post by:
I have a quality control database containing data for samples pulled every four hours (several years worth of data). What I need to do is find the maximum value for a field for each eight hour...
3
by: Amal Ismail | last post by:
hello this is the third time i inquire aboutthe same thing and none answer me !! .. hope to find help here i'm using ASP.NET Mobile web application using c# And i'm facing a problem , i use...
7
by: Edward Mitchell | last post by:
I have a number of DateTimePicker controls, some set to dates, some set to a format of Time. The controls are all embedded in dialogs. I created the controls by dragging the DateTime picker from...
3
by: BrianDH | last post by:
Hi I have a form with multi-tabs, and on each tab I have a one combo that has the same values. I populate all 3 combo with the same one datacall/dataset My problem is: when I make a value...
1
by: ashok76 | last post by:
Hi I've a DataGridView and where has column 0 is a DataGridViewComboBoxColumn Whenever I selected that cell then the drop down list not appeared on single click event hence I need to click again...
5
by: ameshkin | last post by:
What I want to do is very simple, but I'm pretty new at PHP and its a little hard for me. I have one page, where there are a rows of checkboxes. A button selects all checkboxes, and then...
37
by: David T. Ashley | last post by:
I have Red Hat Enterprise Linux 4. I was just reading up about UTC and leap seconds. Is it true on my system that the Unix time may skip up or down by one second at midnight when there is a...
9
by: Sebarry | last post by:
Hi, I've put together a little test page to create checkboxes each time a button is clicked. The check button is created and should be appended to the form, but when the post is submitted the...
14
by: tdahsu | last post by:
I have twenty-five checkboxes I need to create (don't ask): self.checkbox1 = ... self.checkbox2 = ... .. .. .. self.checkbox25 = ... Right now, my code has 25 lines in it, one for each...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.