【发布时间】:2020-06-23 12:50:57
【问题描述】:
Sub Get_Data()
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim dateVar As Date
Set conn = New ADODB.Connection
conn.ConnectionString = "DRIVER={MySQL ODBC 5.1 Driver}; SERVER=localhost; DATABASE=bi; UID=username; PWD=password; OPTION=3"
conn.Open
strSQL = " SELECT " & _
" Products " & _
" FROM Logistics " & _
" WHERE DATE(insert_timestamp) = ""2020-02-24"" " & _
" GROUP BY 1"
Set rs = New ADODB.Recordset
rs.Open strSQL, conn, adOpenStatic
Sheet5.Range("A1").CopyFromRecordset rs
rs.Close
conn.Close
End Sub
我运行上述VBA 以从database 中提取值。
它完全符合我的需要。
现在,我不想在strSQL 中使用预定义的日期,而是想使用在我的 Excel 电子表格中输入到 Cell B1 的数据。因此,我将VBA中的strSQL部分改为:
strSQL = " SELECT " & _
" Products " & _
" FROM Logistics " & _
" WHERE DATE(insert_timestamp) = " & Sheet1.Range("B1") & " " & _
" GROUP BY 1"
但是,现在我在rs.Open strSQL, conn, adOpenStatic 上收到了runtime error -2147217887 (80040e21)。
我需要更改我的VBA 中的哪些内容才能使其正常工作?
【问题讨论】: