Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

check all check box based on 0ne master

Status
Not open for further replies.

dixxy12

Technical User
Jun 18, 2007
45
CA
Hi,

I would like to know if there is a way to check a checkbox in every record base on 1 check box outside that form?

The main form has the checkbox 'chkReceivedAll' and the subform 'frmReceivedSud' has a checkbox on every record call 'chkReceived'.

So when the user check the 'main' checkbox i would like that all checkboxes in the subform to be'check'.

I'm playing with this code with not much luck:
Code:
Dim ctr As Control
Dim FirstNull As CheckBox
For Each ctr In Me.Controls
If ctr.ControlType = acCheckBox Then
  If IsNull(ctr) Then
    If FirstNull = "" Then
    ctr.SetFocus
    ctr.Checked = True
    End If
  FirstNull.Value = True
  End If
End If
Next
Can this be done?

Thanks
 
I would say the esiest thing would be an update query.

Here is a very rough example with imaginary names.
[tt]strSQL="UPDATE tblTable SET ChkField=True WHERE LinkField=" & Me.LinkField
CurrentDB.Execute strSQL, dbFailOnError[/tt]
 
yeah that could work...could you just elaborate a little more....in terms of what i have to dim....and the other details of the procedure...
I know i need:

dim str as string

replace chkFeild, by the field name?

what else?
 
strSQL is indeed a string and ChkField is the name of the subform field that you need to update. The important bit is the link field, that is, the field that identifies the subset of records in the subform. This will generally be the field that you used as the link master and link child field.

The alternative to the above is to use the recordsetclone of the subform, but this can be a little tedious.
 
ok...starting to get this....

this is what i have

Code:
strSQL = "UPDATE tblPODoorHistory SET forms!frmReceiced!frmRecivedDoorSub.form!chkAllRec=True WHERE chkReceived=" & Me.chkAllRec
CurrentDb.Execute strSQL, dbFailOnError

main form name : frmReceived Data : Unbound Feild Name : chkReceivedAll(Data unbound)
SubFormName: frmRecivedDoorSub Feild Name : chkReceived Data: SQL recordSource
 
How are ya dixxy12 . . .

Using an [blue]Update SQL[/blue] I get:
Code:
[blue]   Dim db As DAO.Database, frm As Form, SQL As String, flg As Boolean
   
   Set db = CurrentDb
   Set frm = [frmReceivedSub].Form
   If Me!chkReceivedAll Then flg = True
   
   SQL = "UPDATE tblPODoorHistory " & _
         "SET [chkReceived] = " & flg & _
         "WHERE [[purple][b][i]PrimarykeyName[/i][/b][/purple]] = " & Me![purple][b][i]PrimarykeyName[/i][/b][/purple] & ";"
   db.Execute SQL, dbFailOnError
   
   frm.Requery
   
   Set frm = Nothing
   Set db = notning[/blue]
Using [blue]Recordset[/blue]:
Code:
[blue]   Dim frm As Form, rst As DAO.Recordset, ctl As Control, flg As Boolean
   
   If Me!chkReceivedAll Then flg = True
   Set frm = [frmReceivedSub].Form
   Set rst = frm.RecordsetClone
   
   If Not rst.BOF Then
      Do
         rst.Edit
         rst!chkReceived = flg
         rst.Update
         rst.MoveNext
      Loop Until rst.EOF
   End If
   
   frm.Requery
   
   Set rst = Nothing[/blue]
[blue]Your Thoughts? . . .[/blue]

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
Also faq181-2886
 
Thanks AceMan,

The main form is Unbound Data....does this matter in either case

I see that your first statment seems to be looking for the link between the forms....what about the second one will it matter?
 
dixxy12 said:
[blue]I see that your first statment seems to be looking for the link between the forms....what about the second one will it matter?[/blue]
Since [blue]chkReceivedAll[/blue] is unbound and only has operational control then remove the where clause:
Code:
[blue]   Dim db As DAO.Database, frm As Form, SQL As String, flg As Boolean
   
   Set db = CurrentDb
   Set frm = [frmReceivedSub].Form
   If Me!chkReceivedAll Then flg = True
   
   SQL = "UPDATE tblPODoorHistory " & _
         "SET [chkReceived] = " & flg & ";"
   db.Execute SQL, dbFailOnError
   
   frm.Requery
   
   Set frm = Nothing
   Set db = notning[/blue]
However, the code will turn all [blue]chkReceived[/blue] checkboxes on or off. I'm just not sure if you're looking to change a subset, instead of all records in the table.
[blue]Your Thoughts? . . .[/blue]

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
Also faq181-2886
 
I realy appriciate your help but something is not working out...

