我对VBA很新,而且我一直在尝试在Excel 2016中编写一些代码,当按下按钮时,会占用一系列单元格并将它们写入I&I表格中#39;已在Access 2016数据库中设置。我现在正在使用.xlsm工作簿。
我已经将我的代码基于批量插入方法(构建SQL" INSERT INTO"语句),提交者将其作为对此之前的StackOverflow问题的编辑{{3} }。
到目前为止,这里是我编写的VBA(或者至少是处理Access导入的部分)。当我运行它时,我收到错误"运行时错误' -2147467259(80004005)':找不到可安装的ISAM。"。我认为问题出在我的连接字符串中,我一直遇到问题,因为我无法在Internet上找到使用Access 2016和Excel 2016(大多数旧版本)的正确格式化连接字符串的示例)。
' Set up the transfer into Access
Dim dbPath As String, dbWb As String, dbWs As String, scn As String, dsh As String, ssql As String
Set cn = CreateObject("ADODB.Connection")
dbPath = Application.ActiveWorkbook.Path & "\MyDatabase.accdb"
dbWb = Application.ActiveWorkbook.FullName
dbWs = Application.ActiveSheet.Name
scn = "Provider=Microsoft.ACE.OLEDB.16.0;Data Source=" & dbPath
dsh = "[" & Application.ActiveSheet.Name & "$]" & "Data2" 'Data2 is the named range of data to be written to Access
' Open the connection with Access
cn.Open scn
' Create an SQL "Insert" statement using the name dsh developed earlier
ssql = "INSERT INTO DataPortfolio ([Field1], [Field2], [Field3]) "
ssql = ssql & "SELECT * FROM [Excel 16.0;HDR=YES;DATABASE=" & dbWb & "]." & dsh
' Execute the SQL statement
cn.Execute ssql
任何人都可以看到我可能出错的地方吗?我假设它在连接字符串(scn)中,但我不确定。
任何帮助表示赞赏!非常感谢。