在VBA中设置与访问数据库的连接会导致excel崩溃

时间:2017-06-04 10:16:35

标签: excel vba excel-vba crash adodb

这是我用来从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位

如果您已经遇到此问题,并且知道任何修复或解决方法,请告诉我。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

工具 - 参考

  • 使用object定义数据库:

     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
相关问题