Connecting Tech Pros Worldwide Forums | Help | Site Map

complex loop

Newbie
 
Join Date: Aug 2009
Posts: 30
#1: Oct 22 '09
I have nine checkboxes in my form

named like this
check1
check2
check3
check4
...
check9

i want to create a loop that loops them and if they are checked then do something



i wrote something like this

Expand|Select|Wrap|Line Numbers
  1.  
  2. dim name as string
  3. dim iter as integer
  4. iter = 1
  5.  
  6.  
  7.  
  8. Do
  9.  
  10. name = "check" & iter
  11.  
  12. if me.name.value = -1 then
  13.  
  14.  do something
  15.  
  16. end if
  17.  
  18. iter = iter + 1
  19.  
  20. loop until iter = 10
  21.  
but i keep getting errors

i also tried something like this

Expand|Select|Wrap|Line Numbers
  1.  
  2. if check & iter.value = -1 then
  3.  
  4. do something
  5. end if
  6.  
  7.  
also error


how can i do the above loop??
best answer - posted by ADezii
This code should work nicely, substitute your own Control Names within the Select Case construct:
Expand|Select|Wrap|Line Numbers
  1. Dim ctl As Control
  2.  
  3. For Each ctl In Me.Controls         'Analyze each Control on the Form
  4.   If ctl.ControlType = acCheckBox Then      'Is it a Check Box?
  5.     If ctl.Value Then       'Is it Checked?
  6.       Select Case ctl.Name      'Make a decision based on the Check Box selected
  7.         Case "Check1"
  8.           'process here for Check1
  9.         Case "Check2"
  10.           'process here for Check2
  11.         Case "Check3"
  12.           'process here for Check3
  13.         Case "Check4"
  14.           'process here for Check4
  15.         Case "Check5"
  16.           'process here for Check5
  17.         Case "Check6"
  18.           'process here for Check6
  19.         Case "Check7"
  20.           'process here for Check7
  21.         Case "Check8"
  22.           'process here for Check8
  23.         Case "Check9"
  24.           'process here for Check9
  25.       End Select
  26.     End If
  27.   End If
  28. Next

ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,226
#2: Oct 22 '09

re: complex loop


This code should work nicely, substitute your own Control Names within the Select Case construct:
Expand|Select|Wrap|Line Numbers
  1. Dim ctl As Control
  2.  
  3. For Each ctl In Me.Controls         'Analyze each Control on the Form
  4.   If ctl.ControlType = acCheckBox Then      'Is it a Check Box?
  5.     If ctl.Value Then       'Is it Checked?
  6.       Select Case ctl.Name      'Make a decision based on the Check Box selected
  7.         Case "Check1"
  8.           'process here for Check1
  9.         Case "Check2"
  10.           'process here for Check2
  11.         Case "Check3"
  12.           'process here for Check3
  13.         Case "Check4"
  14.           'process here for Check4
  15.         Case "Check5"
  16.           'process here for Check5
  17.         Case "Check6"
  18.           'process here for Check6
  19.         Case "Check7"
  20.           'process here for Check7
  21.         Case "Check8"
  22.           'process here for Check8
  23.         Case "Check9"
  24.           'process here for Check9
  25.       End Select
  26.     End If
  27.   End If
  28. Next
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,765
#3: Oct 23 '09

re: complex loop


Try the following :
Expand|Select|Wrap|Line Numbers
  1. Dim intX As Integer
  2.  
  3. For intX = 1 To 9
  4.     If Me.Controls("Check" & intX) Then
  5.         'Do something
  6.     End If
  7. Next intX
Unfortunately, as your question makes little sense in itself, I suspect this will only be useful to you to help you understand why your question was so little use.
Newbie
 
Join Date: Aug 2009
Posts: 30
#4: 4 Weeks Ago

re: complex loop


Thanks Guys...both ideas worked well form me...Thanks Again.. :D
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,765
#5: 4 Weeks Ago

re: complex loop


Thanks for letting us know. That helps for people searching through threads for answers :)
Reply