Need help with a lookup please

Buns1976

Board Regular
Joined
Feb 11, 2019
Messages
138
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!
 

Some videos you may like

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

welshgasman

Well-known Member
Joined
May 25, 2013
Messages
842
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
138
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,695
Office Version
  1. 2013
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
138

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
138
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,695
Office Version
  1. 2013
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
138
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,695
Office Version
  1. 2013
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,695
Office Version
  1. 2013
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,109,522
Messages
5,529,330
Members
409,863
Latest member
stacy09
Top