Avatar of SteveL13
Flag for United States of America asked on

Trying to lookup last record

What is wrong with this code that I'm using in an oncurrent event of a form?

Me.txtLastTranxItem = DLast("[PartN]", "tblWarehouseTransTranx", "[PartN] = " & Forms![frmWHtransTranx]![cboPartN])

Microsoft Access

Avatar of undefined
Last Comment
Jeffrey Coachman

8/22/2022 - Mon

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Andy Marshall

The structure of the DLast call you've stated looks OK; are you sure that the value you want returned is PartN, when you are already specifying PartN in the criterion of the DLookup?  It's like you are looking up a value you already know (unless PartN is a similar value in a different table of course).

The only other thing I can think of off the top of my head is the value in the criterion "[PartN] = " & Forms![frmWHtransTranx]![cboPartN].  I presume from your syntax that the values in your combo box cboPartN are numbers as opposed to strings.  If you had string values, you'd need to enclose the criterion value in single quotes, i.e.
"[PartN] = '" & Forms![frmWHtransTranx]![cboPartN] & "'"

Jeffrey Coachman

DLast, (like most other aggregate functions), will depend on how the table is sorted.
Remember a user may be able to easily change how a table is sorted

This is why it may be better to create a query (of the same table) that is sorted the way you want.
Then DLast *should* return the correct value.

However to be more certain, use something like what mbizup posted.

...or create a date/time stamp field with a default value of: =Now()
But even here you may have to make a distinction between Last "added" record, and last "edited" record.

Your help has saved me hundreds of hours of internet surfing.