简体   繁体   中英

ms access data type mismatch in criteria expression when passing a variable into a sql query

I am getting a data type mismatch with the following code. However, it looks fine when I do a debug print.

Dim strSQL As String
Dim StartDate As Date
Dim EndDate As Date

StartDate = “1/1/2019”
EndDate = “2/1/2019”


strSQL = SELECT Table1.Start INTO 1 FROM Table1 WHERE (((Table1.Start) BETWEEN ‘“ & StartDate & “‘“ & “ AND ‘“ & EndDate & “‘));”

Debug.Print strSQL

Docmd.RunSQL strSQL 

Use the correct syntax for dates and date value expressions:

Dim strSQL As String
Dim StartDate As Date
Dim EndDate As Date

StartDate = #1/1/2019#
EndDate = #2/1/2019#


strSQL = "SELECT Table1.Start INTO 1 FROM Table1 WHERE (((Table1.Start) BETWEEN #" & Format(StartDate, "yyyy\/mm\/dd") & "# AND #" & Format(EndDate, "yyyy\/mm\/dd") & "#);"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM