Solved

Modifying a loop

Posted on 2000-04-04
5
135 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 Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Use Multiple Forms 4 49
Paint/Redraw window while dragging 16 68
Using "ScreenUpdating" 6 55
Added a column screws up code 5 50
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…

920 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

16 Experts available now in Live!

Get 1:1 Help Now