IN THE SPOTLIGHT: MDE to MDB Conversion Service
(also supports: ACCDE to ACCDB, ADE to ADP, etc)
IN THE SPOTLIGHT: vbWatchdog
VBA error handling just got easier...
" vbWatchdog is off the chart. It solves a long standing problem of how to consolidate error handling into one global location and avoid repetitious code within applications. "
- Joe Anderson,
Microsoft Access MVP
Meet Shady, the vbWatchdog mascot watching over your VBA code →
(courtesy of Crystal Long, Microsoft Access MVP)
IN THE SPOTLIGHT: vbMAPI
An Outlook / MAPI code library for VBA, .NET and C# projects
Get emails out to your customers reliably, and without hassle, every single time.
Use vbMAPI alongside Microsoft Outlook to add professional emailing capabilities to your projects.
Provided by Allen Browne, email@example.com. Updated July 2006.
Sooner or later, you will need to retrieve a value stored in a table. If you regularly make write invoices to companies, you will have a Company table that contains all the company's details including a CompanyID field, and a Contract table that stores just the CompanyID to look up those details. Sometimes you can base your form or report on a query that contains all the additional tables. Other times, DLookup() will be a life-saver.
DLookup() expects you to give it three things inside the brackets. Think of them as:
Look up the _____ field, from the _____ table, where the record is _____
Each of these must go in quotes, separated by commas.
You must also use square brackets around the table or field names if the names contain odd characters (spaces, #, etc) or start with a number.
This is probably easiest to follow with some examples:
Look up the CompanyName field from table Company, where CompanyID = 874. This translates to:
=DLookup("CompanyName", "Company", "CompanyID = 874")
You don't want Company 874 printed for every record! Use an ampersand (&) to concatenate the current value in the CompanyID field of your report to the "Company = " criteria:
=DLookup("CompanyName", "Company", "CompanyID = " & [CompanyID])
If the CompanyID is null (as it might be at a new record), the 3rd agumenent will be incomplete, so the entire expression yields #Error. To avoid that use Nz() to supply a value for when the field is null:
=DLookup("CompanyName", "Company", "CompanyID = " & Nz([CompanyID],0))
The example above is correct if CompanyID is a number. But if the field is text, Access expects quote marks around it. In our second example, we look up the CategoryName field in table Cat, where Category = 'C'. This means the DLookup becomes:
=DLookup("CategoryName", "Cat", "Category = 'C'")
Single quotes within the double quotes is one way to do quotes within quotes. But again, we don't want Categoy 'C' for all records: we need the current value from our Category field patched into the quote. To do this, we close the quotation after the first single quote, add the contents of Category, and then add the trailing single quote. This becomes:
=DLookup("CategoryName", "Cat", "Category = '" & [Category] & "'")
In our third example, we need the full name from a Student table. But the student table has the name split into FirstName and Surname fields, so we need to refer to them both and add a space between. To show this information on your form, add a textbox with ControlSource:
=DLookup("[FirstName] & ' ' & [Surname]", "Student", "StudentID = '" & [StudentID] & "'")
Now you know how to supply the 3 parts for DLookup(), you are using quotes inside quotes. The single quote character fails if the text contains an apostrophe, so it is better to use the double-quote character. But you must double-up the double-quote character when it is inside quotes.
See Quotation marks within quotes for details.
If you are interested in an extended DLookup() that operates faster, handles nulls correctly, and lets you to specify which of several possible matches you want returned, see: ELookup(). Suits Access 95 and later.
|Home||Index of tips||Top|
This is a cached tutorial, reproduced with permission.
Have your say - comment on this article.
What did you think of 'Getting a value from a table: DLookup()'?
|Nguyen Bi says...|
26 December 2010
I spent a lot of time to deal with case looking up value where matched value is txt. This saved me lots of time.
Thanks so much!
Have your say...