Count

S

Stephanie

Hi. I have a sql query that does what I want it to do- gives me, based on
contact, the count of animals associate with the contact, and the count of
how many of those animals are retired or deceased.

I created a form from this query that I want to use as a subform on my
Animals form. In order to do so, I need AnimalsID as the linking field.
However, if I add AnimalsID to my query, my "counts" are now by AnimalsID,
not the count of animals associated with the Contact. I'd appreciate
suggestions. Thanks!

Here's my query:
SELECT Count(AnimalCount.AnimalsID) AS CountOfAnimalsID,
Count(AnimalCount.Retired) AS CountOfRetired, Count(AnimalCount.Deceased) AS
CountOfDeceased, [CountOfAnimalsID]-[CountOfRetired]-[CountOfDeceased] AS
Active
FROM AnimalCount
WHERE ((([Animals].[AnimalsID])=[natlcertresults].[animalsid]) AND
(([Contacts].[ContactID])=[natlcertresults].[contactid])) OR
((([Animals].[PrimaryOwner])=[natlcertresults].[contactid])) OR
((([Contacts].[ContactID])=[forms]![individuals]![contactid]));
 
K

Ken Snell [MVP]

If the query is giving you data based on a ContactID association, why do you
also need an AnimalID association? I see that you're filtering the query to
just a single AnimalsID, so I admit that I'm not understanding what you want
to do..

Tell us about the form -- what is on the main form that needs to link to the
subform (and this query). Maybe that will help me see how to do what you
want.
 
S

Stephanie

Sorry- it's always difficult to determine what is too much information.

Contacts certify with Animals (many to many). My main form has Contact info
and has a sfrmAnimals that has Animals info (only those animals that
certified with the Contact appear). The sfrmAnimals is a single view form
and I have a button to select the next Animal associated with the Contact.
The form doesn't have navigation buttons and no record counter. I wanted to
have a subform on sfrmAnimals to indicate how many animals are associated
with the Contact (so the user will know when to click the next pet button),
and how many of the animals are retired/deceased.

Seems kind of complicated just to get a count! I'd appreciate suggestions.
Thanks!

Ken Snell said:
If the query is giving you data based on a ContactID association, why do you
also need an AnimalID association? I see that you're filtering the query to
just a single AnimalsID, so I admit that I'm not understanding what you want
to do..

Tell us about the form -- what is on the main form that needs to link to the
subform (and this query). Maybe that will help me see how to do what you
want.

--

Ken Snell
<MS ACCESS MVP>

Stephanie said:
Hi. I have a sql query that does what I want it to do- gives me, based on
contact, the count of animals associate with the contact, and the count of
how many of those animals are retired or deceased.

I created a form from this query that I want to use as a subform on my
Animals form. In order to do so, I need AnimalsID as the linking field.
However, if I add AnimalsID to my query, my "counts" are now by AnimalsID,
not the count of animals associated with the Contact. I'd appreciate
suggestions. Thanks!

Here's my query:
SELECT Count(AnimalCount.AnimalsID) AS CountOfAnimalsID,
Count(AnimalCount.Retired) AS CountOfRetired, Count(AnimalCount.Deceased)
AS
CountOfDeceased, [CountOfAnimalsID]-[CountOfRetired]-[CountOfDeceased] AS
Active
FROM AnimalCount
WHERE ((([Animals].[AnimalsID])=[natlcertresults].[animalsid]) AND
(([Contacts].[ContactID])=[natlcertresults].[contactid])) OR
((([Animals].[PrimaryOwner])=[natlcertresults].[contactid])) OR
((([Contacts].[ContactID])=[forms]![individuals]![contactid]));
 
J

John Vinson

Hi. I have a sql query that does what I want it to do- gives me, based on
contact, the count of animals associate with the contact, and the count of
how many of those animals are retired or deceased.

I created a form from this query that I want to use as a subform on my
Animals form. In order to do so, I need AnimalsID as the linking field.
However, if I add AnimalsID to my query, my "counts" are now by AnimalsID,
not the count of animals associated with the Contact. I'd appreciate
suggestions. Thanks!

