Link to home
Start Free TrialLog in
Avatar of sirbounty
sirbountyFlag for United States of America

asked on

check all checkboxes

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
        Next
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sirbounty

ASKER

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? : \
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).
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.
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.  =)