Sub connectmysqlnormal()
Dim objListObj As ListObject
For Each objListObj In ActiveSheet.ListObjects
objListObj.Delete
Next
ActiveSheet.Cells.Select
Selection.ClearContents
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"ODBC;DSN=localtest;", Destination:=Range("$A$1")).QueryTable
.CommandText = Array( _
"SELECT cpu_avg_statistics_0.LOGDATE as 'Date of Month', cpu_avg_statistics_0.CPU as 'CPU Utilization %' FROM test.cpu_avg_statistics cpu_avg_statistics_0 WHERE (cpu_avg_statistics_0.LOGDATE between '2012-02-01' and '2012-02-05') AND (cpu_avg_statistics_0.SERVER_NAME='adm1') ORDER BY cpu_avg_statistics_0.LOGDATE" _
)
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Table_Query_from_localtest"
.Refresh BackgroundQuery:=False
End With
End Sub
我可以连接在mysql中运行sql。但是当我按照上面的vba代码放置时, 我在sql语句中遇到了类型不匹配错误,请帮忙告诉我出了什么问题?
答案 0 :(得分:2)
在以下行中尝试不使用数组:
.CommandText = Array( _
"SELECT ..." _
)
答案 1 :(得分:0)
我认为您需要以下一行以及其他QueryTable属性:
.CommandType = xlCmdSql