我希望能够将Excel表格连接到MsSQL表或其他Excel表格。我试图测试第二种情况。我正在尝试使用OPENROWSET从Excel表中获取数据。稍后我将使用它来连接表,但现在我甚至无法使用这个简单的查询。
Ciselnik1
是Test.xlsx工作簿中的工作表,包含一个带标题的小表:
FK__S_HEAD | Desc
-------------------------------
ODD AM | ODD - description1
ODDZP | ODD - desc2
以下代码抛出了“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
答案 0 :(得分:0)
OPENROWSET不是Microsoft.ACE.OLEDB.12.0提供程序支持的功能。