Solved

Modifying a loop

Posted on 2000-04-04
5
140 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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:
Brendt Hess 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…

717 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