Count

G

Guest

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.
 
G

Guest

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]));
 
G

Guest

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.
 
G

Guest

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 >
 
G

Guest

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 >
 
K

Ken Snell [MVP]

OK - let's try this minor change for the subform attempt:

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

I had included the table names in the WHERE clause, but I note that the
query itself does not contain "duplicate" field names in the output field
list, so this should work.

Also, the above assumes that you have a control named "contactid" on the
parent form's Recordsource. Change it to the actual name of the control if
it's named differently.
--

Ken Snell
<MS ACCESS MVP>



Stephanie said:
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 >
 
G

Guest

Thanks for the reply.
I still can't get DCount to work, but all of your poking around has led me
to believe that I need to streamline the query. I actually don't care about
PrimaryOwner. A Contact can be the primary owner, but if they aren't
certified with the animal through NatlCertResults, they can't volunteer with
an animal that they own.

So the where clause in my query is now:
WHERE (((Animals.AnimalsID)=[natlcertresults].[animalsid]) AND
((Contacts.ContactID)=[natlcertresults].[contactid] And
(Contacts.ContactID)=[forms]![individuals]![contactid]))


So I tried to streamline the DCount, originally:
=DCount("*","qryAnimals1","[PrimaryOwner]=" & [Parent]![ContactID]
& " OR [ContactID]=" & [Parent]![contactid])

to take out PrimaryOwner, so I tried:
=DCount("*","qryAnimals1","[ContactID]=" & [Parent]![contactid])

which gives me #Error.

Just to be clear, when you say Parent, which form do you mean? The DCount
field is on Animals1 form which is on the main form Individuals. To me, it
seems with that set up, Animals1 is the Parent. I tried replacing Parent
with [Forms]![Individuals]![contactid] which didn't work either.

The record source of both Animals1 and Individuals contains ContactID.

Sorry to be such a pain! I appreciate your time. Thanks!

Ken Snell said:
OK - let's try this minor change for the subform attempt:

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

I had included the table names in the WHERE clause, but I note that the
query itself does not contain "duplicate" field names in the output field
list, so this should work.

Also, the above assumes that you have a control named "contactid" on the
parent form's Recordsource. Change it to the actual name of the control if
it's named differently.
--

Ken Snell
<MS ACCESS MVP>



Stephanie said:
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>


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 >
 
K

Ken Snell [MVP]

Comments inline....

--

Ken Snell
<MS ACCESS MVP>

Stephanie said:
Thanks for the reply.
I still can't get DCount to work, but all of your poking around has led me
to believe that I need to streamline the query. I actually don't care
about
PrimaryOwner. A Contact can be the primary owner, but if they aren't
certified with the animal through NatlCertResults, they can't volunteer
with
an animal that they own.

So the where clause in my query is now:
WHERE (((Animals.AnimalsID)=[natlcertresults].[animalsid]) AND
((Contacts.ContactID)=[natlcertresults].[contactid] And
(Contacts.ContactID)=[forms]![individuals]![contactid]))


So I tried to streamline the DCount, originally:
=DCount("*","qryAnimals1","[PrimaryOwner]=" & [Parent]![ContactID]
& " OR [ContactID]=" & [Parent]![contactid])

to take out PrimaryOwner, so I tried:
=DCount("*","qryAnimals1","[ContactID]=" & [Parent]![contactid])

which gives me #Error.

As I noted earlier, the above syntax assumes that you have a * control *
named "contactid" on the form "Individuals". It's not enough to have the
field in the form's recordsource because the subform is not able to read the
value of a field directly; it must read the value of a control that is bound
to that field. Look on the Individuals form and if you do not have a control
that is bound to "contactid" field, put one on the form. Once you have a
control bound to contactid field, note the name of the control, and put it
in the DCount expression:

=DCount("*","qryAnimals1","[ContactID]=" &
[Parent]![NameOfControlBoundTocontactidField])


Just to be clear, when you say Parent, which form do you mean?

When you use Parent on a subform, it refers to the main form in which the
subform is being used. So, yes, in this case, Parent refers to the form
"Individuals".

