Below is UNQUESTIONABLY the "Poster Child" of the special interest group known as "Programmers Who Disdain Embedded-in-source SQL and/or Touchy SQL Syntax":
Read all in it's glory here:
http://www.utteraccess.com/forums/showflat.php?Board=82&Number=1755780
-------------------------------------------------------------------------
strSQL = "UPDATE [t_table1] " & _
" SET [t_table1].dateRemoved = Date() " & _
" WHERE ([t_table1].EmployeeID = """ & Forms!YourForm!cboEmployeeID & """) " & _
" And ([t_table1].recAdded = " & Format( _
DMax("recAdded", "t_table1", "[EmployeeID] = """ & _
Forms!YourForm!cboEmployeeID & """"), "\#yyyy-mm-dd\#") & ") " & _
" And ([t_table1].dateRemoved Is Null)" In general, when construction an SQL String in VBA code, the most important thing to remember is that there are 2 completely different usages of the double-quote (or single-quote):
1. The first usage is to use the double-quotes as String delimiters
in VBA to supply a literal String in the construction. The pair of double-quotes (the left-most double-quote and the right-most double-quote in the literal String component used in the concatenation) will be consumed by VBA, i.e. they won't appear in the final SQL String.
2. The second usage is
to supply the double-quote to the SQL String, meaning you want the double-quote to appear in the final SQL String (and not consumed by VBA).
The 2 different usages of the double-quote plus the requirement that to include a double-quote in a double-quote-delimited String, you need to use 2 double-quotes create a myriad of double-quotes required in the (VBA) SQL String construction. This is rather daunting to construct but you will get used to it provided that you are aware of the VBA consumption. A very useful tool is the Debug.Print strSQL statement so that you can inspect the result of the String concatenation.
I am sure that you are aware of the delimiting requirements for literal values in SQL String but I list here for completeness:
1. A literal string value must be enclosed in double-quotes (or single-quotes)
2. A literal date/time value must be enclosed in hashes (#). In addition, the date value must be in the US format "mm/dd/yyyy" or an internationally unambiguous format such as "yyyy-mm-dd" which I use in the posted SQL construction.
3. A literal numeric value does not require delimiting characters.
Note also the use of parentheses I adopt. While most of the time, JET seems to be able to interpret the component criteria correctly (I had one case where using parentheses worked and using no parentheses didn't work a few years back but didn't note down the exact situation / SQL), I just feel that using a pair of parentheses around each component criterion clearly shows the logical operation, much better than without parentheses. I read in Roger Jennings' "Using Access 97" (published by Que) that the multiple parentheses created by JET / Access (when the QBE is used to construct Queries) do have their proper functions in delineating the Boolean components / operations and sometimes, multiple / nested parentheses are necessary. I basically adopt using a pair of parentheses as the delimiter for each component Boolean expression as it is easier for me to see the logic of the combined criterion, IMHO.