A lot of Access users (especially newer ones) like to use the function DLookup to quickly lookup a value from a table or query. It is quite handy shorthand and can be used in VBA and in queries.
The usage of DLookup is:
DLookup (Expr, Domain, Criteria)
Or to put in more Access friendly terms:
Expr is a field name you want.
Domain is a table or query name.
Criteria is what you’d put in the WHERE clause to filter the results.
So you could do something like this to return the phone number of an Employee named “Jon” from the Employees table.
This is little more readable and easy to type than something like this:
Those two statements would return the same result. They are both doing essentially the same thing, opening a new database object, executing a SQL command, and returning the result of that command.
Now the first thing to understand about that is:
- It’s a very expensive operation.
So this would be very slow to do 1000 times in a loop compared to simple VBA operations.
- It also does not immediately release the database object it creates.
This is something you’d likely never know unless you’ve experienced the dreaded “Out of Resources” error, but DLookup doesn’t release the database object it creates after it’s done. Garbage collection in Access seems to clean up after it, but calling it recursively in queries or in long code loops can eventually garner you a nice error message which is not at all clear.
To fix this condition, as well as allow a few extra features (like grouping functions and table joins) we have written this function that we use in our HAL library in our Access JumpStart product and affectionately call “Nice” DLookup (NiceDLookup) It tries to behave in a similar manner as far as what it returns in terms of error messages and values and nulls, but it cleans up after itself after each call. Here it is for your viewing pleasure:
Bonus content: Eric Blomquist took the function and made a few modifications for his own library and sent me a copy. Check it out. He commented each change.
Nice, but if there are spaces in either the table or field name, the function will return nothing or an error. I know it may be unprofessional with spaces in these names, but Access do allow it.
Hi mor10! If you enclose your table name in brackets, this function will handle table names with spaces and special characters. That’s how Access handles them in the background with queries and in other expressions like using Forms!Form Name, you would have to use Forms![Form Name] in order for it to work.
Like this: [My Table Name With Spaces]
But I see you are right in terms of matching functionality with DLookup. DLookup requires an existing table or query object and won’t allow you to do fancy joins and SQL things in the domain argument, but if you just put the name of the table with spaces or special characters it doesn’t care if you use brackets.
To make this a drop in replacement for this functionality, I could add a check at the beginning to see if the whole string is an existing table or query and if so, add brackets around it. Otherwise, run it as is. That would be relatively easy since I have a table exists function and a query exists function already.
I will do that for today’s message!