473,320 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

check-boxes

553 512MB
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
Jul 24 '07 #1
19 2792
MikeTheBike
639 Expert 512MB
Hi
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
Jul 24 '07 #2
questionit
553 512MB
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?




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
Jul 24 '07 #3
MikeTheBike
639 Expert 512MB
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
Jul 24 '07 #4
questionit
553 512MB
MTB

Can you help me in starting setting up the counter?

Thanks

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
Jul 24 '07 #5
missinglinq
3,532 Expert 2GB
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.  
Jul 24 '07 #6
MikeTheBike
639 Expert 512MB
Hi missinglinq

I was hoping that questionit might try having a go, its the only way to learn !?

MTB
Jul 24 '07 #7
questionit
553 512MB
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?




Hi missinglinq

I was hoping that questionit might try having a go, its the only way to learn !?

MTB
Jul 24 '07 #8
missinglinq
3,532 Expert 2GB
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.  
Jul 24 '07 #9
MikeTheBike
639 Expert 512MB
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
Jul 24 '07 #10
missinglinq
3,532 Expert 2GB
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)>
Jul 24 '07 #11
questionit
553 512MB
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 :)

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)>
Jul 24 '07 #12
questionit
553 512MB
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 !

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 :)
Jul 24 '07 #13
missinglinq
3,532 Expert 2GB
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)>
Jul 24 '07 #14
MikeTheBike
639 Expert 512MB
Hi
For Ech Ctrl in Me.Controls
Good spot missinglinq!

As for
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
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
Jul 25 '07 #15
questionit
553 512MB
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.  
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)>
Jul 25 '07 #16
missinglinq
3,532 Expert 2GB
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)>
Jul 25 '07 #17
questionit
553 512MB
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


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)>
Jul 25 '07 #18
MikeTheBike
639 Expert 512MB
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
Jul 25 '07 #19
missinglinq
3,532 Expert 2GB
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)>
Jul 25 '07 #20

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: Craig Bailey | last post by:
Someone please explain what alternate universe I fell into this afternoon when PHP started telling me that 2 doesn't equal 2. Not sure about you, but when I run this, it tells me 59001.31 doesn't...
2
by: Askari | last post by:
Hi, How do for do a "select()" on a CheckButton in a menu (make with add_checkbutton(....) )? I can modify title, state, etc but not the "check state". :-( Askari
5
by: Mr. B | last post by:
The following code is how I check for duplicates in a List box. This is simple enough as there is only one column of stuff to check. ' Check for Duplicates ' Search listbox (from last to first)...
5
by: Steve Wylie | last post by:
I am constructing an HTML questionnaire and one of the questions requires people to rate some choices from 1 to 5, where 1 is their favourite and 5 is their least favourite: Car Bus Taxi cab...
7
by: Tony Johnson | last post by:
Can you make a check box very big? It seems like when you drag it bigger the little check is still the same size. Thank you, *** Sent via Developersdex http://www.developersdex.com ***...
1
by: scprosportsman | last post by:
Please help guys, i am trying to set up a database here at work and im fairly new to access in terms of writing functions and queries and stuff. I have 2 different places on my design that will...
3
by: ferg | last post by:
I have a Customer table. The table has two different CHECK constraints. Then there is the Customer details dialog, which provides the user with an UI for changing users. I have some UPDATE sql,...
2
by: Chris Davoli | last post by:
How do you enable a check box in the GridView. I selected Checkbox Field in the Columns of the GridView, and the check box shows up in the Grid view, but it is disabled. How do I enable it so I can...
16
by: Brian Tkatch | last post by:
Is there a way to check the order in which SET INTEGRITY needs to be applied? This would be for a script with a dynamic list of TABLEs. B.
5
by: starke1120 | last post by:
Im creating a check in – check out database for RF guns. I have a table that contains models. ID (primary key) Model A table that contains Gun Details ID (primary key) Model_id...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.