尝试通过 VBA 和 OLEDB 在 excel 中创建 table
Trying to create a table in excel via VBA and OLEDB
我正在尝试使用 VBA 和 ADODB 在 Excel 中创建一个 table(制表符)。你能告诉我哪里出错了吗?
我在 comm.CommandText = "create table [temptable]([AA] VARCHAR(40));"
行之后的 comm.Execute
处收到错误:
Microsoft Access database engine could not find the object 'temptable'.
在 table 名称末尾添加 $ 或在开头添加 # 似乎没有帮助。
Sub sqlquery2()
Dim rs As ADODB.Recordset
Dim conn As String
Dim conObj As ADODB.Connection
Dim comm As ADODB.Command
Dim fileName As String
Set conObj = New ADODB.Connection
Set comm = New ADODB.Command
fileName = ThisWorkbook.FullName
conn = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & fileName & ";" & _
"Extended Properties=Excel 12.0 XML"
conObj.ConnectionString = conn
conObj.Open
comm.ActiveConnection = conObj
comm.CommandType = adCmdText
On Error Resume Next
comm.CommandText = "DROP TABLE [temptable];"
comm.Execute
On Error GoTo 0
comm.CommandText = "create table [temptable]([AA] VARCHAR(40));"
comm.Execute
comm.CommandText = "insert into [temptable] select [A] from [SQL_Test$];"
comm.Execute
conObj.Close
End Sub
非常感谢您!
尝试在关闭的工作簿上执行此操作。
我在打开工作簿时遇到了这个问题。当我在一个封闭的工作簿上这样做时它就消失了。当 wbk 打开时,它也崩溃了,告诉我 table(工作表)不存在。工作表确实存在。当我查询一个关闭的wb时,这个问题也消失了。
我正在尝试使用 VBA 和 ADODB 在 Excel 中创建一个 table(制表符)。你能告诉我哪里出错了吗?
我在 comm.CommandText = "create table [temptable]([AA] VARCHAR(40));"
行之后的 comm.Execute
处收到错误:
Microsoft Access database engine could not find the object 'temptable'.
在 table 名称末尾添加 $ 或在开头添加 # 似乎没有帮助。
Sub sqlquery2()
Dim rs As ADODB.Recordset
Dim conn As String
Dim conObj As ADODB.Connection
Dim comm As ADODB.Command
Dim fileName As String
Set conObj = New ADODB.Connection
Set comm = New ADODB.Command
fileName = ThisWorkbook.FullName
conn = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & fileName & ";" & _
"Extended Properties=Excel 12.0 XML"
conObj.ConnectionString = conn
conObj.Open
comm.ActiveConnection = conObj
comm.CommandType = adCmdText
On Error Resume Next
comm.CommandText = "DROP TABLE [temptable];"
comm.Execute
On Error GoTo 0
comm.CommandText = "create table [temptable]([AA] VARCHAR(40));"
comm.Execute
comm.CommandText = "insert into [temptable] select [A] from [SQL_Test$];"
comm.Execute
conObj.Close
End Sub
非常感谢您!
尝试在关闭的工作簿上执行此操作。
我在打开工作簿时遇到了这个问题。当我在一个封闭的工作簿上这样做时它就消失了。当 wbk 打开时,它也崩溃了,告诉我 table(工作表)不存在。工作表确实存在。当我查询一个关闭的wb时,这个问题也消失了。