0
我試圖構建一個查詢Apache Drill的宏。我無法得到它的工作。VBA/Excel - 查詢Apache Drill
我試過兩種方法。 首先使用QueryTables.Add
。它不斷添加結果,以便不更新結果,但只將舊結果移到右側,並在A1中插入新的結果。
問:如何使查詢刪除舊結果並將新結果放在同一個地方?
第二次嘗試使用ADODB.Recordset
。該連接不起作用錯誤:[微軟] [ODBC驅動程序管理器]數據源名稱找不到和未指定默認驅動程序
問:能否ADODB.Connection
,ADODB.Recordset
被用於從Appachi鑽查詢數據?
首次嘗試
Sub S3Download()
Dim sConn As String
Dim oQt As QueryTable
Dim sSql As String
sConn = "ODBC;DSN=MapR Drill;"
sSql = "SQL statement"
Set oQt = Sheets("Data").QueryTables.Add(Connection:=sConn,_
Destination:=Sheets("Data").Range("A1"), SQL:=sSql)
With oQt
.Name = "Query1"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = False
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=True
.RefreshStyle = xlInsertDeleteCells
End With
End Sub
第二次嘗試
Sub S3_download()
Dim oCn As ADODB.Connection
Dim oRS As ADODB.Recordset
Dim ConnString As String
Dim SQL As String
Dim qt As QueryTable
ThisWorkbook.Sheets("Data").Activate
ConnString = "Driver={MySQL ODBC 5.1 Driver};DSN=MapR Drill;"
Set oCn = New ADODB.Connection
oCn.ConnectionString = ConnString
oCn.Open
SQL = "SQL statement"
Set oRS = New ADODB.Recordset
oRS.Source = SQL
oRS.ActiveConnection = oCn
oRS.Open
Set qt = ThisWorkbook.Sheets("Data").ListObjects.Add(SourceType:=XlListObjectSourceType.xlSrcQuery, Source:=oRS, _
Destination:=ThisWorkbook.Sheets("Data").Range("A1")).QueryTable
qt.Refresh
If oRS.State <> adStateClosed Then
oRS.Close
End If
If Not oRS Is Nothing Then Set oRS = Nothing
If Not oCn Is Nothing Then Set oCn = Nothing
End Sub