0
我希望能夠將Excel表聯接到MsSQL表或其他Excel表。我試圖測試第二種情況。我試圖使用OPENROWSET從Excel表中獲取數據。後來我會用它來連接表,但現在我無法工作,即使這個簡單的查詢。FROM子句中的VBA OPENROWSET語法錯誤
Ciselnik1
是在Test.xlsx工作簿中工作,幷包含與頭小表:
FK__S_HEAD | Desc
-------------------------------
ODD AM | ODD - description1
ODDZP | ODD - desc2
的follwing代碼拋出 「語法錯誤在FROM子句」 錯誤:
Sub TestExternalSQLwithCisJoin()
Dim objConn As ADODB.Connection, objCmd As ADODB.Command, objRS As ADODB.Recordset
Dim sPath As String, sSQL As String, sConn As String
Set objConn = New ADODB.Connection
Set objCmd = New ADODB.Command
Set objRS = New ADODB.Recordset
sSQL = "SELECT * FROM OPENROWSET(""Microsoft.ACE.OLEDB.12.0"",""Database=c:\...\Test.xlsx;Extended Properties=Excel 12.0 Xml;HDR=YES"",""SELECT * FROM [Ciselnik1$]"")"
sConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\...\Test.xlsm;Extended Properties=""Excel 12.0 Macro;HDR=YES"";"
Set objConn = New ADODB.Connection
'MsgBox sSQL
objConn.Open sConn
'objRS.Open sSQL, objConn, adOpenStatic, adLockBatchOptimistic, adCmdText
objConn.Execute sSQL, lngRecsAff, adExecuteNoRecords
Dim A0cell As Range
Worksheets("Test").Activate
Set A0cell = Worksheets("Test").Cells(1, 1)
A0cell.CopyFromRecordset objRS
End Sub