[英]VBA, Excel ODBC driver - Type Mismatch error while using sql query with parameter from cell
I have problem with Type Mismatch error
while I do a select with getting parameter from excels cell. 当我从excels单元格中获取参数时,我遇到
Type Mismatch error
问题。 I am using excel like a database. 我像数据库一样使用excel。 My code is:
我的代码是:
Sub maxlg()
Dim sSQLQry As String
Dim ReturnArray
Dim Conn As New ADODB.Connection
Dim mrs As New ADODB.Recordset
Dim DBPath As String, sconnect As String
DBPath = ThisWorkbook.FullName
sconnect = "Provider=MSDASQL.1;DSN=Excel Files;DBQ=" & DBPath & ";HDR=Yes';"
Conn.Open sconnect
sSQLSting = "SELECT SUM(Sicherkoef*LG/KFM/'" & Range("U17") & "') AS MAXLG From [DATABASE$] WHERE Platzierung =('" & Range("S11") & "') AND RPJ = ('" & Range("U12") & "') "
mrs.Open sSQLSting, Conn
'=>Load the Data into an array
'ReturnArray = mrs.GetRows
''OR''
'=>Paste the data into a sheet
ActiveSheet.Range("V12").CopyFromRecordset mrs
'Close Recordset
mrs.Close
Conn.Close
End Sub
Problem is in getting parameter from here: RPJ = ('" & Range("U12") & "') "
in the WHERE
clause. 问题在于从这里获取参数:在
WHERE
子句中RPJ = ('" & Range("U12") & "') "
。
You may have suffered from a little too much copy and paste. 您可能已经遭受了太多的复制和粘贴。 It seems that you are wrapping the `Range("U17") value in ticks while using it within a mathematical calculation.
在数学计算中使用它时,您似乎在刻度线中包含`Range(“U17”)值。
sSQLSting = "SELECT SUM(Sicherkoef*LG/KFM/" & Range("U17").value & ") AS MAXLG From [DATABASE$] WHERE Platzierung ='" & Range("S11").value & "' AND RPJ = '" & Range("U12").value & "';"
I've also removed some bracketing in the actual text WHERE conditions. 我还删除了实际文本WHERE条件中的一些包围。 They are really only necessary when multiple text values are used with,
实际上只有在使用多个文本值时才需要它们,
... WHERE [fld1] IN ('abc', 'def')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.