hi all, i use a command for accept change in records of subform, but i want to give a msgbox when click on "ok" button and unchecked any checkbox in records :
msgbox "There Is no checked checkbox in subform, Please check at least one of the checkboxes"
you can use a domain function to count the checked records.
Code:
If DCount("*", "yourqueryORtable", "yourcheckfield =True") =0 Then
Msgbox "Please check at least one item", vbOKOnly
Exit Sub
End If
.... do whatever you want to do with the checked items
thanks, but that's not table or querry, it's just Filtered table records in subform on unchecked checkbox and i change the code to (but it's not working) :
DCount("*", Me.child11.Form, "Chk =True")
i use other code to count checked item but it's not working too :
count = 0
For Each ctl In Me.tbl_Comp_Sub.Form
If ctl.ControlType = acCheckBox Then
If ctl.Value = True Then
count = count + 1
End If
End If
Next
MsgBox "Total Boxes Checked =" & count
You cannot use DCount that way. The 2nd argument of ANY of the domain aggregate functions MUST be a named query or table. A form is neither of those.
To make this work, the question is how you do the filtering that you imply in the phrase "it's just Filtered table records in subform" - because if you can filter the subform, there might be a way to filter the subform's source. If you can do that, it would work. Otherwise, you would have to make a more complex criteria clause in the domain aggregate. As a simple example not necessarily related to your exact problem:
Code:
X = DCount( "*", "query-name", "([Chk]=True) AND ([Project]='Apollo'")
Of course, your filter string might have to be more complex than that, but that is the general idea.
When you say your code is not working, are you sure it is executing?
Code:
count = 0
For Each ctl In Me.tbl_Comp_Sub.Form
If ctl.ControlType = acCheckBox Then
If ctl.Value = True Then
count = count + 1
End If
End If
Next
MsgBox "Total Boxes Checked =" & count
When you say your code is not working, are you sure it is executing
Of course it is not working. It may return 1 since there is one checkbox control. It is not going to loop your records. In a continuous form no matter how many records you have only one set of controls. The rest are "paint"
I you wanted to do this in code looping the records, instead of the dlookup solution it would be something like this (untested). Called from the main form
Code:
Private Function CountChecks() as Integer
dim rs as dao.recordset
set rs = me.subformcontrolName.form.recordset
do while not rs.eof
if rs!booleanFieldName = true then
countChecks = CountChecks + 1
end if
rs.movenext
loop
end Function