将ListObject从Excel导出到Access

时间:2013-04-03 04:27:37

标签: ms-access excel-vba vba excel

我正在编写一个将从excel工作簿运行并将其中一个ListObject导出到我的访问文件的宏。

我现在有这个VBA代码:

Sub AccessImport()

' Create connection
Dim Path As String
Dim conn As Object
Dim connectstr As String
Dim recordset As Object
Dim strSQL As String

Path = "P:\CALIBRE-YSP Implementation\11 General\CDM Database"

Set conn = CreateObject("ADODB.Connection")

connectstr = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=" & Path & "\CDM_Database_DataOnly.mdb;"

strSQL = "SELECT * INTO DeliverablesLivesheet FROM [Excel 8.0;HDR=YES;DATABASE=P:\CALIBRE-YSP Implementation\11 General\CDM Database\CDMv2 - Development.xlsm].[DeliverablesImport];"


conn.Open connectstr
Set recordset = conn.Execute(strSQL)

recordset.Close
Set recordset = Nothing

conn.Close
Set conn = Nothing

End Sub

它应该从Excel中获取ListObject,然后将数据传输到新的访问表,覆盖旧的访问表。

尝试执行SQL时会抛出错误:

strSQL = "SELECT * INTO DeliverablesLivesheet FROM [Excel 8.0;HDR=YES;DATABASE=P:\CALIBRE-YSP Implementation\11 General\CDM Database\CDMv2 - Development.xlsm].[DeliverablesImport];"

所以我猜SQL有问题吗?

我似乎无法解决这个问题

由于

2 个答案:

答案 0 :(得分:1)

strSQL="insert into DeliverablesLivesheet Select * FROM [Excel 8.0;HDR=YES;DATABASE=P:\CALIBRE-YSP Implementation\11 General\CDM Database\CDMv2 - Development.xlsm].[DeliverablesImport];"

答案 1 :(得分:0)

好的,我没有意识到连接字符串的作用如下:

[Excel 8.0; HDR = YES; DATABASE = {文件地址}]。[{sheet name}]

我在那里有ListObject的名字。

现在我陷入困境“数据库或对象是只读的。