I'm having a challenge understanding the DLookup function and applying it to a Query Expression.
Does anyone have an DB example that you can share?
On line examples generally associate it with a form. I'm interested in using DLookup within a query.
Regards,
John
Expression DLookup
-
- GoldLounger
- Posts: 2628
- Joined: 26 Jan 2010, 16:31
- Location: Southern California
Expression DLookup
Regards,
John
John
-
- gamma jay
- Posts: 25455
- Joined: 17 Mar 2010, 17:33
- Location: Cape Town
Re: Expression DLookup
Try these two web samples...
-- http://www.techonthenet.com/access/func ... lookup.php
-- http://office.microsoft.com/en-za/acces ... 28825.aspx
-- http://www.techonthenet.com/access/func ... lookup.php
-- http://office.microsoft.com/en-za/acces ... 28825.aspx
Regards,
Rudi
If your absence does not affect them, your presence didn't matter.
Rudi
If your absence does not affect them, your presence didn't matter.
-
- 5StarLounger
- Posts: 1148
- Joined: 08 Feb 2010, 21:27
Re: Expression DLookup
If you use any of the aggregate functions in a query it will slow the query substantially.
-
- GoldLounger
- Posts: 2628
- Joined: 26 Jan 2010, 16:31
- Location: Southern California
Re: Expression DLookup
Rudi,
Thanks for the links but I am still having a challenges.
I have attached a sample Access DB. The expected query results should be Thanks for taking a look,
John
Thanks for the links but I am still having a challenges.
I have attached a sample Access DB. The expected query results should be Thanks for taking a look,
John
You do not have the required permissions to view the files attached to this post.
Regards,
John
John
-
- 5StarLounger
- Posts: 1148
- Joined: 08 Feb 2010, 21:27
Re: Expression DLookup
What you need to do is to join the tables in a query based on Item#.
See attached database. I have added a new query called Query1.
Is that what you want?
See attached database. I have added a new query called Query1.
Is that what you want?
You do not have the required permissions to view the files attached to this post.
-
- GoldLounger
- Posts: 2628
- Joined: 26 Jan 2010, 16:31
- Location: Southern California
Re: Expression DLookup
Pat,
I'm interested in a DLookup solution but thanks for providing a suggestion. From what I have read DLookup should work.
I must be missing something with the DLookup solution.
Regards,
John
I'm interested in a DLookup solution but thanks for providing a suggestion. From what I have read DLookup should work.
I must be missing something with the DLookup solution.
Regards,
John
Regards,
John
John
-
- 5StarLounger
- Posts: 1148
- Joined: 08 Feb 2010, 21:27
Re: Expression DLookup
For a start your query will provide a Cartesian product of all records in A with all records in B? So you should join the tables at least to stop that.
The query you want will possibly look like this:
SELECT DISTINCT A.Account, A.[ITEM#], DLookUp("COLOR","tbl_Item","[ITEM#]=" & A.[ITEM#]) AS Color
FROM tbl_Main AS A INNER JOIN tbl_Item AS B ON A.[ITEM#] = B.[ITEM#]
Why do you wish to use DLookup??
The query you want will possibly look like this:
SELECT DISTINCT A.Account, A.[ITEM#], DLookUp("COLOR","tbl_Item","[ITEM#]=" & A.[ITEM#]) AS Color
FROM tbl_Main AS A INNER JOIN tbl_Item AS B ON A.[ITEM#] = B.[ITEM#]
Why do you wish to use DLookup??
-
- GoldLounger
- Posts: 2628
- Joined: 26 Jan 2010, 16:31
- Location: Southern California
Re: Expression DLookup
Pat,
The actual table I will be using has a number Items in tbl_Main that are not in tbl_Item plus a good number of other fields. The query results include a variety of fields one of which is the ITEM#. If I use a join between the two tables I will not be able to query all "Item#s"; just those which are joined i.e. Inner, Right or Left joins.
Since Access has the DLookup function I believe it works in the same manner as Excel's Index and Match functionality. I have reviewed the DLookup examples but can not get it to work therefore I posted the example DB in my Post=131076 inquiring about DLookup.
I am aware of the Cartesian product of all records but will reduce the query results by applying a WHERE statement between two dates; from/to dates.
Regards,
John
The actual table I will be using has a number Items in tbl_Main that are not in tbl_Item plus a good number of other fields. The query results include a variety of fields one of which is the ITEM#. If I use a join between the two tables I will not be able to query all "Item#s"; just those which are joined i.e. Inner, Right or Left joins.
Since Access has the DLookup function I believe it works in the same manner as Excel's Index and Match functionality. I have reviewed the DLookup examples but can not get it to work therefore I posted the example DB in my Post=131076 inquiring about DLookup.
I am aware of the Cartesian product of all records but will reduce the query results by applying a WHERE statement between two dates; from/to dates.
Regards,
John
Regards,
John
John
-
- Administrator
- Posts: 78391
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Expression DLookup
The solution provided by Rudi is MUCH more efficient than using DLookup. If you still want to use DLookup, however, you should NOT include tbl_Item in the query - you us DLookup to look up a value in this table, so including it in the query too is redundant.
As Pat has indicated, the third argument to DLookup is a string (just like the other two arguments). The SQL for the query looks like this:
SELECT DISTINCT Account, [ITEM#], DLookUp("COLOR","tbl_Item","[ITEM#]=" & [ITEM#]) AS Color
FROM tbl_Main;
A screenshot of design view:
And of the result:
As Pat has indicated, the third argument to DLookup is a string (just like the other two arguments). The SQL for the query looks like this:
SELECT DISTINCT Account, [ITEM#], DLookUp("COLOR","tbl_Item","[ITEM#]=" & [ITEM#]) AS Color
FROM tbl_Main;
A screenshot of design view:
And of the result:
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans
Hans
-
- GoldLounger
- Posts: 2628
- Joined: 26 Jan 2010, 16:31
- Location: Southern California
Re: Expression DLookup
Hans,
Thank you for the detailed explanation. I now understand how the DLookup function works. One additional thing I did learn through this exercise is that the field data type comes into play. The expression you provided is based on the field data type being numeric.
The expressions changes slightly when the field data type is text. Color: DLookUp("COLOR","tbl_Item","[ITEM#]='" & [item#] & '"")
Rudi/Pat - I appreciate your contributions as well.
Regards,
John
Thank you for the detailed explanation. I now understand how the DLookup function works. One additional thing I did learn through this exercise is that the field data type comes into play. The expression you provided is based on the field data type being numeric.
The expressions changes slightly when the field data type is text. Color: DLookUp("COLOR","tbl_Item","[ITEM#]='" & [item#] & '"")
Rudi/Pat - I appreciate your contributions as well.
Regards,
John
Regards,
John
John
-
- Administrator
- Posts: 78391
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Expression DLookup
That is correct. It's also different for date fields:jstevens wrote:The expressions changes slightly when the field data type is text.
DLookup("SomeField", "MyTable", "MyDate=#" & Format([MyDate], "yyyy-mm-dd") & "#")
The date value that we compare to must be enclosed in # characters, and we must either use US date format mm/dd/yyyy, or the international unambiguous format yyyy-mm-dd.
Best wishes,
Hans
Hans