Connecting Tech Pros Worldwide Forums | Help | Site Map

check-boxes

Site Addict
 
Join Date: Feb 2007
Posts: 553
#1: Jul 24 '07
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
#2: Jul 24 '07

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
Expand|Select|Wrap|Line Numbers
  1. dim Ctrl as Control
  2. For Ech Ctrl in Me.Controls
  3.      if ctrl.ControlType=acCheckBox and ctrl=True then
  4.         ' Your code logic here
  5.     end if
  6. next ctrl
??

MTB
Site Addict
 
Join Date: Feb 2007
Posts: 553
#3: Jul 24 '07

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

Expand|Select|Wrap|Line Numbers
  1. dim Ctrl as Control
  2. For Ech Ctrl in Me.Controls
  3.      if ctrl.ControlType=acCheckBox and ctrl=True then
  4.         ' Your code logic here
  5.     end if
  6. next ctrl
??

MTB

Expert
 
Join Date: Jun 2007
Location: Derbyshire, UK
Posts: 369
#4: Jul 24 '07

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
#5: Jul 24 '07

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

missinglinq's Avatar
Moderator
 
Join Date: Nov 2006
Location: Richmond, Virginia USA
Posts: 3,103
#6: Jul 24 '07

re: check-boxes


Expand|Select|Wrap|Line Numbers
  1. Dim Ctrl as Control
  2. Dim CBCounter as Integer
  3. CBCounter = 0
  4.  
  5. For Each Ctrl in Me.Controls
  6.  If ctrl.ControlType=acCheckBox and ctrl=True then
  7.   CBCounter = CBCounter + 1
  8.  End If
  9. Next ctrl
  10.  
  11. If CBCounter > 1 Then
  12.  'Do whatever you need to do if condition is met
  13. End If
  14.  
Expert
 
Join Date: Jun 2007
Location: Derbyshire, UK
Posts: 369
#7: Jul 24 '07

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
#8: Jul 24 '07

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

missinglinq's Avatar
Moderator
 
Join Date: Nov 2006
Location: Richmond, Virginia USA
Posts: 3,103
#9: Jul 24 '07

re: check-boxes


Try this:
Expand|Select|Wrap|Line Numbers
  1. Dim Ctrl as Control
  2. Dim CBCounter as Integer
  3. CBCounter = 0
  4. For Each Ctrl in Me.Controls
  5.  If TypeOf ctrl Is CheckBox and ctrl = True Then
  6.   CBCounter = CBCounter + 1
  7.  End If
  8. Next ctrl
  9. If CBCounter > 1 Then
  10.  'Do whatever you need to do if condition is met
  11. End If
  12.  
Expert
 
Join Date: Jun 2007
Location: Derbyshire, UK
Posts: 369
#10: Jul 24 '07

re: check-boxes


Quote:

Originally Posted by missinglinq

Try this:

Expand|Select|Wrap|Line Numbers
  1. Dim Ctrl as Control
  2. Dim CBCounter as Integer
  3. CBCounter = 0
  4. For Each Ctrl in Me.Controls
  5.  If TypeOf ctrl Is CheckBox and ctrl = True Then
  6.   CBCounter = CBCounter + 1
  7.  End If
  8. Next ctrl
  9. If CBCounter > 1 Then
  10.  'Do whatever you need to do if condition is met
  11. End If
  12.  

Or pehaps this
Expand|Select|Wrap|Line Numbers
  1. Dim Ctrl as Control
  2. Dim CBCounter as Integer
  3. CBCounter = 0
  4. For Each Ctrl in Me.Controls
  5.  If Ctrl.ControlType = acCheckBox Then
  6.     If Ctrl = True Then CBCounter = CBCounter + 1
  7.  End If
  8. Next ctrl
  9. If CBCounter > 1 Then
  10.  'Do whatever you need to do if condition is met
  11. End If
  12.  
??

MTB
missinglinq's Avatar
Moderator
 
Join Date: Nov 2006
Location: Richmond, Virginia USA
Posts: 3,103
#11: Jul 24 '07

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
#12: Jul 24 '07

re: check-boxes


Thanks missinglinq

I've got the things working fine now with the line from the previous code posting:
Expand|Select|Wrap|Line Numbers
  1.  If Ctrl.ControlType = acCheckBox Then
  2.  
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
#13: Jul 24 '07

re: check-boxes


Hey Experts

By the way

By your method i.e
Expand|Select|Wrap|Line Numbers
  1. For Each Ctrl In Me.Controls 
  2. .......
  3.  
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:

Expand|Select|Wrap|Line Numbers
  1.  If Ctrl.ControlType = acCheckBox Then
  2.  
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 :)

missinglinq's Avatar
Moderator
 
Join Date: Nov 2006
Location: Richmond, Virginia USA
Posts: 3,103
#14: Jul 24 '07

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
#15: Jul 25 '07

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
#16: Jul 25 '07

re: check-boxes


HI Missinglinq

Expand|Select|Wrap|Line Numbers
  1. 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
  2.  
  3.   For Each Ctrl In [Page1].Controls
  4.                       If Ctrl.ControlType = acCheckBox Then
  5.                          If Ctrl = True Then CBCounter = CBCounter + 1
  6.                       End If
  7.                   Next Ctrl
  8.  
  9.                   If CBCounter =0 Then
  10.                   MsgBox "No Options selected"
  11.                   End If
  12.  
  13. 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 ... ?
  14.  
  15.  
  16.  
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)>

missinglinq's Avatar
Moderator
 
Join Date: Nov 2006
Location: Richmond, Virginia USA
Posts: 3,103
#17: Jul 25 '07

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
Expand|Select|Wrap|Line Numbers
  1. If Ctrl.ControlType = acCheckBox Then
  2.  If Ctrl = True Then CBCounter = CBCounter + 1
  3. End If
  4.  

With this

Expand|Select|Wrap|Line Numbers
  1. If Ctrl.ControlType = acCheckBox Then
  2.   If Ctrl.Tag = "CheckThis" Then  
  3.    If Ctrl = True Then CBCounter = CBCounter + 1
  4.   End If
  5. 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
#18: Jul 25 '07

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:
Expand|Select|Wrap|Line Numbers
  1. If Ctrl.Tag = "CheckThis" Then
  2.  
we do the same thing in the for loop (on the top line) . maybe like this:
Expand|Select|Wrap|Line Numbers
  1. For Each Ctrl.Tag = "CheckThis"
  2.  
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

Expand|Select|Wrap|Line Numbers
  1. If Ctrl.ControlType = acCheckBox Then
  2.  If Ctrl = True Then CBCounter = CBCounter + 1
  3. End If
  4.  

With this

Expand|Select|Wrap|Line Numbers
  1. If Ctrl.ControlType = acCheckBox Then
  2.   If Ctrl.Tag = "CheckThis" Then  
  3.    If Ctrl = True Then CBCounter = CBCounter + 1
  4.   End If
  5. 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
#19: Jul 25 '07

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
missinglinq's Avatar
Moderator
 
Join Date: Nov 2006
Location: Richmond, Virginia USA
Posts: 3,103
#20: Jul 25 '07

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)>
Reply