check-boxes | Site Addict | | Join Date: Feb 2007
Posts: 553
| | |
If you have so many check-boxes on a form .
If you want to see if atleast 2 of them have been ticked. Do you have to loop through each of it - it would be difficult since each check-box has a different name so an array of string would have to be created - and that it will be a long procedure.
Is there anything simpler method ?
Thanks
| | Expert | | Join Date: Jun 2007 Location: Derbyshire, UK
Posts: 369
| | | re: check-boxes
Hi Quote:
Originally Posted by questionit If you have so many check-boxes on a form .
If you want to see if atleast 2 of them have been ticked. Do you have to loop through each of it - it would be difficult since each check-box has a different name so an array of string would have to be created - and that it will be a long procedure.
Is there anything simpler method ?
Thanks In short Yes
Try looking at - dim Ctrl as Control
-
For Ech Ctrl in Me.Controls
-
if ctrl.ControlType=acCheckBox and ctrl=True then
-
' Your code logic here
-
end if
-
next ctrl
??
MTB
| | Site Addict | | Join Date: Feb 2007
Posts: 553
| | | re: check-boxes
MTB
I havn't understood the login of your code.
If i have to loop through all the Controls manually anyway , whats the need of the code you have just given?
your code is checking if all the controls on Me. are true (means ticked?) then do something - is that right?
can i not just get the count of how many are ticked by doing a simple For loop? Quote:
Originally Posted by MikeTheBike Hi
In short Yes
Try looking at - dim Ctrl as Control
-
For Ech Ctrl in Me.Controls
-
if ctrl.ControlType=acCheckBox and ctrl=True then
-
' Your code logic here
-
end if
-
next ctrl
??
MTB | | Expert | | Join Date: Jun 2007 Location: Derbyshire, UK
Posts: 369
| | | re: check-boxes Quote:
Originally Posted by questionit MTB
I havn't understood the login of your code.
If i have to loop through all the Controls manually anyway , whats the need of the code you have just given?
your code is checking if all the controls on Me. are true (means ticked?) then do something - is that right?
can i not just get the count of how many are ticked by doing a simple For loop? Yes, and anything else you like !
ie. you need to set up a counter and (perhaps) exit the for loop ('Exit For' will do that) if it becomes 2 or whatever ??
MTB
| | Site Addict | | Join Date: Feb 2007
Posts: 553
| | | re: check-boxes
MTB
Can you help me in starting setting up the counter?
Thanks Quote:
Originally Posted by MikeTheBike Yes, and anything else you like !
ie. you need to set up a counter and (perhaps) exit the for loop ('Exit For' will do that) if it becomes 2 or whatever ??
MTB |  | Moderator | | Join Date: Nov 2006 Location: Richmond, Virginia USA
Posts: 3,103
| | | re: check-boxes - Dim Ctrl as Control
-
Dim CBCounter as Integer
-
CBCounter = 0
-
-
For Each Ctrl in Me.Controls
-
If ctrl.ControlType=acCheckBox and ctrl=True then
-
CBCounter = CBCounter + 1
-
End If
-
Next ctrl
-
-
If CBCounter > 1 Then
-
'Do whatever you need to do if condition is met
-
End If
-
| | Expert | | Join Date: Jun 2007 Location: Derbyshire, UK
Posts: 369
| | | re: check-boxes
Hi missinglinq
I was hoping that questionit might try having a go, its the only way to learn !?
MTB
| | Site Addict | | Join Date: Feb 2007
Posts: 553
| | | re: check-boxes
HI MTB /Missinglinq
I get error on this:
If Ctrl.ControlType = acCheckBox And Ctrl = True Then
"Object doesn't support this property or method. "
I am using VBA, which doesn't have .ControlType property i think -- any alternatives? Quote:
Originally Posted by MikeTheBike Hi missinglinq
I was hoping that questionit might try having a go, its the only way to learn !?
MTB |  | Moderator | | Join Date: Nov 2006 Location: Richmond, Virginia USA
Posts: 3,103
| | | re: check-boxes
Try this: - Dim Ctrl as Control
-
Dim CBCounter as Integer
-
CBCounter = 0
-
For Each Ctrl in Me.Controls
-
If TypeOf ctrl Is CheckBox and ctrl = True Then
-
CBCounter = CBCounter + 1
-
End If
-
Next ctrl
-
If CBCounter > 1 Then
-
'Do whatever you need to do if condition is met
-
End If
-
| | Expert | | Join Date: Jun 2007 Location: Derbyshire, UK
Posts: 369
| | | re: check-boxes Quote:
Originally Posted by missinglinq Try this: - Dim Ctrl as Control
-
Dim CBCounter as Integer
-
CBCounter = 0
-
For Each Ctrl in Me.Controls
-
If TypeOf ctrl Is CheckBox and ctrl = True Then
-
CBCounter = CBCounter + 1
-
End If
-
Next ctrl
-
If CBCounter > 1 Then
-
'Do whatever you need to do if condition is met
-
End If
-
Or pehaps this - Dim Ctrl as Control
-
Dim CBCounter as Integer
-
CBCounter = 0
-
For Each Ctrl in Me.Controls
-
If Ctrl.ControlType = acCheckBox Then
-
If Ctrl = True Then CBCounter = CBCounter + 1
-
End If
-
Next ctrl
-
If CBCounter > 1 Then
-
'Do whatever you need to do if condition is met
-
End If
-
??
MTB
|  | Moderator | | Join Date: Nov 2006 Location: Richmond, Virginia USA
Posts: 3,103
| | | re: check-boxes
It's the little things that trip you up! I copied Mike's original code and modified it, and he did the same thing. The problem was, he had a typo in it!
dim Ctrl as Control
For Ech Ctrl in Me.Controls
if ctrl.ControlType=acCheckBox and ctrl=True then
' Your code logic here
end if
next ctrl Ech should have been Each! I've corrected this in all the code listed here except the original posting by Mike, and I expect both versions will now work! I haven't tried the ControlType but I expect that works just fine. There's about three or four ways of referencing the control types, all of which are fine!
The hilited line on the error If Ctrl.ControlType = acCheckBox And Ctrl = True Then points out a common flaw in the VB Editor; it tends to hilite the next line after the line that causes the error. In this case that was the line:
For Ech Ctrl in Me.Controls
Linq ;0)> | | Site Addict | | Join Date: Feb 2007
Posts: 553
| | | re: check-boxes
Thanks missinglinq
I've got the things working fine now with the line from the previous code posting: -
If Ctrl.ControlType = acCheckBox Then
-
But I get error if i do it this way :
If TypeOf Ctrl Is CheckBox And Ctrl = True Then
Error: "Object doesn't support the property or method"
- probably this one is not supported by VBA :) Quote:
Originally Posted by missinglinq It's the little things that trip you up! I copied Mike's original code and modified it, and he did the same thing. The problem was, he had a typo in it!
dim Ctrl as Control
For Ech Ctrl in Me.Controls
if ctrl.ControlType=acCheckBox and ctrl=True then
' Your code logic here
end if
next ctrl Ech should have been Each! I've corrected this in all the code listed here except the original posting by Mike, and I expect both versions will now work! I haven't tried the ControlType but I expect that works just fine. There's about three or four ways of referencing the control types, all of which are fine!
The hilited line on the error If Ctrl.ControlType = acCheckBox And Ctrl = True Then points out a common flaw in the VB Editor; it tends to hilite the next line after the line that causes the error. In this case that was the line:
For Ech Ctrl in Me.Controls
Linq ;0)> | | Site Addict | | Join Date: Feb 2007
Posts: 553
| | | re: check-boxes
Hey Experts
By the way
By your method i.e -
For Each Ctrl In Me.Controls
-
.......
-
We check every check-box on the form. But what if we dont want to check all of them. For example i have a few sets of check-box on a form and i want to look for those only which i am interested in ? Sounds impossible task to me ! Quote:
Originally Posted by questionit Thanks missinglinq
I've got the things working fine now with the line from the previous code posting: -
If Ctrl.ControlType = acCheckBox Then
-
But I get error if i do it this way :
If TypeOf Ctrl Is CheckBox And Ctrl = True Then
Error: "Object doesn't support the property or method"
- probably this one is not supported by VBA :) |  | Moderator | | Join Date: Nov 2006 Location: Richmond, Virginia USA
Posts: 3,103
| | | re: check-boxes
No, it's not impossible at all! I'll be away from my PC for a while, but if you'll post the actual code you're now using, when I get back I'll show you how to only check certain controls! It's really an easy technique!
Linq ;0)> | | Expert | | Join Date: Jun 2007 Location: Derbyshire, UK
Posts: 369
| | | re: check-boxes
Hi Quote:
For Ech Ctrl in Me.Controls
Good spot missinglinq!
As for Quote:
I've got the things working fine now with the line from the previous code posting:
Code: ( text )
1. If Ctrl.ControlType = acCheckBox Then
But I get error if i do it this way :
If TypeOf Ctrl Is CheckBox And Ctrl = True Then
Error: "Object doesn't support the property or method"
- probably this one is not supported by VBA :)
I believe that this error/solution Quote:
I've got the things working fine now with the line from the previous code posting:
Code: ( text )
1. If Ctrl.ControlType = acCheckBox Then
But I get error if i do it this way :
If TypeOf Ctrl Is CheckBox And Ctrl = True Then
Error: "Object doesn't support the property or method"
- probably this one is not supported by VBA :)
is due to some of the controls not liking ctrl=True (a lable for instance)
which is why I changed it to be used only if the control is a checkbox !
I cannot think of away (off hand) to check specific checkboxes, other than the obviouse of using the checkbox names.
This is possible with a little mod to the code, if you want to go this way, but it will mean hard coding the checkbox names into the code, but this is not particularly desirable for design maintenance.
MTB
| | Site Addict | | Join Date: Feb 2007
Posts: 553
| | | re: check-boxes
HI Missinglinq -
Can you demonstrate how to implement this. I have no other code. I use your example. I just added 'Page1'.Controls - I have a Tab-Control
-
-
For Each Ctrl In [Page1].Controls
-
If Ctrl.ControlType = acCheckBox Then
-
If Ctrl = True Then CBCounter = CBCounter + 1
-
End If
-
Next Ctrl
-
-
If CBCounter =0 Then
-
MsgBox "No Options selected"
-
End If
-
-
Basically, i have 12 check-boxes - 6 at left and 6 at right of the page form. How would i check only the 6 right ones or left ones - You are saying this thing is possible ... ?
-
-
-
Quote:
Originally Posted by missinglinq No, it's not impossible at all! I'll be away from my PC for a while, but if you'll post the actual code you're now using, when I get back I'll show you how to only check certain controls! It's really an easy technique!
Linq ;0)> |  | Moderator | | Join Date: Nov 2006 Location: Richmond, Virginia USA
Posts: 3,103
| | | re: check-boxes
Doing this will involve the use of a control property known as Tag.
In Design View, for each checkbox that you want included in the procedure:
1) Right Click on the checkbox (make sure it's the checkbox, not its label!)
2) Click on Properties
3) Click on Other
4) In the box for the Tag Property, type in CheckThis (one word, no quotes)
Now, replace this part of your code - If Ctrl.ControlType = acCheckBox Then
-
If Ctrl = True Then CBCounter = CBCounter + 1
-
End If
-
With this - If Ctrl.ControlType = acCheckBox Then
-
If Ctrl.Tag = "CheckThis" Then
-
If Ctrl = True Then CBCounter = CBCounter + 1
-
End If
-
End If
Line @ 2 of the new code will check each checkbox to see whether its Tag property reads CheckThis, and if it does will increment the counter, otherwise it will skip the particular control.
Linq ;0)> | | Site Addict | | Join Date: Feb 2007
Posts: 553
| | | re: check-boxes
Great Linq,
I wonder why the other experts dont know this thing....
It is really helpful
Now i was thinking if instead of doing this: -
If Ctrl.Tag = "CheckThis" Then
-
we do the same thing in the for loop (on the top line) . maybe like this: -
For Each Ctrl.Tag = "CheckThis"
-
so we done have to write an extra line to do this. ... I will experiment this.
Thanks Quote:
Originally Posted by missinglinq Doing this will involve the use of a control property known as Tag.
In Design View, for each checkbox that you want included in the procedure:
1) Right Click on the checkbox (make sure it's the checkbox, not its label!)
2) Click on Properties
3) Click on Other
4) In the box for the Tag Property, type in CheckThis (one word, no quotes)
Now, replace this part of your code - If Ctrl.ControlType = acCheckBox Then
-
If Ctrl = True Then CBCounter = CBCounter + 1
-
End If
-
With this - If Ctrl.ControlType = acCheckBox Then
-
If Ctrl.Tag = "CheckThis" Then
-
If Ctrl = True Then CBCounter = CBCounter + 1
-
End If
-
End If
Line @ 2 of the new code will check each checkbox to see whether its Tag property reads CheckThis, and if it does will increment the counter, otherwise it will skip the particular control.
Linq ;0)> | | Expert | | Join Date: Jun 2007 Location: Derbyshire, UK
Posts: 369
| | | re: check-boxes
Hi missinglinq
I thought at the back of my mind there was something else, never used tag (yet) but must bear it in mind, excelent solution.
MTB
|  | Moderator | | Join Date: Nov 2006 Location: Richmond, Virginia USA
Posts: 3,103
| | | re: check-boxes
It is a little known property but really great for doing all kind of odd jobs where you need to, sorry, can't help it, Tag certain controls! ;0)> |  | Similar Microsoft Access / VBA bytes | | | Forums
Visit our community forums for general discussions and latest on Bytes
/bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 229,155 network members.
|