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
19 2792
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 - 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
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 - 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
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
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
- 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
-
Hi missinglinq
I was hoping that questionit might try having a go, its the only way to learn !?
MTB
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
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
-
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
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)>
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 :)
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)>
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 !
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 :)
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)>
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
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 ... ?
-
-
-
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)>
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)>
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
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)>
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
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)> Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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
|
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)...
|
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...
|
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 ***...
|
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...
|
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,...
|
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...
|
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.
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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...
| |