Need help with a lookup please

Buns1976

Board Regular
Joined
Feb 11, 2019
Messages
194
Office Version
  1. 365
Platform
  1. Windows
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

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
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
 
Upvote 0
Hi HTH,

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

Thanks!
 
Upvote 0
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
 
Upvote 0
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:
Upvote 0
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!
 
Upvote 0
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).
 
Upvote 0
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:
Upvote 0
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:
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,213,520
Messages
6,114,101
Members
448,548
Latest member
harryls

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