The DCount
field is on Animals1 form which is on the main form Individuals. To me,
it
seems with that set up, Animals1 is the Parent. I tried replacing Parent
with [Forms]![Individuals]![contactid] which didn't work either.

Which is further confirmation of my note above that you do not have a
control named contactid on the Individuals form.
The record source of both Animals1 and Individuals contains ContactID.

Sorry to be such a pain! I appreciate your time. Thanks!

< snipped >
 
G

Guest

Thanks for the step through. My Access knowledge seems to be steady state.
Every time I learn something, I forget something else.

Everything is working now. Thanks for your help!


Ken Snell said:
Comments inline....

--

Ken Snell
<MS ACCESS MVP>

Stephanie said:
Thanks for the reply.
I still can't get DCount to work, but all of your poking around has led me
to believe that I need to streamline the query. I actually don't care
about
PrimaryOwner. A Contact can be the primary owner, but if they aren't
certified with the animal through NatlCertResults, they can't volunteer
with
an animal that they own.

So the where clause in my query is now:
WHERE (((Animals.AnimalsID)=[natlcertresults].[animalsid]) AND
((Contacts.ContactID)=[natlcertresults].[contactid] And
(Contacts.ContactID)=[forms]![individuals]![contactid]))


So I tried to streamline the DCount, originally:
=DCount("*","qryAnimals1","[PrimaryOwner]=" & [Parent]![ContactID]
& " OR [ContactID]=" & [Parent]![contactid])

to take out PrimaryOwner, so I tried:
=DCount("*","qryAnimals1","[ContactID]=" & [Parent]![contactid])

which gives me #Error.

As I noted earlier, the above syntax assumes that you have a * control *
named "contactid" on the form "Individuals". It's not enough to have the
field in the form's recordsource because the subform is not able to read the
value of a field directly; it must read the value of a control that is bound
to that field. Look on the Individuals form and if you do not have a control
that is bound to "contactid" field, put one on the form. Once you have a
control bound to contactid field, note the name of the control, and put it
in the DCount expression:

=DCount("*","qryAnimals1","[ContactID]=" &
[Parent]![NameOfControlBoundTocontactidField])


Just to be clear, when you say Parent, which form do you mean?

When you use Parent on a subform, it refers to the main form in which the
subform is being used. So, yes, in this case, Parent refers to the form
"Individuals".

The DCount
field is on Animals1 form which is on the main form Individuals. To me,
it
seems with that set up, Animals1 is the Parent. I tried replacing Parent
with [Forms]![Individuals]![contactid] which didn't work either.

Which is further confirmation of my note above that you do not have a
control named contactid on the Individuals form.
The record source of both Animals1 and Individuals contains ContactID.

Sorry to be such a pain! I appreciate your time. Thanks!

< snipped >
 
G

Guest

Ken,
Sorry to bug you again.
The count is working but it is based on the # of times an Animal has
certified with a Contact. I only want to count 1 animal 1 time.

So I think I need to use max on the animal certification (NatlCertDate), but
am running into the "group by" issue again. I tired to use 2 queries, one to
show the max vertification, but then the count was off. I'd appreciate any
guidance:

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,
NatlCertResults.NatlCertDate
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] And
(Contacts.ContactID)=[forms]![individuals]![contactid]));


Ken Snell said:
Comments inline....

--

Ken Snell
<MS ACCESS MVP>

Stephanie said:
Thanks for the reply.
I still can't get DCount to work, but all of your poking around has led me
to believe that I need to streamline the query. I actually don't care
about
PrimaryOwner. A Contact can be the primary owner, but if they aren't
certified with the animal through NatlCertResults, they can't volunteer
with
an animal that they own.

So the where clause in my query is now:
WHERE (((Animals.AnimalsID)=[natlcertresults].[animalsid]) AND
((Contacts.ContactID)=[natlcertresults].[contactid] And
(Contacts.ContactID)=[forms]![individuals]![contactid]))


So I tried to streamline the DCount, originally:
=DCount("*","qryAnimals1","[PrimaryOwner]=" & [Parent]![ContactID]
& " OR [ContactID]=" & [Parent]![contactid])

to take out PrimaryOwner, so I tried:
=DCount("*","qryAnimals1","[ContactID]=" & [Parent]![contactid])

