Looping through Ctls on a Form, then referring to it by "name"

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I would like to be able to click a button on a form, and make all the
controls that are check boxes TRUE (without having to know their names before
the code runs: that is, I know how to do Me.Chk1.value = true)

There's a line in the code below (written as pseudo code so you can
hopefully understand what I'm asking) that obviously doesn't work:

If the Control is a check box Then
Me.ctl.Properties ("Name").value = True

Any help is appreciated.

Private Sub cmdListProperties_Click()
ListControlProps Me
End Sub

Public Sub ListControlProps(ByRef frm As Form)
Dim ctl As Control
Dim prp As Property

On Error GoTo props_err

For Each ctl In frm.Controls
For Each prp In ctl.Properties
If the Control is a check box Then
Me.ctl.Properties ("Name").value = True
End If
Next prp
Next ctl
 
This should do it:

For Each ctl In frm.Controls
If ctl.ControlType = acCheckBox Then
ctl = True
End If
Next ctl

If you have heaps of *bound* check boxes on a form, it may indicate the
table is not well designed. See:
Don't use Yes/No fields to store preferences
at:
http://allenbrowne.com/casu-23.html
 
Allen,

Very helpful all around - thanks for the Yes/No article too, I'll be passing
that on.

Thanks a lot
 
Back
Top