Need help with a lookup please

Buns1976

Board Regular
Joined
Feb 11, 2019
Messages
149
Hi Everyone,
I need some help with the lookup I attached below. The "LotterySales" table also has an "InventoryDate" field.

Since the "LotterySales" table will have numerous entries with the same "BOOKNUMBER", I need to
look at the most recent "InventoryDate" field in the "LotterySales" table and return "POSSOLD" if it exists
on the most recent "InventoryDate". If not then return "0".

I hope that makes sense?

Code:
DLookUp("POSSOLD","LotterySales","BOOKNUMBER=" & [BookNumber])

Thank you!
 

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.

welshgasman

Well-known Member
Joined
May 25, 2013
Messages
921
You could use DMAX to get the InventoryDate for the BookNumber, then use both to get POSOLD and use NZ to make it zero if none found.

I'd probably write a dedicated function for this and pass in the booknumber.?

HTH
 

Buns1976

Board Regular
Joined
Feb 11, 2019
Messages
149
Hi HTH,

Can you show me an example of what that might look like please?

Thanks!
 

xenou

MrExcel MVP
Joined
Mar 2, 2007
Messages
16,836
Office Version
  1. 2019
Platform
  1. Windows
Only because I love nested D- functions:
Code:
select Nz(DLookUp("[POSSOLD]", "[LotterySales]", "BOOKNUMBER=" & "3" & " AND InventoryDate=#" & DMax("[InventoryDate]", "[LotterySales]", "BOOKNUMBER=" & "3") & "#"),0) as Result
 

Buns1976

Board Regular
Joined
Feb 11, 2019
Messages
149

ADVERTISEMENT

Hi xenou,

I'm getting a compile error "Expected:Case" on Nz in AfterUpdate with this? The AfterUpdate fires when a field named Scan is populated
with a handheld scanner

Thank you!
 
Last edited:

Buns1976

Board Regular
Joined
Feb 11, 2019
Messages
149
Xenou,

Scratch that last post. I will run this in the field. Don't know what I was thinking earlier?

Anyway I am getting an error "The syntax of the subquery is incorrect" on "select"

Thanks


Hi xenou,

I'm getting a compile error "Expected:Case" on Nz in AfterUpdate with this? The AfterUpdate fires when a field named Scan is populated
with a handheld scanner

Thank you!
 

xenou

MrExcel MVP
Joined
Mar 2, 2007
Messages
16,836
Office Version
  1. 2019
Platform
  1. Windows

ADVERTISEMENT

I don't know why you would get a syntax error. I tested it with no problems (open a query create window, put it in sql view, paste in the query text, and run it).
 

Buns1976

Board Regular
Joined
Feb 11, 2019
Messages
149
Hi Xenou,

I get Syntax error in date in query expression'BOOKNUMBER=3 AND InventoryDate=#'
If it makes any difference BOOKNUMBER is a number format?

Thanks
 
Last edited:

xenou

MrExcel MVP
Joined
Mar 2, 2007
Messages
16,836
Office Version
  1. 2019
Platform
  1. Windows
You shouldn't be using a hardcoded 3 anyway. Your query has a parameter for BookNumber (maybe from a form?)

Also for what it's worth formats are not important to me in a case like this. I only care about datatypes. Is something a number, text, or a date? That's it. Doesn't matter how it is formatted (because formatting is simply how it is displayed to the end user).
 
Last edited:

xenou

MrExcel MVP
Joined
Mar 2, 2007
Messages
16,836
Office Version
  1. 2019
Platform
  1. Windows
Also why do you say that you get a syntax error in "date":

I get Syntax error in date in query expression'BOOKNUMBER=3 AND InventoryDate=#'

What kind of date data are you working with?

It's syntax so you should be doing some trial and error and trying some experiments to working out the correct syntax. Syntax problems are usually just a matter of working out the quotes, or brackets, or the way you are concatenating things.

Remember that the last parameter of the D- functions should look like a SQL where clause without the word where. But since you are building a string with a lot of concatenation operations, it is the resulting string expression that you really care about, after all the variables are substituted and all the parts are smashed together by the concatenation operations. This is also why I was being sarcastic about loving nested D- functions - it is actually quite unpleasant to deal with so much syntax in one expression, and requires thinking about datatypes, string operations, variables, and the proper way to handle string literals in Access, as well as keeping track of your actual nested functions.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,628
Messages
5,838,447
Members
430,549
Latest member
jayjay2022

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
Top