select last record in a one to many relationship

I need to select the last record from the child table in a one to many relationship.

I have found some solutions in Experts Exchange but have not been able to translate them to my database.

My simple test database has two tables

tblIncident has two fields, IncidentID (PK, AutoNumber) and LastName (Text)
tblStatus has three fields, StatusID (PK, AutoNumber), IncidentID (Foreign Key, Number), Comments (Memo)

tblStatus may have many records related to one IncidentID in tblIncident.

I want to SELECT only the last record from tblStatus per IncidentID based on the highest number StatusID

Of course I can add a date field to tblStatus if that is necessary
Wayne MarkelOwnerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Create a query that selects only the StatusID and IncidentID
Select Max(StatusID) as MaxStatusID, IncidentID
From tblStatus
Group By IncidentID;

This will get you the highest StatusID for each IncidentID.  Remember that "last" has no meaning in a relational database.  If you are positive that status values get inserted in order then this is sufficient.  However, if they might get entered out of order, you will need to add a date field and use that.

Then in your main query, include this query between tblIncident and tblStatus.  This query will NOT be updateable because of the aggregation in the totals query.

Alternatively, you can use a subselect to find the maximum value for statusID.  This method may or may not result in an updateable query.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rey Obrero (Capricorn1)Commented:
here is how to write the query

SELECT tblIncident.IncidentID, tblIncident.LastName,S4.Comment,S4.StatusID
FROM tblIncident
(SELECT S.StatusID, S.IncidentID, S.Comment
FROM tblStatus AS S
Inner Join
(SELECT Max(S2.StatusID) AS MaxOfStatusID, S2.IncidentID
FROM tblStatus AS S2
GROUP BY S2.IncidentID) As S3
ON S.IncidentID=S3.IncidentID And  S.StatusID= S3.MaxOfStatusID) As S4
On tblIncident.IncidentID=S4.IncidentID
Wayne MarkelOwnerAuthor Commented:
Excellent.  The solution has been tested in my production database.
Rey Obrero (Capricorn1)Commented:
Wayne MarkelOwnerAuthor Commented:
The solution from Rey Obrero also worked.  Thanks to both who contributed.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.