By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,652 Members | 1,911 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,652 IT Pros & Developers. It's quick & easy.

Checkbox selects other Checkboxes

beacon
100+
P: 579
Hi everybody,

[Access 2003]

I have a quality assurance database that has a form with a bunch of checkboxes on it that serve as reasons for why a product didn't pass inspection.

It's a given that the users will be able to select individual checkboxes to indicate the reasons. What I would like to find out how to do is to put a checkbox or radio button on the form that will select all the checkboxes if it's selected.

When I added a checkbox to my form and tried to write code for it, the control didn't have a 'Value' or 'Checked' property for me to choose. Is it not possible to do this? If I use a radio button, I'd like to have one to 'Select All' and one to 'Deselect' if at all possible.

I tried the following to no avail based on something I found online:
Expand|Select|Wrap|Line Numbers
  1. Private Sub chkSelectAll_AfterUpdate()
  2. me.chkDefect = me.chkSelectAll
  3. me.chkPowerSupply = me.chkSelectAll
  4. end sub
  5.  
Can anyone share a little help with me?

Thanks in advance,
beacon
Jan 20 '09 #1
Share this Question
Share on Google+
13 Replies


100+
P: 675
Expand|Select|Wrap|Line Numbers
  1. me!chkDefect = me!chkSelectAll
  2. me!chkPowerSupply = me!chkSelectAll
  3.  
You do not need a 'Deselect' as all will be deselected if/when chkSelectAll is clicked again, and is then false.
It makes no difference whether you use checkboxes or radiobuttons. However, radiobuttons imply that only one can be true at any given time. Radio button refers to a radio presets, and selecting a station deselects another. I assume that a product can fail for multiple reasons simultaneously, and therefore checkboxes would be my preference.

OldBirdman
Jan 21 '09 #2

beacon
100+
P: 579
Hi OldBirdMan,

Here's the code I entered for my 'Select All' checkbox. I don't think it matters, but the checkboxes are all on a tab control.

Expand|Select|Wrap|Line Numbers
  1. Private Sub chkSelectAll_AfterUpdate()
  2. me.chkDefect = me.chkSelectAll
  3. End Sub
  4.  
When I enter this and go back to use the 'Select All' checkbox, I get an error message that says, "Run-time error 438: Object doesn't support this method or property."

This is the error message I was getting initially that prompted me to post to the forum. I don't use checkboxes that often and this is the first time I've tried to control other checkboxes with a checkbox.
Jan 21 '09 #3

NeoPa
Expert Mod 15k+
P: 31,263
Hi OB. Good to see you around again :)

