[英]Getting Type Mismatch error on vba while trying to fetch data from ODBC database
I am trying to fetch some data from SQL query using vba, but when I try to run the code it gives me a type mismatch error.我正在尝试使用 vba 从 SQL 查询中获取一些数据,但是当我尝试运行代码时,它给了我一个类型不匹配错误。 can anyone please help任何人都可以请帮忙
Sub Get_Rowkey()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim DBresults As ADODB.Recordset
Dim sqlquery As String, wsData As Worksheet, Datasht As Worksheet
Dim elr As Long
Dim lRow As Long
Set DBresults = New ADODB.Recordset
Set wsData = Worksheets("Main")
Set Datasht = Worksheets("Backend")
lRow2 = Datasht.Cells(Rows.Count, 2).End(xlUp).Row
elr = Datasht.Cells(Rows.Count, 3).End(xlUp).Row
If lRow2 > 1 Then
Call createconn
sqlquery = "SELECT vyc.object_id, vyc.rowkey, el.instrument, i.ccy, vyc.timezone, vyc.asof, vyc.snaptime, vp.rate as Mid, vp.bid, vp.ask, vp.updatetime" & vbNewLine & _
"from val_yield_curves vyc" & vbNewLine & _
"join val_yc_def_elements el on el.pkey = valuations_yieldcurves.getCurveDefinitionKey(vyc.asof, vyc.object_id)" & vbNewLine & _
"join (SELECT asof, curve, instrument, rate, quality, source, updatetime, bid, ask from val_prices_interestrates" & vbNewLine & _
"union SELECT asof, curve, instrument, rate, quality, source, updatetime, bid, ask from val_prices_basisswaps" & vbNewLine & _
"union SELECT asof, curve, instrument, rate, quality, source, updatetime, bid, ask from val_prices_oisrates" & vbNewLine & _
"union SELECT asof, curve, instrument, rate, quality, source, updatetime, bid, ask from val_prices_fx)" & vbNewLine & _
"vp on el.instrument = vp.instrument and vp.asof = vyc.asof and vp.curve = vyc.rowkey" & vbNewLine & _
"join (select pkey, symbol from val_mds_sources) vms on vms.pkey = vp.source" & vbNewLine * _
"join instruments i on i.pkey = el.instrument" & vbNewLine & _
"where vyc.asof in '12Oct2020'" & vbNewLine & _
"and vyc.timezone in 'L1200'" & vbNewLine & _
"and vyc.object_id like '%FXSpot%'" & vbNewLine & _
"ORDER BY vp.updatetime"`enter code here`
''sqlquery = Left(sqlquery, Len(sqlquery) - 1) & ")"
DBresults.Open sqlquery, DBConn, adOpenDynamic
Consider saving your long, involved SQL in a text file with all needed formatting of line breaks and spaces which helps in readability and maintainability.考虑将冗长的、涉及的 SQL 保存在一个文本文件中,其中包含所有需要的换行符和空格格式,这有助于提高可读性和可维护性。 You can even catch syntax errors like not wrapping IN
elements in parentheses (though one item list should be replaced with =
).您甚至可以捕获语法错误,例如未将IN
元素括在括号中(尽管一个项目列表应替换为=
)。
Then, read into Excel as string without any messy concatenation or quote punctuation.然后,将 Excel 作为字符串读入,没有任何混乱的串联或引号标点符号。
SQL (save as .sql file) SQL (另存为 .sql 文件)
SELECT vyc.object_id
, vyc.rowkey
, el.instrument
, i.ccy
, vyc.timezone
, vyc.asof
, vyc.snaptime
, vp.rate AS Mid
, vp.bid
, vp.ask
, vp.updatetime
FROM val_yield_curves vyc
JOIN val_yc_def_elements el
ON el.pkey = valuations_yieldcurves.getCurveDefinitionKey(vyc.asof, vyc.object_id)
JOIN
(SELECT asof, curve, instrument, rate, quality, source, updatetime, bid, ask
FROM val_prices_interestrates
UNION
SELECT asof, curve, instrument, rate, quality, source, updatetime, bid, ask
FROM val_prices_basisswaps
UNION
SELECT asof, curve, instrument, rate, quality, source, updatetime, bid, ask
FROM val_prices_oisrates
UNION
SELECT asof, curve, instrument, rate, quality, source, updatetime, bid, ask
FROM val_prices_fx) vp
ON el.instrument = vp.instrument
AND vp.asof = vyc.asof
AND vp.curve = vyc.rowkey
JOIN
(SELECT pkey, symbol FROM val_mds_sources) vms
ON vms.pkey = vp.source
JOIN instruments i
ON i.pkey = el.instrument
WHERE vyc.asof IN ('12Oct2020') -- FIXED
AND vyc.timezone IN ('L1200') -- FIXED
AND vyc.object_id LIKE '%FXSpot%'
ORDER BY vp.updatetime
VBA VBA
...
' READ SQL QUERY FROM FILE INTO STRING
With CreateObject("Scripting.FileSystemObject")
sqlquery = .OpenTextFile("C:\path\to\my\SQL\Query.sql", 1).readall
End With
DBresults.Open sqlquery, DBConn, adOpenDynamic
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.