Solved

Modifying a loop

Posted on 2000-04-04
5
132 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now