Avatar of SteveL13
SteveL13Flag 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])

--Steve
Microsoft Access

Avatar of undefined
Last Comment
Jeffrey Coachman
ASKER CERTIFIED SOLUTION
Avatar of mbizup
mbizup
Flag of Kazakhstan image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Andy Marshall
Andy Marshall
Flag of United Kingdom of Great Britain and Northern Ireland image

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] & "'"

HTH
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.

JeffCoachman
Microsoft Access
Microsoft Access

Microsoft Access is a rapid application development (RAD) relational database tool. Access can be used for both desktop and web-based applications, and uses VBA (Visual Basic for Applications) as its coding language.

226K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo