Question : Incorrect syntax near '#'. in ADODB.Connection in Excel workbook

With the block of code below in the Sheet Code of an Excel workbook I'm getting this error when I change the values

Run-time error '-2147217900 (80040e14)':
Incorrect syntax near '#'.

I copied and pasted this text from another sheet where it works fine:

"WHERE (CLSDDATE BETWEEN #" & Format(fromDate, "mm/dd/yyyy") & "# AND #" & Format(toDate, "mm/dd/yyyy") & "#)" & _

I'm not sure why I'm getting the syntax error.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
Dim dbConnection As ADODB.Connection
    Set dbConnection = New ADODB.Connection
    Dim connStr As String

    connStr = "myWonderfulConnectionString"
    dbConnection.ConnectionString = connStr
    dbConnection.Open

    Dim rsData As ADODB.Recordset
    Set rsData = New ADODB.Recordset
    Dim sql As String

Dim fromDate As Range
Set fromDate = ActiveSheet.Range("B1")

Dim toDate As Range
Set toDate = ActiveSheet.Range("D1"

sql = "SELECT [WO_NUM]" & _
        ",[CLSDBY]" & _
        ",[CLSDDATE]" & _
        "FROM [TRACKIT8_from7].[dbo].[TASKS] " & _
        "WHERE (CLSDDATE BETWEEN #" & Format(fromDate, "mm/dd/yyyy") & "# AND #" & Format(toDate, "mm/dd/yyyy") & "#)" & _
        "OR CLSDDATE IS NULL"

    With rsData
        .ActiveConnection = dbConnection
        .Open sql
            If Not rsData.EOF Then
                Worksheets("TrackIt").Range("A3").CopyFromRecordset rsData
                'ActiveSheet.Range("C6").CopyFromRecordset rsData
            End If
    End With

Answer : Incorrect syntax near '#'. in ADODB.Connection in Excel workbook

#date# is Access syntax. MSSQL needs single quotes ( 'date' ) around date literals.
Random Solutions  
 
programming4us programming4us