check all checkboxes

Posted on 2006-04-11
Medium Priority
Last Modified: 2010-04-23
This doesn't seem to be working for me...what am I missing?

        Dim ctl As CheckBox
        For Each ctl In Controls
           ctl.Checked = True
Question by:sirbounty
  • 3
  • 2
LVL 86

Accepted Solution

Mike Tomlinson earned 500 total points
ID: 16428411
Dim cb As CheckBox
Dim ctl As Control
For Each ctl In Me.Controls
    If TypeOf ctl Is CheckBox Then
        cb = CType(ctl, CheckBox)
        cb.Checked = True
    End If

*** BUT *** this will only hit checkboxes directly contained by the FORM itself.  If the checkboxes are contained in another control (panel, groupboxes, etc) then you would need a recursive algorithm to find all of them.
LVL 67

Author Comment

ID: 16428593
ksir - can ya explain what we're doing here?  I'm not very familiar with ctype...does that statement convert the control to a checkbox before proceeding?  Why doesn't the fact that I've established ctl as a checkbox work?  Shouldn't the loop go through all checkboxes, since ctl isn't declared as a generic control? : \
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16428650
When you use Me.Controls, you iterate ALL controls directly contained by the form.  What if you have controls on your form that are not CheckBoxes?  This would cause an error since your code would attempt to convert say a TextBox to a CheckBox in your For...Next loop!

You are correct about the CType() function.  It is converting the "ctl" variable to a CheckBox if it passed the "TypeOf ctl Is CheckBox" test (which returns True if "ctl" is a CheckBox).
LVL 67

Author Comment

ID: 16428736
The way I read it was
Declare a variable as a checkbox control.  Now loop through all of those 'type' controls, not 'all' controls.
I guess it sort of makes sense, I just presumed that by declaring the ctl as a specific checkbox that that would/should limit the iteration to those types of controls only...
Thanx for the follow.
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16428765
Don't feel bad, your assumption is a pretty common one and the type of code you posted comes up on EE quite a bit.  =)

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question