> SELECT tblDocuments.Entity_ID
FROM tblDocuments LEFT JOIN tblEntity
ON tblEntity.Entity_ID = tblDocuments.Entity_ID
WHERE tblEntity.Entity_ID IS NULL
But there are no Nulls in tblEntity.
Actually, it's a bit more complicated.
There are documents associated with entities. And there are categories of
entities. Documents can be associated with more than one entity, including
entities in multiple categories.
When the user wants to delete a category of entities, the documents
associated with those entities should be deleted - but only if the document
is not associated with another entity in another category. This is because
the document in tblDocuments is actually a hyperlink to a document stored on
disk. So we want a Recordset of all documents that are associated with
entities in the selected category, but are not associated with entities in
other categories.
This will get me documents that are NOT in the selected category and
associated with more than one entity:
[qryA]
SELECT tblDocuments.Doc_ID
FROM tblDocuments INNER JOIN tblEntity ON tblDocuments.Entity_ID =
tblEntity.Entity_ID
WHERE (tblEntity.Cat_ID <> prmCatID And (tblDocuments.Document In (SELECT
tblDocuments.Document FROM tblDocuments GROUP BY tblDocuments.Document
HAVING Count(*)>1));
(I have to join to tblEntity because that's where the Cat_ID is)
This will get me documents that ARE in the selected category and associated
with one or more entity (in that category)
[qryB]
SELECT tblDocuments.Doc_ID
FROM tblDocuments INNER JOIN tblEntity ON tblDocuments.Entity_ID =
tblEntity.Entity_ID
WHERE (tblEntity.Cat_ID = prmCatID) And (tblDocuments.Document In (SELECT
tblDocuments.Document FROM tblDocuments GROUP BY tblDocuments.Document
HAVING Count(*)>=1));
This will get me documents appearing qryA and also in qryB (what we DON'T
want to delete)
[qryC]
SELECT DISTINCT qryB.Doc_ID
FROM qryB INNER JOIN qryA ON qryB.Document = qryA.Document;
So the Recordset I need is all records in qryB that do not appear in qryC.
But this is pathetically slow:
SELECT qryB.Doc_ID FROM qryB WHERE qryB.Doc_ID Not In (SELECT qryC.Doc_ID
FROM qryC)
The Not operator appears to really slows things down.
Anyway, I'm kind of stumped on this one so I appreciate any suggestions you
care to offer.