Solved

Modifying a loop

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

791 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