View Full Version : Help with finding "newest" record on a subform


ejstefl
01-28-2002, 04:54 PM
Hello everyone,

I hope you can help me. I work in a mutual funds operations environment, and I have created a database to track our balance of an account to the fund's balance of that account.

Here is how the database is set up: One table for accounts, which has an accountID, and one table for balances, which has an accountID and a balanceID. I have set up a form for accounts with a subform for balances. That way, I can see a running history of balances.

For reporting purposes, I need to use the current balance (I have a date field to show the date it was balanced). The problem is that I don't know how to do this, aside from having a field labeled "flag" where you put an 'x' by the most recent record. In the balances table, there may be 5 records that have the same accountID, but different dates. So I need to select the most recent one of those 5, for each accountID. I can do this with a flag field, but I would also like to do a report showing how long it takes to balance something, and I can't make that work with the flag. Any ideas on how I can do this? Is my design flawed?

Sorry for the long post, any help would be greatly appreciated. Thanks!

Eric

Rich
01-28-2002, 11:05 PM
Either totals query for the report with the date as Max or order the date field desc.

ejstefl
01-29-2002, 04:58 AM
The reason I don't think that will work is because there will be, as an example, 20 records in the balances table. Of those 20, there may be 5 records each for 4 accountID's. If I use a total query with the date as max, I will only get the most recent entry for one accountID.

Unless I misunderstood what you were saying, I don't think this would work... Any other ideas? Thanks for the help so far!

Rich
01-29-2002, 06:22 AM
You'll get the Max date for each account number add GroupBy to the account ID, you'll need a second query to get the correct amount though. Are you trying to do this at form level?

[This message has been edited by Rich (edited 01-29-2002).]

ejstefl
01-29-2002, 10:38 AM
OK, that worked brilliantly. Thanks! Now, what if I want to find the latest two records per accountID? How could I do that? Again, thanks for the help!

Oh, and I'm not trying to do it on a form level. The form is fine, I can just sort the subform by date. Its for making reports, etc.

[This message has been edited by ejstefl (edited 01-29-2002).]

Rich
01-29-2002, 10:59 AM
I haven't tried this so it's just theory but you might try the Top values property of the query.
HTH

ejstefl
01-29-2002, 11:15 AM
That only gives me the top two AccountID's...