By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,962 Members | 1,791 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,962 IT Pros & Developers. It's quick & easy.

How do I Iterate through checkboxes three at a time

P: 2
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

Share this Question
Share on Google+
3 Replies


Seth Schrock
Expert 2.5K+
P: 2,932
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

P: 2
Other than adding a paren before the ", Checkbox", it worked as expected. Thank you very much!
Jun 8 '15 #3

Seth Schrock
Expert 2.5K+
P: 2,932
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

Post your reply

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