complex loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • totomalas
    New Member
    • Aug 2009
    • 31

    complex loop

    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

    Code:
    dim name as string
    dim iter as integer
    iter = 1
    
    
    
    Do
    
    name = "check" & iter
    
    if me.name.value = -1 then
    
     do something
    
    end if
    
    iter = iter + 1
    
    loop until iter = 10
    but i keep getting errors

    i also tried something like this

    Code:
    if check & iter.value = -1 then
    
    do something
    end if
    also error


    how can i do the above loop??
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    This code should work nicely, substitute your own Control Names within the Select Case construct:
    Code:
    Dim ctl As Control
    
    For Each ctl In Me.Controls         'Analyze each Control on the Form
      If ctl.ControlType = acCheckBox Then      'Is it a Check Box?
        If ctl.Value Then       'Is it Checked?
          Select Case ctl.Name      'Make a decision based on the Check Box selected
            Case "Check1"
              'process here for Check1
            Case "Check2"
              'process here for Check2
            Case "Check3"
              'process here for Check3
            Case "Check4"
              'process here for Check4
            Case "Check5"
              'process here for Check5
            Case "Check6"
              'process here for Check6
            Case "Check7"
              'process here for Check7
            Case "Check8"
              'process here for Check8
            Case "Check9"
              'process here for Check9
          End Select
        End If
      End If
    Next
    Last edited by NeoPa; Oct 27 '09, 02:34 PM. Reason: Removed Quote for Best Answer.

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32668

      #3
      Try the following :
      Code:
      Dim intX As Integer
      
      For intX = 1 To 9
          If Me.Controls("Check" & intX) Then
              'Do something
          End If
      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.

      Comment

      • totomalas
        New Member
        • Aug 2009
        • 31

        #4
        Thanks Guys...both ideas worked well form me...Thanks Again.. :D

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32668

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

          Comment

          Working...