As most ASP developers quickly discover, Microsoft's two most
popular databases (Access and SQL Server) implement SQL
differently. The most significant difference for many users
is the difference in date delimiter characters. Access likes
# while SQL Server expects '. This can make writing a query
difficult when you're not sure what platform it'll end up
running on or you want it to run on both.
This tip will show you how to determine, from an existing
connection, what delimiter you should use. This function
checks for Access and if it finds it sets the delimiter to #.
If it doesn't it defaults to ' for SQL Server.
' This function takes an open ADODB.Connection object and will
' return the appropriate date delimiter to be used for queries.
' Only Access is checked for... ' is returned o/w. (SQL Server)
Function DateDelimiter(ByRef cnnOpenConnectionObject) ' As String
strDateDelimiter = "'"
strDBMSName = cnnOpenConnectionObject.Properties("DBMS Name").Value
If InStr(1, strDBMSName, "access", 1) Then strDateDelimiter = "#"
If InStr(1, strDBMSName, "jet", 1) Then strDateDelimiter = "#"
DateDelimiter = strDateDelimiter
This allows you to write code like this that will run against
either database without any changes.
' Assuming there's an open connection named cnnOpenConnection,
' the following will produce the appropriate SQL query for
' either Access or SQL Server without any code changes!
strDD = DateDelimiter(cnnOpenConnection)
strSQL = "SELECT * FROM table_name " & _
"WHERE date_field < " & strDD & Now() & strDD & ";"