I am using this handy function to format dates:
Public Function JetSqlDate(ByVal d As Variant) As String
If IsNull(d) Then
JetSqlDate = "NULL"
Else
JetSqlDate = Format$(d, "\#mm\/dd\/yyyy hh\:nn\:ss\#")
End If
End Function
Usage:
strRowSource = "SELECT ID, TaskType, TaskName, StartDate, EndDate, isFinished " & _
"FROM tblTasks WHERE Person = " & TempVars("CurrentUser") & _
" AND EndDate < " & JetSqlDate(Now())
Note that escaping column names ([]) is only required when they contain invalid characters like spaces or when they conflict with keywords. E.g. if you had a column named From you would have to write it as [From].
for SQL-Server pass-through queries I use:
Public Function TSqlDate(ByVal d As Variant) As String
If IsNull(d) Then
TSqlDate = "NULL"
Else
TSqlDate = "{ ts '" & Format$(d, "yyyy\-mm\-dd hh\:nn\:ss") & "' }"
End If
End Function
For strings:
Public Function SqlStr(ByVal s As String) As String
'Input: s="" Returns: NULL
'Input: s="abc" Returns: 'abc'
'Input: s="x'y" Returns: 'x''y'
If s = "" Then
SqlStr = "NULL"
Else
SqlStr = "'" & Replace(s, "'", "''") & "'"
End If
End Function
Note that this functions adds the delimiters and escapes any delimiters contained in the string value. This makes queries more reliable and also prevents SQL Injection.
Format(Date, "mm/dd/yyyy").