Re: Checkbox Looping Help
VB Code:
If Check(a).Value = vbChecked Then
:)
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.
Re: Checkbox Looping Help
Quote:
Originally Posted by Merri
VB Code:
If Check(a).Value = vbChecked Then
:)
Checked actually works.
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" :cry:
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.
Re: Checkbox Looping Help
[QUOTE=rovcruiser]
Quote:
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" :cry:
????
- Do you have a control array?
- Is the name of that control array Check?
Re: Checkbox Looping Help
Quote:
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
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
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
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?
Re: Checkbox Looping Help
Quote:
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 ... :p :p :p
Re: Checkbox Looping Help
Quote:
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:
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.
Re: Checkbox Looping Help
Quote:
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 :wave:
Re: Checkbox Looping Help
Quote:
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 :eek:
Re: Checkbox Looping Help
You don't need a control array. Try post #10 above.
Re: Checkbox Looping Help
Quote:
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...
:sick:
Re: Checkbox Looping Help
Quote:
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...
:sick:
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.
Re: Checkbox Looping Help
cruiser,
you may want to zip your project and upload it so we can take a look at it.
Re: Checkbox Looping Help
That is an Excel project ??? You should've open this thread in VBA Forum .
Re: Checkbox Looping Help
Quote:
Originally Posted by RhinoBull
That is an Excel project ??? You should've open this thread in
VBA Forum .
OOoooooopsss :ehh: ...sorry im new
Re: Checkbox Looping Help