在VBA中设置连接到访问数据库崩溃excel

这是我用来打开连接到Excel的访问数据库的代码。 它曾经工作了一年多。

Set dbname = New ADODB.Connection theconnection = "//xxx.sharepoint.com/sites" & Application.PathSeparator & TARGET_DB With dbname .Provider = "Microsoft.ACE.OLEDB.12.0" .Open theconnection End With 

通过试用一个错误,我得出结论,这条线是造成这个问题。

 Set dbname= New ADODB.Connection 

自动更新我的电脑我的Excel版本2016 MSO(16.0.7726.1036)32位后,问题开始

请让我知道,如果你也遇到这个问题,如果你知道任何修复或解决方法。

  • 尝试取消选中“ActiveX数据对象”引用并将其添加回来:

工具 – 参考

要么

  • 使用对象来定义一个数据库:

      Dim dbname As Object Set dbname = CreateObject("ADODB.Connection") 

要么

如果你创build这样的连接variables:

 Dim con as New ADODB.Connection 

将其更改为:

 Dim con as ADODB.Connection Set con = New ADODB.Connection 

也许

 Dim dbname As Object Set dbname = CreateObject("ADODB.Connection") theconnection = "//xxx.sharepoint.com/sites" & Application.PathSeparator & TARGET_DB With dbname .Provider = "Microsoft.ACE.OLEDB.12.0" .Open theconnection End With 

我用这个,所有的代码

 Dim Rs As Object Dim strConn As String Dim i As Integer Dim strSQL As String strSQL = "select * from [table] " strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" & _ "Data Source=" & ThisWorkbook.FullName & ";" & _ "Extended Properties=Excel 12.0;" Set Rs = CreateObject("ADODB.Recordset") Rs.Open strSQL, strConn If Not Rs.EOF Then With Ws .Range("a1").CurrentRegion.ClearContents For i = 0 To Rs.Fields.Count - 1 .Cells(1, i + 1).Value = Rs.Fields(i).Name Next .Range("a" & 2).CopyFromRecordset Rs End With End If Rs.Close Set Rs = Nothing