The Animals form presumably has just one animal; how can you "count"
multiple animals if there's just one? It sounds like the ContactID
would be a better master/child link field; is it available on the
mainform?

John W. Vinson[MVP]
 
K

Ken Snell [MVP]

For what you want to do, I would not use a subform for the counts. Instead,
I would put textboxes in the Form Footer section ofo the sfrmAnimals
subform, and then use DCount function in expressions in the control sources
to get your counts. For example, to get the count of animals total, based on
the query (does that query have errors in it? you're referencing some tables
that are not in the query's data sources) you posted, assuming that you have
a field or control named [contactid] in the subform's recordsource (which I
assume you do because it's the linking field between subform and main form):

=DCount("AnimalID", "AnimalCount", "[ContactID]=" & [contactid])

Etc.

--

Ken Snell
<MS ACCESS MVP>



Stephanie said:
Sorry- it's always difficult to determine what is too much information.

Contacts certify with Animals (many to many). My main form has Contact
info
and has a sfrmAnimals that has Animals info (only those animals that
certified with the Contact appear). The sfrmAnimals is a single view form
and I have a button to select the next Animal associated with the Contact.
The form doesn't have navigation buttons and no record counter. I wanted
to
have a subform on sfrmAnimals to indicate how many animals are associated
with the Contact (so the user will know when to click the next pet
button),
and how many of the animals are retired/deceased.

Seems kind of complicated just to get a count! I'd appreciate
suggestions.
Thanks!

Ken Snell said:
If the query is giving you data based on a ContactID association, why do
you
also need an AnimalID association? I see that you're filtering the query
to
just a single AnimalsID, so I admit that I'm not understanding what you
want
to do..

Tell us about the form -- what is on the main form that needs to link to
the
subform (and this query). Maybe that will help me see how to do what you
want.

--

Ken Snell
<MS ACCESS MVP>

Stephanie said:
Hi. I have a sql query that does what I want it to do- gives me, based
on
contact, the count of animals associate with the contact, and the count
of
how many of those animals are retired or deceased.

I created a form from this query that I want to use as a subform on my
Animals form. In order to do so, I need AnimalsID as the linking
field.
However, if I add AnimalsID to my query, my "counts" are now by
AnimalsID,
not the count of animals associated with the Contact. I'd appreciate
suggestions. Thanks!

Here's my query:
SELECT Count(AnimalCount.AnimalsID) AS CountOfAnimalsID,
Count(AnimalCount.Retired) AS CountOfRetired,
Count(AnimalCount.Deceased)
AS
CountOfDeceased, [CountOfAnimalsID]-[CountOfRetired]-[CountOfDeceased]
AS
Active
FROM AnimalCount
WHERE ((([Animals].[AnimalsID])=[natlcertresults].[animalsid]) AND
(([Contacts].[ContactID])=[natlcertresults].[contactid])) OR
((([Animals].[PrimaryOwner])=[natlcertresults].[contactid])) OR
((([Contacts].[ContactID])=[forms]![individuals]![contactid]));
 
S

Stephanie

Curious. You are correct that ContactID would work on sfrmAnimals, giving me
the count that I require. However, once I had sfrmAnimalCount on sfrmAnimals
(which in on frmIndividuals), frmIndividuals wouldn't open without entering
the ContactID. And then when it did open, sfrmAnimalCount was blank. I
admit, the setup is complicated- thankfully the fabulous discussion group
folks helped me set it up long ago.
I'm going to post the underlying queries in my reply to Ken to as to
minimize space in the discussion group- hope you'll keep playing! Thanks.
 
S

Stephanie

Ken, thanks! DCount didn't work, no doubt due to my set up. So here's the
whole thing:

frmIndividuals is based on tblContacts (ContactID as PK).

sfrmAnimals is linked to frmIndividuals on ContactID and is based on a
qryAnimals1:
SELECT Animals.AnimalName, AnimalType.AnimalType, Animals.AnimalBreed,
Animals.DateOfBirth, Animals.Retired, Animals.Deceased, Animals.AnimalsID,
Nz([Contacts].[NickName],[Contacts].[FirstName]) & " " &
[Contacts].[LastName] AS [Member Name], Contacts.ContactID,
Animals.PrimaryOwner
FROM (AnimalType RIGHT JOIN Animals ON AnimalType.AnimalTypeID =
Animals.AnimalTypeID) INNER JOIN (Contacts INNER JOIN NatlCertResults ON
Contacts.ContactID = NatlCertResults.ContactID) ON Animals.AnimalsID =
NatlCertResults.AnimalsID
WHERE (((Animals.AnimalsID)=[natlcertresults].[animalsid]) AND
((Contacts.ContactID)=[natlcertresults].[contactid])) OR
(((Animals.PrimaryOwner)=[natlcertresults].[contactid])) OR
(((Contacts.ContactID)=[forms]![individuals]![contactid]));

this gives me all animals that have certified with the contact- when I'm on
frmIndividuals sfrmAnimals displays the correct animals without neede to
enter ContactID. And for reasons that escape me, this query is unable to
also give me the animal count.

So I wrote another query (and have streamlined the whole process so
qryAnimals1 is the basis for sfrmAnimals and the qryCountOfAnimals):
SELECT Count(Animals1.AnimalsID) AS CountOfAnimalsID,
Count(Animals1.Retired) AS CountOfRetired, Count(Animals1.Deceased) AS
CountOfDeceased, [CountOfAnimalsID]-[CountOfRetired]-[CountOfDeceased] AS
Active, Animals1.ContactID
FROM Animals1
WHERE ((([Animals].[AnimalsID])=[natlcertresults].[animalsid]) AND
(([Contacts].[ContactID])=[natlcertresults].[contactid])) OR
((([Animals].[PrimaryOwner])=[natlcertresults].[contactid])) OR
((([Contacts].[ContactID])=[forms]![individuals]![contactid]))
GROUP BY Animals1.ContactID;

All of the underlying queries and the forms themselves give me the correct
information- contacts and animals. Now I'd like to be able to state how many
animals each contact has certified with, displayed on Individuals form.

But I think you are correct, a subform for animal count gives heartburn- I
have to enter the ContactID over and over for each record. Perhaps you can
better direct me to use DCount now that I have (I hope!) explained my set up.
Thanks for your time.

Ken Snell said:
For what you want to do, I would not use a subform for the counts. Instead,
I would put textboxes in the Form Footer section ofo the sfrmAnimals
subform, and then use DCount function in expressions in the control sources
to get your counts. For example, to get the count of animals total, based on
the query (does that query have errors in it? you're referencing some tables
that are not in the query's data sources) you posted, assuming that you have
a field or control named [contactid] in the subform's recordsource (which I
assume you do because it's the linking field between subform and main form):

=DCount("AnimalID", "AnimalCount", "[ContactID]=" & [contactid])

Etc.

--

Ken Snell
<MS ACCESS MVP>



Stephanie said:
Sorry- it's always difficult to determine what is too much information.

Contacts certify with Animals (many to many). My main form has Contact
info
and has a sfrmAnimals that has Animals info (only those animals that
certified with the Contact appear). The sfrmAnimals is a single view form
and I have a button to select the next Animal associated with the Contact.
The form doesn't have navigation buttons and no record counter. I wanted
to
have a subform on sfrmAnimals to indicate how many animals are associated
with the Contact (so the user will know when to click the next pet
button),
and how many of the animals are retired/deceased.

Seems kind of complicated just to get a count! I'd appreciate
suggestions.
Thanks!

Ken Snell said:
If the query is giving you data based on a ContactID association, why do
you
also need an AnimalID association? I see that you're filtering the query
to
just a single AnimalsID, so I admit that I'm not understanding what you
want
to do..

Tell us about the form -- what is on the main form that needs to link to
the
subform (and this query). Maybe that will help me see how to do what you
want.

--

Ken Snell
<MS ACCESS MVP>

Hi. I have a sql query that does what I want it to do- gives me, based
on
contact, the count of animals associate with the contact, and the count
of
how many of those animals are retired or deceased.

I created a form from this query that I want to use as a subform on my
Animals form. In order to do so, I need AnimalsID as the linking
field.
However, if I add AnimalsID to my query, my "counts" are now by
AnimalsID,
not the count of animals associated with the Contact. I'd appreciate
suggestions. Thanks!

Here's my query:
SELECT Count(AnimalCount.AnimalsID) AS CountOfAnimalsID,
Count(AnimalCount.Retired) AS CountOfRetired,
Count(AnimalCount.Deceased)
AS
CountOfDeceased, [CountOfAnimalsID]-[CountOfRetired]-[CountOfDeceased]
AS
Active
FROM AnimalCount
WHERE ((([Animals].[AnimalsID])=[natlcertresults].[animalsid]) AND
(([Contacts].[ContactID])=[natlcertresults].[contactid])) OR
((([Animals].[PrimaryOwner])=[natlcertresults].[contactid])) OR
((([Contacts].[ContactID])=[forms]![individuals]![contactid]));
 
K

Ken Snell [MVP]

Let me answer your question about why qryAnimals1 query won't also return a
count of animals for you. To get a count, you must use a "totals" query,
which aggregates records based on the GROUP BY clause in the query. If you
wanted to use qryAnimals1 as a "counting" query, you would have to
"sacrifice" the details about each animal in order to count how many animals
belong to a single contactID. That of course would defeat the purpose of the
query, which is to give you a list of the individual animals.

It's possible to get the count in the query as part of each record, but it
would be somewhat inefficient because it would require running a subquery
for each AnimalID, and the subquery would return the same value for all
animals belonging to a single ContactID... in other words, if 10 animals
belong to a ContactID, the subquery would run 10 times just to return the
same number. So I still think using a DCount query will be better for you.
Now, let's see which query is the best to use for this.

I've studied your qryAnimals1 and qryCountOfAnimals queries to see how the
WHRE clauses work.

To make it easier for me to "see", I've split out the WHERE clause for the
qryAnimals1 query this way:

(
((Animals.AnimalsID)=[natlcertresults].[animalsid])
AND
((Contacts.ContactID)=[natlcertresults].[contactid])
)

OR

(
((Animals.PrimaryOwner)=[natlcertresults].[contactid])
)

OR

(
((Contacts.ContactID)=[forms]![individuals]![contactid])
);


This shows that you're using three separate "WHERE" subclauses, all
separated by OR, with the first subclause having an AND for the first two
items. It appears that qryCountOfAnimals uses the same WHERE structure.

In this query, you're using a reference to the "Individuals" form to get a
ContactID value. If you use a subform's LinkChildFields and LinkMasterFields
properties, you can forgo a reference to the form and let the form/subform
setup handle it for you. However, in this case I'm not sure that letting the
form/subform setup do this for you will be good, because that "WHERE"
subclause is just one of three ways to get records returned to the subform,
and using the "Links" properties may exclude some records that you want to
include.

So, let me see if I'm understanding... you would want to return the count of
animals if the "ContactID" value on the form "Individuals" matches the
Animals.PrimaryOwner or the Contacts.ContactID in the query qryAnimals1 --
is this correct?

Try this as the DCount expression (all one line, so watch for
line-wrapping):

=DCount("*", "qryAnimals1", "[Animals].[PrimaryOwner]=" &
[Parent].[ContactID] & " OR [Contacts].[ContactID]=" & [Parent].[contactid])

The above DCount will return the count of records where the PrimaryOwner or
the ContactID in the query matches the value of the ContactID on the parent
form (Individuals).

You shouldn't need to use the qryCountOfAnimals query at all.
--

Ken Snell
<MS ACCESS MVP>


Stephanie said:
Ken, thanks! DCount didn't work, no doubt due to my set up. So here's
the
whole thing:

frmIndividuals is based on tblContacts (ContactID as PK).

sfrmAnimals is linked to frmIndividuals on ContactID and is based on a
qryAnimals1:
SELECT Animals.AnimalName, AnimalType.AnimalType, Animals.AnimalBreed,
Animals.DateOfBirth, Animals.Retired, Animals.Deceased, Animals.AnimalsID,
Nz([Contacts].[NickName],[Contacts].[FirstName]) & " " &
[Contacts].[LastName] AS [Member Name], Contacts.ContactID,
Animals.PrimaryOwner
FROM (AnimalType RIGHT JOIN Animals ON AnimalType.AnimalTypeID =
Animals.AnimalTypeID) INNER JOIN (Contacts INNER JOIN NatlCertResults ON
Contacts.ContactID = NatlCertResults.ContactID) ON Animals.AnimalsID =
NatlCertResults.AnimalsID
WHERE (((Animals.AnimalsID)=[natlcertresults].[animalsid]) AND
((Contacts.ContactID)=[natlcertresults].[contactid])) OR
(((Animals.PrimaryOwner)=[natlcertresults].[contactid])) OR
(((Contacts.ContactID)=[forms]![individuals]![contactid]));

this gives me all animals that have certified with the contact- when I'm
on
frmIndividuals sfrmAnimals displays the correct animals without neede to
enter ContactID. And for reasons that escape me, this query is unable to
also give me the animal count.

So I wrote another query (and have streamlined the whole process so
qryAnimals1 is the basis for sfrmAnimals and the qryCountOfAnimals):
SELECT Count(Animals1.AnimalsID) AS CountOfAnimalsID,
Count(Animals1.Retired) AS CountOfRetired, Count(Animals1.Deceased) AS
CountOfDeceased, [CountOfAnimalsID]-[CountOfRetired]-[CountOfDeceased] AS
Active, Animals1.ContactID
FROM Animals1
WHERE ((([Animals].[AnimalsID])=[natlcertresults].[animalsid]) AND
(([Contacts].[ContactID])=[natlcertresults].[contactid])) OR
((([Animals].[PrimaryOwner])=[natlcertresults].[contactid])) OR
((([Contacts].[ContactID])=[forms]![individuals]![contactid]))
GROUP BY Animals1.ContactID;

All of the underlying queries and the forms themselves give me the correct
information- contacts and animals. Now I'd like to be able to state how
many
animals each contact has certified with, displayed on Individuals form.

But I think you are correct, a subform for animal count gives heartburn- I
have to enter the ContactID over and over for each record. Perhaps you
can
better direct me to use DCount now that I have (I hope!) explained my set
up.
Thanks for your time.


< snipped >
 
S

Stephanie

Ken,
Thanks for taking the time to ponder on this one! Sorry- I didn't see the
reply at an earlier date.
Your understanding is correct, but I still can't get DCount to work. No
doubt, I put the test field in the wrong place.

I put a text field, built using the test box icon. In the record source I
put your DCount line:
=DCount("*","qryAnimals1","[Animals].[PrimaryOwner]=" & [Parent].[ContactID]
& " OR Contacts].[ContactID]=" & [Parent].[contactid])

Since the text field is on Animals1 which is on Individuals, that makes the
reference in DCount of "Parent" mean "Individuals". Correct?

When I look at Animals1 form by itself, all I see in the DCount text field
is #Name?
When I pull up Individuals form all I see in the DCount text field is #Error

Even when you do all of the work, I can't do it correctly! Any suggestions?
Thanks for the help.


Ken Snell said:
Let me answer your question about why qryAnimals1 query won't also return a
count of animals for you. To get a count, you must use a "totals" query,
which aggregates records based on the GROUP BY clause in the query. If you
wanted to use qryAnimals1 as a "counting" query, you would have to
"sacrifice" the details about each animal in order to count how many animals
belong to a single contactID. That of course would defeat the purpose of the
query, which is to give you a list of the individual animals.

It's possible to get the count in the query as part of each record, but it
would be somewhat inefficient because it would require running a subquery
for each AnimalID, and the subquery would return the same value for all
animals belonging to a single ContactID... in other words, if 10 animals
belong to a ContactID, the subquery would run 10 times just to return the
same number. So I still think using a DCount query will be better for you.
Now, let's see which query is the best to use for this.

I've studied your qryAnimals1 and qryCountOfAnimals queries to see how the
WHRE clauses work.

To make it easier for me to "see", I've split out the WHERE clause for the
qryAnimals1 query this way:

(
((Animals.AnimalsID)=[natlcertresults].[animalsid])
AND
((Contacts.ContactID)=[natlcertresults].[contactid])
)

OR

(
((Animals.PrimaryOwner)=[natlcertresults].[contactid])
)

OR

(
((Contacts.ContactID)=[forms]![individuals]![contactid])
);


This shows that you're using three separate "WHERE" subclauses, all
separated by OR, with the first subclause having an AND for the first two
items. It appears that qryCountOfAnimals uses the same WHERE structure.

In this query, you're using a reference to the "Individuals" form to get a
ContactID value. If you use a subform's LinkChildFields and LinkMasterFields
properties, you can forgo a reference to the form and let the form/subform
setup handle it for you. However, in this case I'm not sure that letting the
form/subform setup do this for you will be good, because that "WHERE"
subclause is just one of three ways to get records returned to the subform,
and using the "Links" properties may exclude some records that you want to
include.

So, let me see if I'm understanding... you would want to return the count of
animals if the "ContactID" value on the form "Individuals" matches the
Animals.PrimaryOwner or the Contacts.ContactID in the query qryAnimals1 --
is this correct?

Try this as the DCount expression (all one line, so watch for
line-wrapping):

=DCount("*", "qryAnimals1", "[Animals].[PrimaryOwner]=" &
[Parent].[ContactID] & " OR [Contacts].[ContactID]=" & [Parent].[contactid])

The above DCount will return the count of records where the PrimaryOwner or
the ContactID in the query matches the value of the ContactID on the parent
form (Individuals).

You shouldn't need to use the qryCountOfAnimals query at all.
--

Ken Snell
<MS ACCESS MVP>


Stephanie said:
Ken, thanks! DCount didn't work, no doubt due to my set up. So here's
the
whole thing:

frmIndividuals is based on tblContacts (ContactID as PK).

sfrmAnimals is linked to frmIndividuals on ContactID and is based on a
qryAnimals1:
SELECT Animals.AnimalName, AnimalType.AnimalType, Animals.AnimalBreed,
Animals.DateOfBirth, Animals.Retired, Animals.Deceased, Animals.AnimalsID,
Nz([Contacts].[NickName],[Contacts].[FirstName]) & " " &
[Contacts].[LastName] AS [Member Name], Contacts.ContactID,
Animals.PrimaryOwner
FROM (AnimalType RIGHT JOIN Animals ON AnimalType.AnimalTypeID =
Animals.AnimalTypeID) INNER JOIN (Contacts INNER JOIN NatlCertResults ON
Contacts.ContactID = NatlCertResults.ContactID) ON Animals.AnimalsID =
NatlCertResults.AnimalsID
WHERE (((Animals.AnimalsID)=[natlcertresults].[animalsid]) AND
((Contacts.ContactID)=[natlcertresults].[contactid])) OR
(((Animals.PrimaryOwner)=[natlcertresults].[contactid])) OR
(((Contacts.ContactID)=[forms]![individuals]![contactid]));

this gives me all animals that have certified with the contact- when I'm
on
frmIndividuals sfrmAnimals displays the correct animals without neede to
enter ContactID. And for reasons that escape me, this query is unable to
also give me the animal count.

So I wrote another query (and have streamlined the whole process so
qryAnimals1 is the basis for sfrmAnimals and the qryCountOfAnimals):
SELECT Count(Animals1.AnimalsID) AS CountOfAnimalsID,
Count(Animals1.Retired) AS CountOfRetired, Count(Animals1.Deceased) AS
CountOfDeceased, [CountOfAnimalsID]-[CountOfRetired]-[CountOfDeceased] AS
Active, Animals1.ContactID
FROM Animals1
WHERE ((([Animals].[AnimalsID])=[natlcertresults].[animalsid]) AND
(([Contacts].[ContactID])=[natlcertresults].[contactid])) OR
((([Animals].[PrimaryOwner])=[natlcertresults].[contactid])) OR
((([Contacts].[ContactID])=[forms]![individuals]![contactid]))
GROUP BY Animals1.ContactID;

All of the underlying queries and the forms themselves give me the correct
information- contacts and animals. Now I'd like to be able to state how
many
animals each contact has certified with, displayed on Individuals form.

But I think you are correct, a subform for animal count gives heartburn- I
have to enter the ContactID over and over for each record. Perhaps you
can
better direct me to use DCount now that I have (I hope!) explained my set
up.
Thanks for your time.


< snipped >
 

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