|
-
Jan 5th, 2005, 06:18 PM
#1
Thread Starter
New Member
Checkbox Looping Help
Please help with looping
--------------------------------------------------------------------------
I don't understand why the following code isn't working
--------------------------------------------
For a = 1 To 3
If Check(a).Value = Checked Then Label1.Caption = "a check box is checked"
Next a
End If
--------------------------------------------
Any help is appreciated
Last edited by rovcruiser; Jan 5th, 2005 at 10:20 PM.
-
Jan 5th, 2005, 06:22 PM
#2
Re: Checkbox Looping Help
VB Code:
If Check(a).Value = vbChecked Then
-
Jan 5th, 2005, 06:23 PM
#3
Re: Checkbox Looping Help
Your code assumes that you have a control array that starts at an index of 1. The normal starting point is 0. Also it's probably better (more usual anyhow) to use vbChecked rather than Checked.
-
Jan 5th, 2005, 06:24 PM
#4
Re: Checkbox Looping Help
 Originally Posted by Merri
VB Code:
If Check(a).Value = vbChecked Then

Checked actually works.
-
Jan 5th, 2005, 06:45 PM
#5
Thread Starter
New Member
Re: Checkbox Looping Help
[QUOTE=Merri]
VB Code:
If Check(a).Value = vbChecked Then
I just tried that and an error comes up saying "Sub or Function not defined" and then it highlights the word "Check"
-
Jan 5th, 2005, 06:49 PM
#6
Re: Checkbox Looping Help
Then you don't have a control array named Check. Is it Check1 or have you renamed the controls to something else? Also note what MartinLiss said.
-
Jan 5th, 2005, 06:50 PM
#7
Re: Checkbox Looping Help
[QUOTE=rovcruiser]
 Originally Posted by Merri
VB Code:
If Check(a).Value = vbChecked Then
I just tried that and an error comes up saying "Sub or Function not defined" and then it highlights the word "Check" 
????
- Do you have a control array?
- Is the name of that control array Check?
-
Jan 5th, 2005, 07:32 PM
#8
Thread Starter
New Member
Re: Checkbox Looping Help
 Originally Posted by MartinLiss
????
- Do you have a control array?
- Is the name of that control array Check?
I have 3 checkboxes with the following names:
check1
check2
check3
one label (label1) and one command button
-
Jan 5th, 2005, 07:37 PM
#9
Re: Checkbox Looping Help
You don't have a control array (group of controls of the same type sharing one name but having a different indexes) so you'll need to loop through controls collection:
VB Code:
Private Sub Command1_Click()
Dim ctl As Control
For Each ctl In Controls
If TypeOf ctl Is CheckBox Then
Debug.Print ctl.Name & " : " & ctl.Value
End If
Next ctl
End Sub
-
Jan 5th, 2005, 08:48 PM
#10
Re: Checkbox Looping Help
VB Code:
Private Sub Command1_Click()
Dim ctl As Control
For Each ctl In Controls
If TypeOf ctl Is CheckBox Then
If ctl.Value = Checked Then Label1.Caption = "a check box is checked"
End If
Next ctl
End Sub
-
Jan 5th, 2005, 09:18 PM
#11
Re: Checkbox Looping Help
- make a new checkbox
- rename it chkTest
- see property Index, make it be 0
- Edit > Copy
- Edit > Paste
- make so that you have three checkboxes
- the indexes of the checkboxes vary from 0 to 2, so change your code to point from 0 To 2 instead of 1 To 3
- also make sure you have chkTest(a) instead of Check(a)

Edit what the... I just posted this, it is the newest post and I'm BEFORE the post that was posted before I posted?
-
Jan 5th, 2005, 09:26 PM
#12
Re: Checkbox Looping Help
 Originally Posted by Merri
... Edit what the... I just posted this, it is the newest post and I'm BEFORE the post that was posted before I posted?
VBF had a problems from (according to my timing) about 7:45 Pm till 8:40+ PM ... I even open a new thread in feedback forum.
EDIT: the same thing happened with this reply ...
-
Jan 5th, 2005, 09:37 PM
#13
Thread Starter
New Member
Re: Checkbox Looping Help
 Originally Posted by RhinoBull
