Excel SQL VBA: 添加多个cmd参数
Excel SQL VBA: adding more than one cmd parameter
我正在写 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 参数并在我的查询中使用它?
Table 我正在与:here.
CreateParameter
的参数是
command.CreateParameter (Name, Type, Direction, Size, Value)
Direction
- 对于所有参数
,参数应该是 adParamInput
(即 1)
我的经验是,对于字符串参数(adChar
),需要提供大小(字符串的长度)。
尝试
.Parameters.Append .CreateParameter("lne", adChar, adParamInput, len(lne), lne)
我正在写 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 参数并在我的查询中使用它?
Table 我正在与:here.
CreateParameter
的参数是
command.CreateParameter (Name, Type, Direction, Size, Value)
Direction
- 对于所有参数
adParamInput
(即 1)
我的经验是,对于字符串参数(adChar
),需要提供大小(字符串的长度)。
尝试
.Parameters.Append .CreateParameter("lne", adChar, adParamInput, len(lne), lne)