So first things first. It is not recommended that you use spaces or special characters in your table names as it can cause problems referencing them or cases that are ambiguous and may cause unintended results in the code.
But maybe you have used them in the past, or have inherited databases with tables with commas, spaces, ampersands, and other weird special characters in the table name, how do you deal with them?
In Access, there is an optional ability to put brackets around a string to have it interpret the string as the name of a table, query, module, or any other Access object. So let’s say you’ve named your table:
Lions, Tigers, & Bears = ‘Oh My’
Yes, that is a valid table name as far as Access is concerned. You could represent that in a SQL statement as:
SELECT * FROM [Lions, Tigers, & Bears = ‘Oh My’]
There are a few characters that are actually illegal. Check out this Microsoft article for more info:
Guidelines for naming fields, controls, and objects – Microsoft Support
The reason I brought up this topic is because Mar10 brought to my attention that the NiceDLookup function does not allow spaces in table or query names like DLookup does. In DLookup, I can use a string of the table or query name with the spaces and special characters as the domain argument and it will correctly use that table or query. In NiceDLookup, I could work around this by using brackets around a domain argument I was passing, but this does not mimic the behavior of the DLookup function. Here is the original source of the NiceDLookup function:
DLookup and “NiceDLookup” | Access JumpStart
In order to remedy this, I am using two more functions which check for queries and tables in the current database. These were already in my library and can be added as a check to the NiceDLookup function so we can add brackets around it if it’s an actual object. Here are the QueryExists and TableExists functions from my library:
Now we can write some code to update our function to check for the “domain” parameter full string as a table or query in the current database:
And here is the new function implementing the new code:
Trackbacks/Pingbacks