Michael,
My test sql strings are listed below.
My final resul is to be:
I have a donor database which we track all donations and receipt the donors every month. We want to compare a previous time period to a current time period of all donors to find who gave donations previously and have not given during the current time period. I am doing this by comparing total donations for each donor for the time periods. The resultant should be anyone that has given x dollars during the first time period and has not given at least the same amount during the second time period.
I have 4 queries that I have set up where I will substitute variables for the dates and dollar amounts later.
query 1: to find total sum of donations > 300 by date range
time period one
qry1 = "SELECT Donor.[Donar ID], Sum(receipts.Donation) AS SumOfDonation FROM Donor INNER JOIN receipts ON Donor.[Donar ID] = receipts.[Donar id] WHERE (((receipts.Date) >= #1/1/2000# And (receipts.Date) <= #12/30/2000#)) GROUP BY Donor.[Donar ID] HAVING (((Sum(receipts.Donation))>300));"
query 2: to find total sum of donations > 300 by date range
current time period
qry2 = "SELECT Donor.[Donar ID], Sum(receipts.Donation) AS SumOfDonation FROM Donor INNER JOIN receipts ON Donor.[Donar ID] = receipts.[Donar id] WHERE (((receipts.Date) >= #1/1/2001# And (receipts.Date) <= #12/30/2001#)) GROUP BY Donor.[Donar ID] HAVING (((Sum(receipts.Donation))>300));"
query 3: find donors in time period one not found in
the current time period
qry3 = "SELECT qry1.[Donar ID], qry1.SumOfDonation FROM qry1 LEFT JOIN qry2 ON qry1.[Donar ID] = qry2.[Donar ID] WHERE (((qry2.[Donar ID]) Is Null));"
query 4: use qry3 to pick up donor information matching on [donor id]
qry4 = "SELECT Donor.[last name], Donor.[first name], Donor.address1, Donor.address2, Donor.city, Donor.state, Donor.zip, Donor.[Dear Name], qry3.SumOfDonation FROM Donor INNER JOIN qry3 ON Donor.[Donar ID] = qry3.[Donar ID];"
QRY4 is the final query that I would execute. I have this setup in query objects where I reference the predefined queries instead of qry(x) as shown in the sql strings above. When I run the qry4 as the query I receive the correct answer.
I know there has to be a way to simplify this. I started by building query objects and then built the next query to reference a query instead of a table. I was tring to replicate this in code, could be a bad idea....
I would really like to convert it all to VBA so I can control user input for all the dates and $ amounts used in qry1 and qry2.
I hope this is clearer, it's hard to tranlate your thoughts through typed words.
Thanks,
dave