Find Last date & value, before today with empty dates (1 Viewer)

MackMan

Registered User.
Local time
Today, 17:21
Joined
Nov 25, 2014
Messages
174
Hi all.

I'm trying to lookup a value from another query with dates running in ASC order from qryLookup

TransDate Lookup Value
----------- ---------------
20/07/2018 … zz1
20/07/2018 … xz1
21/07/2018 ... xy1
21/07/2018 ... xy2
23/07/2018 ... xy3
23/07/2018 ... xy4
23/07/2018 ... xy5 < -----------
25/07/2018 ... xy6
25/07/2018 … xy7

What I want to do is lookup the last record before today (being 25/07/2018)
and get the value xy5

I've tried
Code:
dlookup("lookupValue","qryLookup","TransDate < " & dlast("transdate","qryLookup","TransDate < #" & date & "#"))

This returns a null

if I check separately
Code:
dlast("transdate","qryLookup","TransDate < #" & date() & "#")
then I get a return of the 20th with 2 dates inbetween ( which is not the last date before today)

Ive tried using an aggregate query with the following for checking the last date before today (which also returns null
Code:
SELECT Last(qryLookup.TransDate) AS LastOfTransDate
FROM qryLookup
HAVING (((Last(qryLookup
.TransDate))<Date()));

Dmax also returns the same

Am I doing something so obviously wrong? or is there a more difficult solution I've not yet come across (so I don't know it)?

As always, your help is much appreciated.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 17:21
Joined
Feb 19, 2013
Messages
16,627
A variation of your last approach is

Code:
SELECT TOP 1 TransDate
FROM qryLookup
WHERE TransDate<Date()
ORDER BY TransDate Desc
 

MackMan

Registered User.
Local time
Today, 17:21
Joined
Nov 25, 2014
Messages
174
Thanks Minty for your link. Now I understand my error and it's easy to see why.
And thanks CJ London once again... does exactly as I need it to!
You guys are awesome! Total respect in regards to your knowledge on all this stuff.

It's still a minefield for me, but I'm getting there.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 00:21
Joined
May 7, 2009
Messages
19,247
You set the Sort Order of qryLookup to TransDate Desc, [Lookup Value] Desc.

or create another query with the above sort order and use it for your dlookup.
 

Users who are viewing this thread

Top Bottom