Hi Jeff,
Before I get too involved with this, there's one obvious problem with what
you've got at present, and also some "good practice" issues, which I'll get
out of the way first.
Avoid use of spaces in table and field name. You can always alias a
non-space field name to a string with spaces in a query if you need it for
display in a form/report (or you can simply change the default fieldname in
the label control). You got several examples of this, in both table and
field names. In particular, the [Baby's DOB] is awful; I'm surprised Access
even let you use this one - I would have though ' was an illegal character
in a field name, but I guess it's not.
The HMB Test table (better name would be tblHMDTests - using a naming
convention helps when reading something later; eg. in a comple query, you
can immediately see if an underlying table/query is a table or a query if
you name your tables tblName and your queries qryName) contains a field
named Date. That's a reserved word in Access (it's own function to return
the current date - which you're also using in your query); reserved words
should never be used as field names; there'll come a time when Access gets
confused and something will break (perhaps not obviously). There's a
comprehensive list of them at Allen Browne's website:
http://allenbrowne.com/AppIssueBadWord.html And he's also got a little
utility to check for these (and some other issues) in existing databases:
http://allenbrowne.com/AppIssueChecker.html
So much for the good practice things; here's the obvious problem:
You are using [DuePlus90] in an expression in an Iif statement; however,
[DuePlus90] is an alias to a calculated field in the query. You cannot use
an alias in the query which defines it - you must use the calculated
expression. So, your
IIf([Pregnancy Outcomes].[Baby's DOB] Between [HMB Test].Date And
[DuePlus90],[Pregnancy Outcomes].[Baby's DOB],Null)
would need to be:
IIf([Pregnancy Outcomes].[Baby's DOB] Between [HMB Test].Date And
DateAdd("d",120,[HMB Test]!EDC),[Pregnancy Outcomes].[Baby's DOB],Null)
Maybe this will fix the issue; could you try it and let me know before I
spend any more effort on this.
I'm also a little concerned that there is no direct link between records for
the same DemogID in the HMB Test and Pregnancy Outcomes tables; does there
need to be a link between a Pregnancy Outcome record for a particular
DemogID and the record(s) for various HMB Test - which could, presumably,
relate to more than one pregnancy at different times. Or are you satisified
that your conditions using dates is sufficient to prevent this. Note that
without a link - if there should be one - your query may return multiple
copies of certain records.
Rob
Jeff said:
Thanks so much.
There are 3 tables involved. One parent table [Demographic] which
represents the client. The key is the DemogID (short for demographic
identifier). There are 2 child tables. The first is the pregnancy test
[HMB Test] which has the date of the pregnancy test, the results of
the test, and the expected due date (EDC). The other child is the
pregnancy outcome, which contains the name, date of birth, etc about
the baby born. All 3 tables contain the DemogID field and are tied
together (1 to many relationship with referential integrity.)
In my example
Demographic information
Jane Doe
pregnancy tests [HMB Test]
02/13/07 -- positive
03/08/08 -- positive
Pregnancy Outcomes
12/5/07 -- should match 2/13/07 test
10/05/04 -- should not match any pregnancy test (child prior to
database.)
I hope that's enough. The SQL is as follows:
SELECT Demographics.DemogID, Demographics.[Active Van Client],
Demographics.[Van Start Date], [HMB Test].LMP, DateAdd("d",90,Date())
AS Plus90, DateAdd("d",120,[HMB Test]!EDC) AS DuePlus90, Demographics.
[Van Neighborhood Served], Demographics.FirstName,
Demographics.LastName, Demographics.MiddleName, [HMB Test].Date, [HMB
Test].EDC, [HMB Test].[Test Results], IIf([Pregnancy Outcomes].[Baby's
DOB] Between [HMB Test].Date And [DuePlus90],[Pregnancy Outcomes].
[Baby's DOB],Null) AS [Pink Birth Date], IIf([Pregnancy Outcomes].
[Deceased Date] Between [HMB Test].Date And [HMB Test].EDC,[Pregnancy
Outcomes].[Deceased Date],Null) AS [Pink Deceased Date],
IIf([Pregnancy Outcomes].[Lost to Followup Date] Between [HMB
Test].Date And [HMB Test].EDC,[Pregnancy Outcomes].[Lost to Followup
Date],Null) AS [Pink Lost to Followup Date]
FROM (Demographics INNER JOIN [HMB Test] ON Demographics.DemogID =
[HMB Test].DemogID) LEFT JOIN [Pregnancy Outcomes] ON
Demographics.DemogID = [Pregnancy Outcomes].DemogID
WHERE (((Demographics.[Active Van Client])=-1) AND (([HMB Test].[Test
Results])="Positive" Or ([HMB Test].[Test Results])="Already
Pregnant"))
ORDER BY Demographics.LastName DESC;
Thanks a lot.
Jeff