简体   繁体   English

将命名表从 Excel 导出到 Access

[英]Export Named Table from Excel to Access


OK.好的。 I'm sorry for wasting everyone's time.很抱歉浪费了大家的时间。 Like a DUMMY i didn't think simple solution first.就像一个 DUMMY 一样,我首先没有想到简单的解决方案。 The amount of data i am dealing with isn't too large and will actually work better just exporting to an excel file (i'm pretty sure).我正在处理的数据量并不太大,实际上只要导出到 excel 文件(我很确定),效果会更好。 I would like to thank all that helped (June7, Parfait, and HansUp).我要感谢所有帮助过的人(June7、Parfait 和 HansUp)。 The support you guys (everyone on this forum) give has made my job easier by far.到目前为止,你们(这个论坛上的每个人)给予的支持让我的工作变得更轻松了。


I'm trying to export an Excel Table from my active excel file to an Access database file.我正在尝试将 Excel 表从我的活动 excel 文件导出到 Access 数据库文件。 I was getting an error at我在

"con.excecute sql" “con.execute sql”

"Run-time error '-2147467259 (80004005)': [Microsoft][ODBC Microsoft Access Driver] Query input must contain at least one table or query." “运行时错误 '-2147467259 (80004005)':[Microsoft][ODBC Microsoft Access Driver] 查询输入必须至少包含一个表或查询。”

Sub updateAccess()

Dim con As New ADODB.Connection
Dim connectionString As String

Dim sql, newTable As String
Filename = "C:\Desktop\Quote-Size_Contacts.accdb"
connectionString = "Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" & Filename

con.Open connectionString

' Save current table ("ContactsTbl_Data") to another table ("ContactsTbl_Data_yyyymmdd_hh_mmss")
newTable = "Quote-Size_Contacts_" & Format(Date, "yyyymmdd") & "_" & Format(Now, "hhmmss")
sql = "SELECT CODE, STORE INTO " & newTable & "FROM ContactsTbl_Data"
con.Execute sql

' Delete rows of current table ("ContactsTbl_Data")
sql = "DELETE FROM ContactsTbl_Data"
con.Execute sql

' Insert new rows into current table ("ContactsTbl_Data") from my Excel Sheet
sql = "INSERT INTO ContactsTbl_Data ([CODE], [STORE]) " & _
      "SELECT * FROM [Excel 8.0;HDR=YES;DATABASE=" & ThisWorkbook.FullName & "].[" & ThisWorkbook.Sheets("Sheet2").Name & "$]"
con.Execute sql

con.Close
Set con = Nothing

End Sub

EDIT:: I'm not sure standard protocol for these forums on cleaning up the code and asking more questions so i'll just put an "Edit" here.编辑:: 我不确定这些论坛关于清理代码和提出更多问题的标准协议,所以我将在这里放置一个“编辑”。 I applied the suggestions and matched the fields it was trying to save to my access file.我应用了这些建议并将它试图保存到我的访问文件的字段匹配。 I now get the error: "Method 'Execute' of object '_Connection' failed"我现在收到错误消息:“object '_Connection' 的方法 'Execute' 失败”

Public Sub updateAccess()

Dim con As New ADODB.Connection
Dim connectionString As String

Dim sql, newTable As String
Filename = "C:\Desktop\Quote-Size_Contacts.accdb"
connectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source='" & Filename & "'"

con.Open connectionString

' Save current table ("ContactsTbl_Data") to another table ("ContactsTbl_Data_yyyymmdd_hh_mmss")
newTable = "Quote-Size_Contacts_" & Format(Date, "yyyymmdd") & "_" & Format(Now, "hhmmss")
sql = "SELECT Company, Contact, Initials, Position, Address, AddressContd, CityStatePost, MainNo, CellNo, FaxNo, Email INTO [" & newTable & "] FROM ContactsTbl_Data"
con.Execute sql

' Delete rows of current table ("ContactsTbl_Data")
sql = "DELETE FROM ContactsTbl_Data"
con.Execute sql

' Insert new rows into current table ("ContactsTbl_Data") from my Excel Sheet
sql = "INSERT INTO ContactsTbl_Data ([Company], [Contact], [Initials], [Position], [Address], [AddressContd], [CityStatePost], [MainNo], [CellNo], [FaxNo], [Email]) " & _
      "SELECT * FROM [Excel 12.0 Xml;HDR=Yes;Database=" & ThisWorkbook.FullName & "].[" & ThisWorkbook.Sheets("Sheet2").Name & "$]"
con.Execute sql

con.Close
Set con = Nothing

End Sub

See if this helps.看看这是否有帮助。

Table name has hyphen (-) character so use [ ] characters to delimit.表名包含连字符 (-) 字符,因此使用 [ ] 字符进行分隔。 Add a space in front of FROM so text doesn't run together in compiled SQL string.在 FROM 前面添加一个空格,这样文本就不会在编译的 SQL 字符串中一起运行。

sql = "SELECT CODE, STORE INTO [" & newTable & "] FROM ContactsTbl_Data"

As for connection to Access database, don't think I've ever used or seen Driver, I use Provider:至于连接Access数据库,别以为我没用过,也没见过Driver,我用的是Provider:
connectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source='" & Filename & "'"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM