1

In my database, I have two tables I am working with that are related. The first table is the Offer Information table (OfferList). The second table is a journal more or less of the progress of the offer (Ledger). The OfferID is referenced in the Ledger Entry. However, since there are multiple entries for the same offer in the Ledger, I have an EntryID that references a specific entry. Both forms have a field called status. The status of each Ledger.Entry does not change but the status of the Offer does change based on the last entry. I need Access to automatically update ONLY the OfferID.Status based upon the Ledger.Entry I am currently working with in my form. I.E. I use a form for Ledger.Entry that has HLID, Date, Status, Notes. Once I am done with the Ledger.Entry and save/update the Ledger table, I want the Offer.ID.Status to update to whatever I put into Ledger.Entry.Status on my Ledger form. The catch is I ONLY want to update the record in Offers that I am currently working with. I suspect that I will be using VBA to accomplish my goal. HL# is linked to HLID.

E.G.

OfferList

HL#     Status    Agent    Buyer
ID01    Presented Jim W.   Roger R.
ID02    Failed    Jim W.   Wilma S.
ID03    Accepted  Jeff H.  Roger R.
ID04    Accepted  Andy K.  Peter P.

Ledger

EntryID  HLID  Date      Status
01       ID03  04/05/13  Presented
02       ID03  04/07/13  Accepted
03       ID02  04/04/13  Presented
04       ID02  04/04/13  Failed
05       ID03  05/05/13  Closed

When I enter that last row into Ledger, I want the Status in OfferList for ID03 to update to Closed.

4

1 回答 1

2

I strongly agree with Andy's statement, "The Status should not be repeated in the Offers table, just determined when necessary by reading the most recent Status in the Ledger table based on the Date-field."

Here is my version of the Ledger table. I stored time of day in addition to the date in the lDate field. And the table design includes a unique index on the combination of HLID and lDate. That makes it possible to determine which row is the latest for HLID = ID02.

EntryID HLID lDate               Status
      1 ID03 4/5/2013 9:00:00 AM Presented
      2 ID03 4/7/2013 9:00:00 AM Accepted
      3 ID02 4/4/2013 9:00:00 AM Presented
      4 ID02 4/4/2013 3:00:00 PM Failed
      5 ID03 5/5/2013 9:00:00 AM Closed

In the OffersList table, I renamed HL# to HLID and discarded the Status field. With those table changes, the query below returns this output in Access 2007.

HLID Status
ID01
ID02 Failed
ID03 Closed
ID04
SELECT o.HLID, sub2.Status
FROM
    OfferList AS o
    LEFT JOIN
    (
        SELECT l.HLID, l.Status
        FROM
            (
                SELECT HLID, Max(lDate) AS MaxOflDate
                FROM Ledger
                GROUP BY HLID
            ) AS sub1
            INNER JOIN Ledger AS l
            ON
                    (sub1.MaxOflDate = l.lDate)
                AND (sub1.HLID = l.HLID)
    ) AS sub2
    ON o.HLID = sub2.HLID;
于 2013-08-02T21:11:17.810 回答