You don't have a control array (group of controls of the same type sharing one name but having a different indexes) so you'll need to loop through controls collection:
VB Code:
Private Sub Command1_Click()
Dim ctl As Control
For Each ctl In Controls
If TypeOf ctl Is CheckBox Then
Debug.Print ctl.Name & " : " & ctl.Value
End If
Next ctl
End Sub
Hey thanx alot for your help, unfortunately I don't seem to know much about arrays so is there any posibility that you could put the coding together with my above code, sorry but Im still a learner :mike:
-
Jan 5th, 2005, 10:04 PM
#14
Re: Checkbox Looping Help
That example is not for an array. It will list the name of your checkboxes.
To create an array, delete all 3 of them. Create a new one, and name if CHECK. Then copy it, and click PASTE. It will ask you if you want to create a control array. Select yet. This will give you 0 and 1. To get the third one, just paste another onto the form. It will be #2.
Then, you can check if Check(0).value as you wanted orginally.
-
Jan 5th, 2005, 10:05 PM
#15
Thread Starter
New Member
Re: Checkbox Looping Help
 Originally Posted by Merri
- make a new checkbox
- rename it chkTest
- see property Index, make it be 0
- Edit > Copy
- Edit > Paste
- make so that you have three checkboxes
- the indexes of the checkboxes vary from 0 to 2, so change your code to point from 0 To 2 instead of 1 To 3
- also make sure you have chkTest(a) instead of Check(a)
Edit what the... I just posted this, it is the newest post and I'm BEFORE the post that was posted before I posted?
YOU SAVED MY DAY, THANX ALOT MERRI
Thanx to everyone else that helped me out... This Forum Rocks, by the way im new
-
Jan 5th, 2005, 10:22 PM
#16
Thread Starter
New Member
Re: Checkbox Looping Help
 Originally Posted by Merri
- make a new checkbox
- rename it chkTest
- see property Index, make it be 0
- Edit > Copy
- Edit > Paste
- make so that you have three checkboxes
- the indexes of the checkboxes vary from 0 to 2, so change your code to point from 0 To 2 instead of 1 To 3
- also make sure you have chkTest(a) instead of Check(a)
Edit what the... I just posted this, it is the newest post and I'm BEFORE the post that was posted before I posted?
Hi again,
Does anyone know how this can be done in VBE (vb excel) as I thought it would be exactly the same as vb6. It just won't let me have 2 check boxes with the same name
-
Jan 6th, 2005, 01:08 AM
#17
Re: Checkbox Looping Help
You don't need a control array. Try post #10 above.
-
Jan 6th, 2005, 06:08 AM
#18
Thread Starter
New Member
Re: Checkbox Looping Help
 Originally Posted by MartinLiss
You don't need a control array. Try post #10 above.
VB Code:
Private Sub Command1_Click()
Dim ctl As Control
For Each ctl In Controls
If TypeOf ctl Is CheckBox Then
Debug.Print ctl.Name & " : " & ctl.Value
End If
Next ctl
End Sub
I don't quite understand where to put the code or if I have to modify it and where my if statement goes that if one of the check boxes is checked...
-
Jan 6th, 2005, 11:34 AM
#19
Re: Checkbox Looping Help
 Originally Posted by rovcruiser
VB Code:
Private Sub Command1_Click()
Dim ctl As Control
For Each ctl In Controls
If TypeOf ctl Is CheckBox Then
Debug.Print ctl.Name & " : " & ctl.Value
End If
Next ctl
End Sub
I don't quite understand where to put the code or if I have to modify it and where my if statement goes that if one of the check boxes is checked...

First of all, that's not the code from #10, this is
VB Code:
Private Sub Command1_Click()
Dim ctl As Control
For Each ctl In Controls
If TypeOf ctl Is CheckBox Then
If ctl.Value = Checked Then Label1.Caption = "a check box is checked"
End If
Next ctl
End Sub
I can't tell you where to put the code since I don't know your application.
-
Jan 6th, 2005, 01:29 PM
#20
Re: Checkbox Looping Help
cruiser,
you may want to zip your project and upload it so we can take a look at it.
-
Jan 6th, 2005, 09:10 PM
#21
Re: Checkbox Looping Help
That is an Excel project ??? You should've open this thread in VBA Forum .
-
Jan 7th, 2005, 09:24 AM
#22
Thread Starter
New Member
Re: Checkbox Looping Help
 Originally Posted by RhinoBull
That is an Excel project ??? You should've open this thread in VBA Forum .
OOoooooopsss ...sorry im new
-
Jan 7th, 2005, 12:04 PM
#23
Re: Checkbox Looping Help
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|