【发布时间】:2020-10-18 21:44:15
【问题描述】:
我正在尝试使用 vba 从 SQL 查询中获取一些数据,但是当我尝试运行代码时,它给了我一个类型不匹配的错误。谁能帮忙
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
【问题讨论】:
-
所有子句前都需要一个空格,
FROM,JOIN,UNION,WHERE,ORDER BY,以及运算符:AND。 -
嗨,Parfait,感谢您的解决方案,您能否进一步说明一下。我需要使用类似“来自 abc 表的东西吗?