【发布时间】:2021-02-23 13:32:30
【问题描述】:
我正在编写 SQL 查询,但字符串值出错:
Dim cn As Object, rs As Object, output As String, sql As String
Dim dt As Date
Dim lne As String
Const adCmdText As Integer = 1, adDate As Integer = 7, adChar As Integer = 8
Set cn = CreateObject("ADODB.Connection")
With cn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = "Data Source=" _
& ThisWorkbook.FullName & ";" _
& "Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
.Open
End With
With Worksheets("Page 2")
dt = CDate(Replace(.Cells(i, 2).Value, ".", "-"))
lne = .Cells(2, 1).Value ' lne = mystr
End With
sql = "SELECT Name, SUM(Worktime) as summa FROM [Page 1$] WHERE DateTime = ? AND Line = " & lne & " GROUP BY Name"
Set cmd = CreateObject("ADODB.Command")
With cmd
.ActiveConnection = cn
.CommandText = sql
.CommandType = adCmdText
.Parameters.Append .CreateParameter("dt", adDate, 1, , dt)
Set rs = .Execute ' ERROR: One or more required parameters are missing a value.
End With
我尝试添加多个 cmd 参数来解决这个问题:
sql = "SELECT Name, SUM(Worktime) as summa FROM [Page 1$] WHERE DateTime = ? AND Line = ? GROUP BY Name"
Set cmd = CreateObject("ADODB.Command")
With cmd
.ActiveConnection = cn
.CommandText = sql
.CommandType = adCmdText
.Parameters.Append .CreateParameter("dt", adDate, 1, , dt)
.Parameters.Append .CreateParameter("lne", adChar, 2, , lne)
Set rs = .Execute
End With
但我也得到一个错误。如何设置多个 cmd 参数并在查询中使用它?
我正在使用的表:here。
【问题讨论】:
-
对于第二个代码块,你说但我也得到一个错误。这是同一个错误吗?如果没有,请发布它的消息。