Option Explicit
Option Compare Text
[color=green]' This code should be placed on the main form with a sub-form control called
' frmCustomersSubForm, change as required. It sums a Boolean field called
' TestCheck in the sub-forms recordset, again change as required.
'
' It should work OK if the Record Source of the sub-form is a Table or Query but will
' not work if the Record Source of the sub-form is a SQL statement.
'
' Here is how it works: -
'
' A Boolean field is stored as a 16 bit number but should only ever have a value of
' False(0), XOR True (Not False(-1)) in VBA.
'
' Therefore we can sum the values of Boolean variables. If the sum of all the fields
' is zero then no individual variable is True, else the sum is Not False.
'
' However, a recordset may not be populated and DSum would return a Null under that
' circumstance. Hence the Nz function is employed to convert any Null to False.
'
' Therefore, if all Fields are False IOR there are no fields then a valid comparison
' can be made with False.
'
' But the reverse is not true. We can not use the result of DSum and reliably compare
' it to True. If one check box is True then DSum will return -1, if two check boxes
' are True then DSum will return -2. If we compare the result of DSum(-2) with True(-1)
' the result of the comparison would be False.
'[/color]
Private Sub cmdTestIt_Click()
If Nz(DSum("TestCheck", Me.frmCustomersSubForm.Form.RecordsetClone.Name), False) = False Then
MsgBox "Report can't open."
Else
MsgBox "Report can open."
End If
End Sub