DLookup with multiple criteria

Nadine67

Board Regular
Joined
May 27, 2015
Messages
225
Hello and thank you in advance for any attention this post may receive.

Table = tblUnit
Result table column = "Make"
Criteria table columns = "ID_Unit" and "Date_Commissioned"
Criteria = ID_Unit = txtUnit value and Max(Date_Commissioned)

My desired result is to return (in txtMake) the Make of the unit in txtUnit, that has the latest commissioned date from tblUnit.
There are multiples of the same unit number in ID_Unit with various commission dates.

This is my txtMake Control Source:
=IIf(IsNull([txtUnit]),"",DLookUp("Make","tblUnit","([ID_Unit]=" & [txtUnit] And [Date_Commissioned]=" & Max([Date_Commissioned]))"))

It is returning the first occurrence where [ID_Unit] = [txtUnit] only.

Thank you again!
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Does using =Max([Date_Commissioned]) as the control source for a text box give you the maximum date you expect to use for the lookup?

If so then try:
Code:
=IIf(IsNull([txtUnit]),"",DLookUp("Make","tblUnit","([ID_Unit]=" & [txtUnit] And [Date_Commissioned]=#" & Max([Date_Commissioned])"#))"

if you are looking to use the latest date from the table then you would need to combine the Dlookup with a Dmax.
 
Last edited:
Upvote 0
That would probably be as you have not concatenated the remaining criteria I believe?
Should be more like
Rich (BB code):
=IIf(IsNull([txtUnit]),"",DLookUp("Make","tblUnit","[ID_Unit]=" & [txtUnit] & " And [Date_Commissioned]=#" & Max([Date_Commissioned]) & "#""""))



Not too sure how many double quotes should be at the end, so try permutations. :D

HTH
 
Upvote 0
Nesting a DMax in a DLookup is also a way to go:

Code:
=IIf(IsNull([txtUnit]),"",DLookUp("Make","tblUnit","([ID_Unit]=" & [txtUnit] & " And [Date_Commissioned]=" & "#" & DMax([Date_Commissioned], [tblUnit], "ID_Unit=" & [txtUnit]) & "#" & ")"))

The above is long and ugly enough that I'd be searching for a way to decompose it (possibly create a custom function to handle the logic). Also you can put the calculation of Date_Commissioned into its own textbox for easier testing and writing of the function. Later (when all is working) then make that textbox for Date_Commissioned invisible.
Code:
'//Place in a hidden (not visible field) called txtMaxDateCommissioned:
=DMax([Date_Commissioned], [tblUnit], "ID_Unit=" & [txtUnit])

=IIf(
	IsNull([txtUnit]),"",
	DLookUp("Make","tblUnit","([ID_Unit]=" & [txtUnit] & " And [Date_Commissioned]=" & "#" & [txtMaxDateCommissioned] & "#" & ")"))
 
Upvote 0
**** SOLVE ****

Thank you once again stumac, welshgasman, and xenou!

You have done it gain, and I am very grateful.

Have a great day!
 
Upvote 0

Forum statistics

Threads
1,223,565
Messages
6,173,071
Members
452,500
Latest member
FrankSit

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top