Assuming the clever naming structure that OB displays in his example (All CheckBox controls, and only CheckBox controls, start chk) and also that chkSelectAll & chkDeselectAll are the only ones ending in the last three characters "All", then the following code should help somewhat :
Expand|Select|Wrap|Line Numbers
  1. Private Sub chkSelectAll_AfterUpdate()
  2.   Call SetAll(blnValue:=True
  3. End Sub
  4.  
  5. Private Sub chkDeselectAll_AfterUpdate()
  6.   Call SetAll(blnValue:=False
  7. End Sub
  8.  
  9. Private Sub SetAll(blnValue As Boolean)
  10.   Dim ctlThis As Form.Control
  11.  
  12.   For Each ctlThis In Me.Controls
  13.     If Left(ctlThis.Name, 3) = "chk" _
  14.     And Right(ctlThis.Name, 3) <> "All" Then _
  15.       ctlThis = blnValue
  16.   Next ctlThis
  17. End Sub
Jan 21 '09 #4

NeoPa
Expert Mod 15k+
P: 31,263
@beacon
This is because the items you're dealing with are objects whose default property is .Value.

When mixed with other items (EG. Me.chkDefect = blnValue) it determines the default property to be required. When used object to object (as here) it thinks you want to assign one object to the other (EG. Set objVar = Me.chkSelectAll).

Does that clarify your understanding?
Jan 21 '09 #5

FishVal
Expert 2.5K+
P: 2,653
@beacon
That only means Me object doesn't support either chkDefect or chkSelectAll property, most probably chkDefect.
Jan 21 '09 #6

beacon
100+
P: 579
@NeoPa
I understand what you're saying, but I'm not sure how to rectify the problem. I saw your response that showed how I could loop through each of the controls based on the 'chk' prefix, but it brings up another potential issue I'll undoubtedly run into.

The potential issue is how can I handle this problem if I'm asked to put in another checkbox that allows the user to select default reasons. For instance, we have 'Select All' that chooses all of the checkboxes and we have a 'Select Default' that only chooses a certain selection based on the product type...how can I indicate just a select few to be selected when that option is chosen?

Keep in mind that there are multiple different products so the options will be different for 'Select Default' based on the product.
Jan 21 '09 #7

100+
P: 675
No, it doesn't clear up anything. It seems like I assign .value to .value all the time. txtA = txtB, chkC = chkD, or txtE = cboF, etc. Never had any issues here with Access making the assignments. Because this code is in the form's module, can't the "me" be dropped completely?

Clever naming structure belongs to beacon's original post, I kept answers in his framework, but it is similar to my naming conventions.

As to having a "DeSelectAll" checkbox, the code shown does not clear it when "SelectAll" is checked. Reflecting on this, I don't think there should be either a Select or DeSelect checkbox. They should be command buttons, "Select All" and "Clear All".

If a product is defective in all categories except 1, user might want to select all, and then deselect it (i.e. uncheck chkPowerSupply) to show that it passed the one test. The chkSelectAll is now incorrect if it means that all checkboxes are checked. It gets more complicated because, after each individual checkbox is changed, all must be checked to see if either chkSelectAll or chkDeSelectAll should be changed. chkSelectAll = (chkPowerSupply And chkDefect . . .) and DeSelectAll = Not (chkPowerSupply Or chkDefect Or . . .). This could be done in a loop similar to that shown in Post #4 by NeoPa.

This could be expanded to include a "Default" command button. (This discussion is now about sets of checkboxes, not the actual problem presented, as something is very wrong if there is a "Default" set of flaws in a product. Improve your quality control, or get a new supplier). Each item would have a Default set of flaws.

Checkboxes would have to be TripleState, and locked if grayed, or Checkboxes would vary from item to item. You might consider a subform with each record showing an inspection item and a checkbox. This is a database design issue.

OldBirdman
Jan 21 '09 #8

beacon
100+
P: 579
I think I found my answer on this website: ** Edit ** link removed.

In case you don't want to go to the website, the gist of the solution is to enter a tag for each of the checkboxes I want selected (All or default) and then create an AfterUpdate() event for the checkbox that will trigger the event.

Here's the code assuming that I put a tag of 'SelectAll' or 'Default' in for the checkboxes that I want controlled by the chkSelectAll or chkDefault:
Expand|Select|Wrap|Line Numbers
  1. Private Sub chkSelectAll_AfterUpdate()
  2.     Dim ctl as Control
  3.  
  4.     For Each ctl In Me.Controls
  5.         If Me.chkSelectAll = True Then
  6.             If ctl.Tag = "SelectAll" Then
  7.                 ctl.Value = True
  8.             End If
  9.         Else
  10.             If ctl.Tag = "SelectAll" Then
  11.                 ctl.Value = False
  12.             End If
  13.         End If
  14.     Next ctl
  15. End Sub
  16.  
This will check only the boxes that you've entered a tag for. I took the code I found and took it one step further by adding the conditional "If Me.chkSelectAll = True" around the other conditional "If ctl.Tag = 'SelectAll'..." which allows you to deselect your selection.

I don't really understand how I was able to give a ctl a .Value when it wasn't available from the properties when I typed in the code, but it works. I know that you can't just type the following:
Expand|Select|Wrap|Line Numbers
  1. If ctl.Tag = "SelectAll" Then
  2.      ctl.Value = True
  3. Else
  4.      ctl.Value = False
  5. End If
  6.  
...because it will give you an error message similar to the runtime error 438 I was receiving when I initially began this process.

You have to put in some work to add the tags to each of the checkboxes, but once you get that entered it works pretty well.

Thanks for everybody's input...
Jan 21 '09 #9

NeoPa
Expert Mod 15k+
P: 31,263
@beacon
As it seems you have already discovered, a good way to track this when the conditions are not as simple as those stipulated, is to use the Tag property of a control. Any value could be used for the tag simply to identify which are grouped together :
Expand|Select|Wrap|Line Numbers
  1. Private Sub SetAll(blnValue As Boolean) 
  2.   Dim ctl As Form.Control 
  3.  
  4.   For Each ctl In Me.Controls 
  5.     If ctl.Tag = "Group1" Then ctl = blnValue 
  6.   Next ctl 
  7. End Sub
Jan 21 '09 #10

NeoPa
Expert Mod 15k+
P: 31,263
@OldBirdman
Well, it seems Fish thinks I may be mistaken too. Possibly true. I'm a bit rushed at the moment to set up a test, but it's not too hard. Check it out.
@OldBirdman
Yes. It can. That doesn't mean it's a good idea though. Me. is more explicit, thus making the code clearer to a reader. Certainly not necessary though.
@OldBirdman
Good point, and good solution.
Jan 21 '09 #11

NeoPa
Expert Mod 15k+
P: 31,263
@beacon
Sorry about that. Our rules forbid linking to competing forums. We suffer enough from spam, and that would just make it too easy. I suspect your usage was innocently enough intended, but it had to be removed nevertheless.
@beacon
If ctl is Dimmed as a simple Form.Control, then this makes sense. Intellisense.

Intellisense (prompting you with a list of available properties) can only prompt with what it knows about. If the variable had been designed as a Form.CheckBox, then it would have known it had a .Value property. As it is defined as a generic control then it doesn't know that.

Your code wouldn't work as written if your form had any controls without the .Value property (EG. Label).

PS. Please check out OB's post where he suggests using command buttons instead of checkboxes for this. He makes a good point.
Jan 21 '09 #12

beacon
100+
P: 579
Hi NeoPa,

Sorry about the link...I questioned whether or not I should include. I'm really in the habit of including my sources for school and thought it best to give credit where it's due. Thanks for clearing that up...I won't include links to other forums in the future.

I read what OB said about the command buttons and I tried that, but I didn't have any luck initially...I still got the 438 error message.

Now that I've gotten the solution I listed...I guess it really doesn't matter if I use a checkbox or a command button. The checkbox will add or remove checks from the other checkboxes depending on whether or not it's value is true or not, so it basically serves the same purpose as a select/clear command button. Heck, a toggle button that changed it's caption from 'Select' to 'Clear' might even be neat/useful.

My task, now that the checkboxes work for default and select all conditions, is to find a way to link the default reason to the product that's chosen. I don't think I'll have any problems...just a lot of repetitive code to address all the possible conditions.

Thanks again, as always, for all your help...
Jan 21 '09 #13

NeoPa
Expert Mod 15k+
P: 31,263
@beacon
Really not a problem. I guessed as much. Incidentally, links to sites hosted by the provider will always be acceptable (MSDN for instance, or an MS web page for Access).

Oh, and have fun with your tagging ;)
Jan 22 '09 #14

Post your reply

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