which gives me #Error.

As I noted earlier, the above syntax assumes that you have a * control *
named "contactid" on the form "Individuals". It's not enough to have the
field in the form's recordsource because the subform is not able to read the
value of a field directly; it must read the value of a control that is bound
to that field. Look on the Individuals form and if you do not have a control
that is bound to "contactid" field, put one on the form. Once you have a
control bound to contactid field, note the name of the control, and put it
in the DCount expression:

=DCount("*","qryAnimals1","[ContactID]=" &
[Parent]![NameOfControlBoundTocontactidField])


Just to be clear, when you say Parent, which form do you mean?

When you use Parent on a subform, it refers to the main form in which the
subform is being used. So, yes, in this case, Parent refers to the form
"Individuals".

The DCount
field is on Animals1 form which is on the main form Individuals. To me,
it
seems with that set up, Animals1 is the Parent. I tried replacing Parent
with [Forms]![Individuals]![contactid] which didn't work either.

Which is further confirmation of my note above that you do not have a
control named contactid on the Individuals form.
The record source of both Animals1 and Individuals contains ContactID.

Sorry to be such a pain! I appreciate your time. Thanks!

< snipped >
 
K

Ken Snell [MVP]

OK - let's create a completely new query that we'll use solely for the
DCount function on your form.

Create the following query (name it qryAnimalContactCount):

SELECT Animals.AnimalsID, Contacts.ContactID
FROM (Animals INNER JOIN NatlCertResults
ON Animals.AnimalsID = NatlCertResults.AnimalsID)
INNER JOIN Contacts
ON NatlCertResults.ContactID = Contacts.ContactID
GROUP BY Animals.AnimalsID, Contacts.ContactID;

Then change the DCount function expression to this:

=DCount("*","qryAnimalContactCount","[ContactID]=" &
[Parent]![NameOfControlBoundTocontactidField])

--

Ken Snell
<MS ACCESS MVP>


Stephanie said:
Ken,
Sorry to bug you again.
The count is working but it is based on the # of times an Animal has
certified with a Contact. I only want to count 1 animal 1 time.

So I think I need to use max on the animal certification (NatlCertDate),
but
am running into the "group by" issue again. I tired to use 2 queries, one
to
show the max vertification, but then the count was off. I'd appreciate
any
guidance:

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,
NatlCertResults.NatlCertDate
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] And
(Contacts.ContactID)=[forms]![individuals]![contactid]));


Ken Snell said:
Comments inline....

--

Ken Snell
<MS ACCESS MVP>

Stephanie said:
Thanks for the reply.
I still can't get DCount to work, but all of your poking around has led
me
to believe that I need to streamline the query. I actually don't care
about
PrimaryOwner. A Contact can be the primary owner, but if they aren't
certified with the animal through NatlCertResults, they can't volunteer
with
an animal that they own.

So the where clause in my query is now:
WHERE (((Animals.AnimalsID)=[natlcertresults].[animalsid]) AND
((Contacts.ContactID)=[natlcertresults].[contactid] And
(Contacts.ContactID)=[forms]![individuals]![contactid]))


So I tried to streamline the DCount, originally:
=DCount("*","qryAnimals1","[PrimaryOwner]=" & [Parent]![ContactID]
& " OR [ContactID]=" & [Parent]![contactid])

to take out PrimaryOwner, so I tried:
=DCount("*","qryAnimals1","[ContactID]=" & [Parent]![contactid])

which gives me #Error.

As I noted earlier, the above syntax assumes that you have a * control *
named "contactid" on the form "Individuals". It's not enough to have the
field in the form's recordsource because the subform is not able to read
the
value of a field directly; it must read the value of a control that is
bound
to that field. Look on the Individuals form and if you do not have a
control
that is bound to "contactid" field, put one on the form. Once you have a
control bound to contactid field, note the name of the control, and put
it
in the DCount expression:

=DCount("*","qryAnimals1","[ContactID]=" &
[Parent]![NameOfControlBoundTocontactidField])


Just to be clear, when you say Parent, which form do you mean?

When you use Parent on a subform, it refers to the main form in which the
subform is being used. So, yes, in this case, Parent refers to the form
"Individuals".