Code:
Dim db As DAO.Database, frm As Form, SQL As String, flg As Boolean
   
   Set db = CurrentDb
   Set frm = Forms![frmReceived]![frmRecivedDoorSub].Form
   If Me!chkAllRec Then flg = True
   
   SQL = "UPDATE tblPODoorHistory " & _
         "SET [chkReceived] = " & flg & ";"
   db.Execute SQL, dbFailOnError
   
   frm.Requery
   
   Set frm = Nothing
   Set db = notning

i have even changed the RecordSource of the subform because before it was a SQL statment, and still nothing happens. -:(

what i'm i not doing irhgt here?
 
ok...got this working would like to add one small thing...

right now it will update every checkbox in the table....so i would like to filter it.

this is working:
Code:
Dim db As DAO.Database, frm As Form, SQL As String, flg As Boolean

   Set db = CurrentDb
   Set frm = Forms![frmReceived]![frmRecivedDoorSub].Form
   If Me.chkAllRec Then flg = True

   SQL = "UPDATE tblPODoorHistory " & _
         "SET [Received] = " & flg & ";"

   db.Execute SQL, dbFailOnError

   Forms!frmReceived!frmRecivedDoorSub.Form!chkReceived.Requery

'   frm.Requery

   Set frm = Nothing
   Set db = notning
but it check every box in the table

trying to filter it with this:
Code:
   Dim db As DAO.Database, frm As Form, SQL As String, flg As Boolean

   Set db = CurrentDb
   Set frm = Forms![frmReceived]![frmRecivedDoorSub].Form
   If Me.chkAllRec Then flg = True

   SQL = "UPDATE tblPODoorHistory " & _
         "SET [Received] = " & flg & _
         "WHERE [tblPODoorHistory]![PONumber] = " & Me.cboDoorPO & ";"

   db.Execute SQL, dbFailOnError

   Forms!frmReceived!frmRecivedDoorSub.Form!chkReceived.Requery

'   frm.Requery

   Set frm = Nothing
   Set db = notning
but this those not want to work, it filters the recorset perfectly but does not check any boxes

what i'm i doing wrong?

 
You seem to be missing a space.

Code:
SQL = "UPDATE tblPODoorHistory " & _
         "SET [Received] = " & flg & _
         "[COLOR=red yellow] [/color]WHERE [tblPODoorHistory]![PONumber] = " & Me.cboDoorPO

& ";" - This is completely unnecessary in Jet SQL.
 
Remou,

smae thing...does not check the boxes at all
 
In that case, you do not have a match.

Try:

Code:
strSQL = "UPDATE tblPODoorHistory " & _
         "SET [Received] = " & flg & _
         " WHERE [tblPODoorHistory]![PONumber] = " & Me.cboDoorPO
[b]Debug.print strSQL[/b]

This will print the SQL to the immediate window, so you can see exactly what is happening.

By the way, SQL is a reserved word ( and therefore should be avoided - I generally use strSQL.
 
i must be doing something wrong, cause it does not show anything in the immidiate window..
 
Code:
Private Sub chkAllRec_AfterUpdate()
   Dim db As DAO.Database, frm As Form, SQL As String, flg As Boolean

   Set db = CurrentDb
   Set frm = Forms![frmReceived]![frmRecivedDoorSub].Form
   If Me.chkAllRec Then flg = True

   SQL = "UPDATE tblPODoorHistory " & _
         "SET [Received] = " & flg & _
         " WHERE [tblPODoorHistory]![PONumber] = " & Me!cboDoorPO & ";"
         Debug.Print strSQL

   db.Execute SQL, dbFailOnError

   Forms!frmReceived!frmRecivedDoorSub.Form!chkReceived.Requery

'   frm.Requery

   Set frm = Nothing
   Set db = notning

End Sub
nothing shows up in the immidiate window....i'm i suppose to do somthing special to see that?

sorry i am not familiar at all with the immidiate window aspect of VBA...
 
Try:

Code:
Private Sub chkAllRec_AfterUpdate()
   Dim db As DAO.Database, frm As Form, strSQL As String, flg As Boolean

   Set db = CurrentDb
   Set frm = Forms![frmReceived]![frmRecivedDoorSub].Form

   'What is flg to equal when Me.ChkAllRec is False?
   'Would it not be easier to simply refer to the control
   'in the SQL, especially when you refer to the combo?
   If Me.chkAllRec Then flg = True

   strSQL = "UPDATE tblPODoorHistory " & _
         "SET [Received] = " & Me.chkAllRec & _
         " WHERE [tblPODoorHistory]![PONumber] = " & Me!cboDoorPO & ";"
         Debug.Print strSQL

   db.Execute strSQL, dbFailOnError

   'You can use records affected here:
   msgbox db.RecordsAffected & " records were updated."
   
   'This should read something like:
   frm.[name of subformcontrol].Form.Requery

'   frm.Requery

   Set frm = Nothing
   Set db = notning

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top