Checking all boxes on a form.

G

Guest

Hello all,

I have a form that connect to a query, so the data on the from changes
depending on what is selected in the query. Each record has a check box. I
have a select all button on my form and when somone clicks it I want all the
boxes checked. This is the crrent code Im using.

Private Sub Command75_Click()
Dim ctl As Control
On Error Resume Next
For Each ctl In Me.Controls
If ctl.ControlType = acCheckBox Then ctl.Value = True
Next ctl
End Sub

The problem is that it is only checking the first box and then stopping.
Thanks for any advice you can offer!

James O
 
M

Marshall Barton

James said:
I have a form that connect to a query, so the data on the from changes
depending on what is selected in the query. Each record has a check box. I
have a select all button on my form and when somone clicks it I want all the
boxes checked. This is the crrent code Im using.

Private Sub Command75_Click()
Dim ctl As Control
On Error Resume Next
For Each ctl In Me.Controls
If ctl.ControlType = acCheckBox Then ctl.Value = True
Next ctl
End Sub

The problem is that it is only checking the first box and then stopping.


There should be no reason to loop through all the controls
just to locate a single check box. Just use the name of the
check box:
Me.thecheckbox = True

If you can get hold of the criteria used to select the
form's records, you should construct an Update query to
modify all the records in one operation:
strSQL = "UPDATE thetable SET theyesnofield = True " _
& "WHERE " & somethingorother
CurrentDb.Execute strSQL

If that's out of the question, then you can loop through the
form's records and set the field's (not the control's)
value:

With Me.RecordsetClone
If .RecordCount > 0 Then
.MoveFirst
Do Until .EOF
.Edit
!theyesnofield = True
.Update
.MoveNext
Loop
End If
End With
 
G

Guest

Thanks for the reply BruceM

Ok I changed to:

Dim ctl As Control
On Error Resume Next
For Each ctl In Me.Controls
If ctl.ControlType = acCheckBox Then
ctl.Value = True
End If
Next ctl

I unfortunately still get the same problem... it checks the first box and
then stops.
 
G

Guest

Thanks Marshall!

I went with your 3rd option which worked like a charm. Perfect I appreciate
the help very much.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top