The DCount
field is on Animals1 form which is on the main form Individuals. To
me,
it
seems with that set up, Animals1 is the Parent. I tried replacing
Parent
with [Forms]![Individuals]![contactid] which didn't work either.

Which is further confirmation of my note above that you do not have a
control named contactid on the Individuals form.
The record source of both Animals1 and Individuals contains ContactID.

Sorry to be such a pain! I appreciate your time. Thanks!

< snipped >
 
G

Guest

Perfect!
Thanks for all of your help. I'm a little depressed that I'll never figure
it all out. I think I followed along, but would never be able to write, what
it for me, such a complicated "FROM" statement. But I do see the allure of
keeping it out of the WHERE statement.
Glad you're such a gracious MVP!


Ken Snell said:
OK - let's create a completely new query that we'll use solely for the
DCount function on your form.

Create the following query (name it qryAnimalContactCount):

SELECT Animals.AnimalsID, Contacts.ContactID
FROM (Animals INNER JOIN NatlCertResults
ON Animals.AnimalsID = NatlCertResults.AnimalsID)
INNER JOIN Contacts
ON NatlCertResults.ContactID = Contacts.ContactID
GROUP BY Animals.AnimalsID, Contacts.ContactID;

Then change the DCount function expression to this:

=DCount("*","qryAnimalContactCount","[ContactID]=" &
[Parent]![NameOfControlBoundTocontactidField])

--

Ken Snell
<MS ACCESS MVP>


Stephanie said:
Ken,
Sorry to bug you again.
The count is working but it is based on the # of times an Animal has
certified with a Contact. I only want to count 1 animal 1 time.

So I think I need to use max on the animal certification (NatlCertDate),
but
am running into the "group by" issue again. I tired to use 2 queries, one
to
show the max vertification, but then the count was off. I'd appreciate
any
guidance:

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,
NatlCertResults.NatlCertDate
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] And
(Contacts.ContactID)=[forms]![individuals]![contactid]));


Ken Snell said:
Comments inline....

--

Ken Snell
<MS ACCESS MVP>

Thanks for the reply.
I still can't get DCount to work, but all of your poking around has led
me
to believe that I need to streamline the query. I actually don't care
about
PrimaryOwner. A Contact can be the primary owner, but if they aren't
certified with the animal through NatlCertResults, they can't volunteer
with
an animal that they own.

So the where clause in my query is now:
WHERE (((Animals.AnimalsID)=[natlcertresults].[animalsid]) AND
((Contacts.ContactID)=[natlcertresults].[contactid] And
(Contacts.ContactID)=[forms]![individuals]![contactid]))


So I tried to streamline the DCount, originally:
=DCount("*","qryAnimals1","[PrimaryOwner]=" & [Parent]![ContactID]
& " OR [ContactID]=" & [Parent]![contactid])

to take out PrimaryOwner, so I tried:
=DCount("*","qryAnimals1","[ContactID]=" & [Parent]![contactid])

which gives me #Error.

As I noted earlier, the above syntax assumes that you have a * control *
named "contactid" on the form "Individuals". It's not enough to have the
field in the form's recordsource because the subform is not able to read
the
value of a field directly; it must read the value of a control that is
bound
to that field. Look on the Individuals form and if you do not have a
control
that is bound to "contactid" field, put one on the form. Once you have a
control bound to contactid field, note the name of the control, and put
it
in the DCount expression:

=DCount("*","qryAnimals1","[ContactID]=" &
[Parent]![NameOfControlBoundTocontactidField])




Just to be clear, when you say Parent, which form do you mean?

When you use Parent on a subform, it refers to the main form in which the
subform is being used. So, yes, in this case, Parent refers to the form
"Individuals".


The DCount
field is on Animals1 form which is on the main form Individuals. To
me,
it
seems with that set up, Animals1 is the Parent. I tried replacing
Parent
with [Forms]![Individuals]![contactid] which didn't work either.

Which is further confirmation of my note above that you do not have a
control named contactid on the Individuals form.


The record source of both Animals1 and Individuals contains ContactID.

Sorry to be such a pain! I appreciate your time. Thanks!


< 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