TriState is a property of a check box control, it does not
apply to a field in a table/query. Furthermore a TriState
checkbox can have any value, limited only by the type of the
numeric (or even a date) field it is bound to, any non-zero,
non-Null value in the bound field will display a check in
the check box.
No, you are not misunderstanding about what will be counted,
I'm just not saying it very well. What I said applies to
both Count(field) and DCount("field", "table") without
criteria. Using criteria on a boolean field, and, once
again, ignoring the complexity of a filtered form/report
recordset,
DCount("field", "table", "field = True")
is equivalent to any of these commonly used aggregates:
Count(IIf(field, x, Null)
Sum(IIf(field, 1, 0)
Sum(IIf(field, 1, Null)
Abs(Sum(field))
I know that you understand all that. The point I have been
trying to address is that it can be very difficult to
specify the proper criteria in a Domain Aggregate function
to match the form/report/query filtering.
And I was really aiming the comment about zero and Null at
other people who may be following this discussion that may
not have made that distinction yet. I apologize, I should
have stated that instead of assuming it would be understood.
You statement regarding Tri-state is not correct. A tri-state check box will
return -1 for true, 0 for false, and Null for no selection.
I think I may be misunderstanding what you are saying about what will and
will not be counted. If your criteria requests only True, then you will get
a count of records with true in the field. It will not count True and Null
or any other value.
I will let the comment about 0 and null being different go. I'm a bit more
advanced than that.
:
A DCount is just a function wrapper around a temporary
query. Except for the criteria issues, the items that are
counted are the same. If performance is not an issue and
leaving aside the complex problems of applying a form's
filtering criteria, the only advantage of using DCount is
that you can use a criteria to select the only the True
values to count.
As with all aggregation functions, except Count(*),
Count(field) and DCount("field"," table") will both count
all non-Null values. So, in the case of a field displayed
as a triple state check box, Count(field) will count both
the True and False values. It will not count the Null
records, nor will it count only the True records (zero is
not the same as Null).
Note that a field displayed as a triple state check box must
be a numeric type such as Integer, because a boolean field
can never be Null.
Klatuu wrote:
My original suggestion was a DCount. Are you saying that if a boolean field
were bound to a tri-state control and thus had a null value, that those rows
would be counted with the Trues? What about the Falses?
We all have our preferences and styles. It is good that there is this group
to share and debate information. I have learned a lot here and it has
improved what I do.
Klatuu wrote:
True, but in this case, there is no mention of a form. His basic question was:
how I can count only the positive responses from a yes no field.
We don't know where he wants to use this. If it is a query or a textbox on
a form or report, then the Sum() would be better than a Domain Aggregate
function, but the Count() would be better because it is straight forward and
does not require an addition function or statement. like:
Abs(Sum([checkfield]))
or
Sum([checkfield]) * -1
:
Well, I assumed that since the question was asked in a Forms
newsgroup, the question related to a form situation, but,
you're right, it was never explicitly stated.
As for counting records with True values, you can not use a
simple Count(field) because Count will count all non-Null
values, which includes the records with a value of False as
well as True. If you want to use Count, then one way is to
use an expression such as:
Count(IIf(field, x, Null))
where x can be any non-Null literal.
Personally I think it's clearer, easier and faster to use
Sum as Fred suggested, but that's your call.