Dlookup Syntax Q from NewBie

moradwan

New Member
Joined
Jul 24, 2009
Messages
40
Hello,
I am an excel user and new to access. I would like to use the dlookup function in an SQL query.

I have created a simple database made up of two tables, lookup_tbl and Data_tbl, to write a practice SQL code using DLookup.

My lookup_tbl is a two column table shown below:

<table bgcolor="#ffffff" border="1" cellspacing="0"><caption>Lookup_tbl</caption> <thead> <tr> <th bordercolor="#000000" bgcolor="#c0c0c0">lookup_value</th> <th bordercolor="#000000" bgcolor="#c0c0c0">return_value</th> </tr> </thead> <tbody> <tr valign="TOP"> <td bordercolor="#c0c0c0">lookup_1</td> <td bordercolor="#c0c0c0">return_1</td> </tr> <tr valign="TOP"> <td bordercolor="#c0c0c0">lookup_2</td> <td bordercolor="#c0c0c0">return_2</td> </tr> <tr valign="TOP"> <td bordercolor="#c0c0c0">lookup_3</td> <td bordercolor="#c0c0c0">return_3</td> </tr> </tbody> <tfoot></tfoot> </table>

My Data_tbl is shown below:
<table bgcolor="#ffffff" border="1" cellspacing="0"><caption>Data_tbl</caption> <thead> <tr> <th bordercolor="#000000" bgcolor="#c0c0c0">col_A</th> </tr> </thead> <tbody> <tr valign="TOP"> <td bordercolor="#c0c0c0">lookup_1</td> </tr> <tr valign="TOP"> <td bordercolor="#c0c0c0">lookup_2</td> </tr> <tr valign="TOP"> <td bordercolor="#c0c0c0">lookup_1</td> </tr> <tr valign="TOP"> <td bordercolor="#c0c0c0">lookup_3</td> </tr> <tr valign="TOP"> <td bordercolor="#c0c0c0">lookup_1</td> </tr> <tr valign="TOP"> <td bordercolor="#c0c0c0">lookup_1</td> </tr> <tr valign="TOP"> <td bordercolor="#c0c0c0">lookup_3</td> </tr> </tbody> <tfoot></tfoot> </table>
I would like to write a query in SQL that creates a new table which matches the values of Data_tbl using lookup_tbl and returning the corresponding values in lookup_tbl.

In other words I would like to end up with this:
<table bgcolor="#ffffff" border="1" cellspacing="0"><caption>Data_tbl</caption> <thead> <tr> <th bordercolor="#000000" bgcolor="#c0c0c0">col_A</th> <th bordercolor="#000000" bgcolor="#c0c0c0">col_B</th> </tr> </thead> <tbody> <tr valign="TOP"> <td bordercolor="#c0c0c0">lookup_1</td> <td bordercolor="#c0c0c0">return_1</td> </tr> <tr valign="TOP"> <td bordercolor="#c0c0c0">lookup_2</td> <td bordercolor="#c0c0c0">return_2</td> </tr> <tr valign="TOP"> <td bordercolor="#c0c0c0">lookup_1</td> <td bordercolor="#c0c0c0">return_1</td> </tr> <tr valign="TOP"> <td bordercolor="#c0c0c0">lookup_3</td> <td bordercolor="#c0c0c0">return_3</td> </tr> <tr valign="TOP"> <td bordercolor="#c0c0c0">lookup_1</td> <td bordercolor="#c0c0c0">return_1</td> </tr> <tr valign="TOP"> <td bordercolor="#c0c0c0">lookup_1</td> <td bordercolor="#c0c0c0">return_1</td> </tr> <tr valign="TOP"> <td bordercolor="#c0c0c0">lookup_3</td> <td bordercolor="#c0c0c0">return_3</td> </tr> </tbody> <tfoot></tfoot> </table>
So I created a query, lookup_test, and placed the following code in it.

SELECT Data_tbl.col_A, DLookUp("[return_value]","Lookup_tbl") AS col_B
FROM Data_tbl;

However, when I run this query I end up with this:

<table bgcolor="#ffffff" border="1" cellspacing="0"><caption>lookup_test</caption> <thead> <tr> <th bordercolor="#000000" bgcolor="#c0c0c0">col_A</th> <th bordercolor="#000000" bgcolor="#c0c0c0">col_B</th> </tr> </thead> <tbody> <tr valign="TOP"> <td bordercolor="#c0c0c0">lookup_1</td> <td bordercolor="#c0c0c0">return_1</td> </tr> <tr valign="TOP"> <td bordercolor="#c0c0c0">lookup_2</td> <td bordercolor="#c0c0c0">return_1</td> </tr> <tr valign="TOP"> <td bordercolor="#c0c0c0">lookup_1</td> <td bordercolor="#c0c0c0">return_1</td> </tr> <tr valign="TOP"> <td bordercolor="#c0c0c0">lookup_3</td> <td bordercolor="#c0c0c0">return_1</td> </tr> <tr valign="TOP"> <td bordercolor="#c0c0c0">lookup_1</td> <td bordercolor="#c0c0c0">return_1</td> </tr> <tr valign="TOP"> <td bordercolor="#c0c0c0">lookup_1</td> <td bordercolor="#c0c0c0">return_1</td> </tr> <tr valign="TOP"> <td bordercolor="#c0c0c0">lookup_3</td> <td bordercolor="#c0c0c0">return_1</td> </tr> </tbody> <tfoot></tfoot> </table>

I know that this is a rookie level question but I can not figure out the syntax for Dlookup. As you can see above it just looked up the first value and returned it for all of the values. Does anyone know the correct syntax?

Thanks in advance
Moradwan
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Why do you want to use DLookUp?

I justed create a simple query and got these results.

<TABLE border=1 cellSpacing=0 bgColor=#ffffff><CAPTION>Query1</CAPTION><THEAD><TR><TH bgColor=#c0c0c0 borderColor=#000000>col_A</TH><TH bgColor=#c0c0c0 borderColor=#000000>return_value</TH></TR></THEAD><TBODY><TR vAlign=top><TD borderColor=#c0c0c0>lookup_1</TD><TD borderColor=#c0c0c0>return_1</TD></TR><TR vAlign=top><TD borderColor=#c0c0c0>lookup_2</TD><TD borderColor=#c0c0c0>return_2</TD></TR><TR vAlign=top><TD borderColor=#c0c0c0>lookup_1</TD><TD borderColor=#c0c0c0>return_1</TD></TR><TR vAlign=top><TD borderColor=#c0c0c0>lookup_3</TD><TD borderColor=#c0c0c0>return_3</TD></TR><TR vAlign=top><TD borderColor=#c0c0c0>lookup_1</TD><TD borderColor=#c0c0c0>return_1</TD></TR><TR vAlign=top><TD borderColor=#c0c0c0>lookup_1</TD><TD borderColor=#c0c0c0>return_1</TD></TR><TR vAlign=top><TD borderColor=#c0c0c0>lookup_3</TD><TD borderColor=#c0c0c0>return_3</TD></TR></TBODY><TFOOT></TFOOT></TABLE>

Here's the SQL.

SELECT data_tbl.col_A, lookup_tbl.return_value
FROM data_tbl LEFT JOIN lookup_tbl ON data_tbl.col_A = lookup_tbl.lookup_value;
 
Upvote 0
Your DLookup syntax is off. You want to tell the function what you want: "[return_value]". You want to tell it where you want to get it from: "Lookup_tbl". You had those parts right. You also want to tell it how to choose what to get. So: lookup_value= something.

Code:
Dlookup("[return_value]","Lookup_tbl","[lookup_value]='" & Data_tbl.col_A & "'")  as col_B

Notice that I concatenated in the value rather than enclose the entire thing in quotes. Enclosing it in quotes will cause the function to look for a value in the field equal to "Data_tbl.col_A" whereas what I did forced it to evaluate what is in the field and dynamically dump it into the function for each field.

hth,

Rich
 
Upvote 0

Forum statistics

Threads
1,221,810
Messages
6,162,108
Members
451,743
Latest member
matt3388

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