2017-06-04 36 views
1

这是我用来打开从excel访问数据库的连接的代码。它曾经工作了一年多。在VBA中设置与访问数据库的连接崩溃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位的自动更新后开始

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

回答

0
  • 尝试取消您的 'ActiveX数据对象' 引用并将它们添加回:

工具 - 参考文献

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

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

如果创建连接变量是这样的:

Dim con as New ADODB.Connection 

更改为:

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

感谢提示。问题已经解决了。当我检查参考时,我检查了Microsoft ActiveX数据对象6.0库。当我没有选中它时,它就消失了。看起来在Office 365的一些升级过程中,版本6.1发生了变化。当我检查这个版本时,它又开始工作了。再次感谢您的帮助。 –

0

也许

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 
+0

我试过这个,但它没有解决问题。这是Microsoft ActiveX库升级引起的这个问题。感谢您的帮助。 –

+0

@MarcinDramiński:我修改了我的答案,完整的代码。 –