Solved

Modifying a loop

Posted on 2000-04-04
5
137 Views
Last Modified: 2010-05-02
Hi,

I have an array of 32 checkboxes (chk1(0) To chk1(31)).  In the caption of each checkbox, there is a number from 1 To 32.  Thus, chk1(0) could have a caption from 1 To 32.  No two captions would be the same.

What I need is when I click on the Save button, it should tell me whether the checkbox with the .caption of 1 is checked, then it should tell me if the checkbox with the .caption of 2 is checked....all the way to 32.

Below is the code I have, it works but as you can see, is not very efficient.  Is there a better way to do this?


'-----------------begin of code---------
Private Sub btnSave_Click()

For i = 0 To 31
If Chk1(i).Caption = "1" And Chk1(i).Value = 1 Then
MsgBox "1 is checked"
End If
Next

For i = 0 To 31
If Chk1(i).Caption = "2" And Chk1(i).Value = 1 Then
MsgBox "2 is checked"
End If
Next

For i = 0 To 31
If Chk1(i).Caption = "3" And Chk1(i).Value = 1 Then
MsgBox "3 is checked"
End If
Next

.......For i = 0 To 31
If Chk1(i).Caption = "32" And Chk1(i).Value = 1 Then
MsgBox "32 is checked"
End If
Next

End Sub

'-----------End of Code------------

thanks much
0
Comment
Question by:sopheak
5 Comments
 
LVL 20

Expert Comment

by:hes
ID: 2684508
For i = 0 To 31
If Chk1(i).Caption =  i + 1 And Chk1(i).Value = 1 Then
MsgBox  i + 1 & " is checked"
End If
Next

0
 

Author Comment

by:sopheak
ID: 2684570
Hes, does not work.

i.e.

if chk1(0).caption =5 and is checked
a msgbox should come up, but it does not.
0
 
LVL 32

Accepted Solution

by:
bhess1 earned 100 total points
ID: 2684586
This should do it simply:

Dim Chkd(1 to 32) as Integer
Dim I As Integer

For i = 0 To 31
  Chkd(Val(Chk1(I).Caption))=Chk1(I).Value
Next I

For I = 1 to 32
  If Chkd(i) = 1 Then
    MsgBox I & " is checked"
  End If
Next
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2684591
or:

Private Sub Command1_Click()
    Dim i As Long
    For i = Chk1.LBound To Chk1.UBound
        If Chk1(i).Value = vbChecked Then
            MsgBox "Box " & Chk1(i).Caption & " is checked"
        End If
    Next
End Sub
0
 

Author Comment

by:sopheak
ID: 2684606
Thanks you!  bhess1!
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question