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!
 
I must admit, I was wondering what the "3" was for, until I assumed it was just a placeholder for Me.BookNumber ?
 
Upvote 0

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
I dont' have your database so I used a very small table with a few records in it for testing. You will need to adapt for your actual situation (whatever that is - I assume you have a form or report based on what I see I really don't know). I couldn't use "BookNumber = [BookNumber]" because there is nothing in my database that that name would point to. so I used "BookNumber = 3".
 
Upvote 0
Cross-posted here: http://www.accessforums.net/showthread.php?t=76035

While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules). This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.
 
Upvote 0
Hi,

The "syntax error in date" is the error message that was displayed when I tried to RUN it in a query as you suggested!

Code:
[COLOR=#333333]select Nz(DLookUp("[POSSOLD]", "[LotterySales]", "BOOKNUMBER=" & "3" & " AND InventoryDate=#" & DMax("[InventoryDate]", "[LotterySales]", "BOOKNUMBER=" & "3") & "#"),0) as Result[/COLOR]

Data Types are Date/Time for InventoryDate and Number for BOOKNUMBER & POSSOLD!

Thanks!
 
Upvote 0
Xenou,

In the table and form below you can see what is happening. On 3/10/2019 POSSOLD in the Table and POSold in the form match.
On 3/11/2019 and 3/12/2019 they do not because the date is not part of the array.

Also this is a trimmed down version. There will be approximately 60 BOOKNUMBERs we would lookup and return POSSOLD each day!


I attached a Dropbox link to the database below.

https://www.dropbox.com/s/qocxjdd20n83626/LOTTERY1.accdb?dl=0


TABLELotterySales
POSDATEBOOKNUMBERPOSSOLD
3/10/20197022162
3/11/20197022163
3/12/20197022164
FORMInventory
InventoryDateBookNumberPOSSold
3/10/20197022162
3/11/20197022162
3/12/20197022162

<tbody>
</tbody>
 
Last edited:
Upvote 0
I'm not sure what you mean when you say the date is not part of the array. I see dates in your data.

Are you adding additional criteria to your original question? Maybe you need to restate the question. Also there is no explanation of why the syntax fails yet. If you have dates and numbers there should be no syntax problem (and as I said it works fine for me).

I don't quite understand the data you posted. Why did you post something that looks like a table that is called "Form"? It's not really clear how you are using this formula. I cannot download dropbox files at work. My formula works on one table. I don't really know what is going on with this Form + Table setup now.
 
Last edited:
Upvote 0
Hi,

The form I posted above in in Datasheet view as example where the number from POSSALES should go in the form by DATE. I made an error in my original post which said
The "LotterySales" table also has an "InventoryDate" field.
. The LotterySales table DOES NOT have a field named InventoryDate, the form does. The DATE field in the LotterySales table is POSDATE. Sorry about that!

I modified the code but still do not have it correct. When I run it in the test query, it shows error which highlights "as". If I could figure out how to post a screenshot it would be helpful.


Code:
select Nz(DLookUp("[POSSOLD]", "[LotterySales]", "BOOKNUMBER=" & " AND POSDATE=#" & DMax("[POSDATE]", "[LotterySales]", "BOOKNUMBER=" & "#"),0) as Result




Thank you!
 
Last edited:
Upvote 0
The last formula you posted would be invalid because you don't have BookNumber being equal to anything.

select Nz(DLookUp("[POSSOLD]", "[LotterySales]", "BOOKNUMBER=" & " AND POSDATE=#" & DMax("[POSDATE]", "[LotterySales]", "BOOKNUMBER=" & "#"),0) as Result

That's basically a criteria that says "booknumber = "

And as you would immediately know in any other context, you can't have a query that is "select * from LotterySales where BookNumber = " and not have a booknumber.

You should practice with other DMax and DLookup functions so you can get the hang of it. You should be able to get a result for the DMax function separately and independently (that's the inner DMax function). Then get a result for the outer DLookup function using a hard coded date. Then (and only then) put them together with the DMax nested inside the DLookup.

Probably you should also avoid using nested D- functions too. I do that about once every 3 years, and then only if I have too. It's not worth the pain.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,868
Members
449,053
Latest member
Mesh

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