Excel VBA在Where子句之间使用

时间:2016-04-28 14:42:44

标签: sql excel vba excel-vba

如何在where子句中使用两个日期。编译器一直给我关于语法的错误。我需要在此语法中更改以使用where where子句

  
Sub Get_Data()
Dim cn As Object
Dim rs As Object
Dim strFile As String
Dim strCon As String
Dim strSQL, strInput As String

strFile = "A:\Test\de.accdb"

strCon = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & strFile

Set cn = CreateObject("ADODB.Connection")
cn.Open strCon

d1 = InputBox("Input Start Date")
d2 = InputBox("Input End Date")

strSQL = "SELECT NAME, Location From db 1WHERE orderdate between ""'d1'"" AND ""'d2'"" Order By Location ASC;"

cn.Execute strSQL

cn.Close
Set cn = Nothing

End Sub

2 个答案:

答案 0 :(得分:2)

strSQL = "SELECT NAME, Location From db WHERE orderdate between '" & d1 & "' AND '" & d2 & "' Order By Location ASC;"

答案 1 :(得分:0)

看起来好像你忘记了查询中的strSQL = "SELECT NAME, Location FROM [name of table] WHERE orderdate between '" & d1 & "' AND '" & d2 & "'" 函数,你需要用ambersands分离出d1和d2,试试这个:

web.ex