Search code examples
ms-accessvbadatasetms-access-2007setvalue

Setting parent record field value based on Maximum Child record field value


I am working with MS Access 2007.

I have a table called [tblDonors] linked to child records in a table called [tblReceipts].
I am trying to set the value of a new Boolean(y/n) field [tblDonors].[blInactive]. I would like to run a script which goes through every record in [tblDonors] and set the [blInactive] field to "True" if the most recent receipt year([tblReceipts].[Date]) is older than year(now())-2.

Here is my query:

SELECT tblDonors.ID, Year([tblReceipts].[Date]) AS [Year], tblDonors.Active
FROM tblDonors
LEFT JOIN tblReceipts ON tblDonors.ID = tblReceipts.ID
WHERE (((Year([tblReceipts].[Date]))>=Year(Now())-2))
ORDER BY Donors.ID;

This query shows the parent record multiple times if the donor has donated more than once within the past two years, which creates duplicate records in a report.
How can I go through each record of this query to set tblDonors.Active to TRUE using VBA? The following helps demonstrate the gist of what I am attempting to accomplish.

    Function fnActivity()                     'locates active donors

        For each record in recordset          'Go through each record in the query...
            record.[Active].Value = True      '...and flag as active member.
        Next record

    End Function

Solution

  • It seems to me that you could accomplish that by setting [blInactive] to Yes for everyone...

    UPDATE tblDonors SET blInactive = Yes
    

    ...and then update the current donors like this:

    UPDATE tblDonors SET blInactive = No
    WHERE ID IN 
        (
            SELECT ID FROM tblReceipts 
            WHERE Year([tblReceipts].[Date]) >= (Year(Now()) - 2)
        )