【问题标题】:Excel VBA 3704 Error (Can't open connection to AdoDB)Excel VBA 3704 错误(无法打开与 AdoDB 的连接)
【发布时间】:2021-07-01 00:49:45
【问题描述】:

我正在尝试使用 adodb 处理我的工作表中的某些内容。 出于某种原因,我一开始就无法打开连接;下面的代码给出了 3704 错误

我检查了我的参考资料,Active X 等已经启用。

Dim sSQLQry As String
Dim ReturnArray

Dim oCn As New ADODB.Connection
Dim oRs As New ADODB.Recordset
Dim str_provider As String
Dim str_hdr As String
If Application.Version < 12 Then
    str_provider = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="
    str_hdr = "Excel 8.0;"
Else
    str_provider = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source="
    str_hdr = "Excel 12.0;"
End If

Dim str_conn
str_conn = str_provider & ThisWorkbook.FullName & "; Extended Properties='" & str_hdr & "HDR=Yes;'"";"
oCn.Open str_conn
Dim str_query As String
str_query = "select * from " + "final_song" + "where 1=0"
oCn.Execute str_query

【问题讨论】:

  • str_hdr = "Excel 12.0;" 更改为str_hdr = "Excel 12.0 Xml; 会发生什么?顺便说一句,错误何时发生?对于8.012.0
  • 还将str_query = "select * from " + "final_song" + "where 1=0" 更改为str_query = "select * from final_song where 1=0",看看是否有区别? “在哪里”之前没有空格
  • 您的文件在 OneDrive 中吗?
  • @SiddharthRout 给我一个运行时错误“02147217805 (80040e73)”。错误是针对 12.0
  • @SiddharthRout 在“where”中添加一个空格,不幸的是。

标签: excel vba adodb


【解决方案1】:

尝试在扩展属性中使用双引号

   "; Extended Properties=""" & str_hdr & "HDR=Yes;"";"

假设您在名为 final_song 的工作表上有数据

Option Explicit

Sub QuerySQL()

    Const SQL = "SELECT TOP 5 [F1],[F2],[F3] FROM [final_song$]"

    Dim oCn As New ADODB.Connection, oRs As New ADODB.Recordset
    Dim str_conn As String, str_prop As String
    Dim wb As Workbook, i As Integer, msg As String
    
    ' connection parameters
    If Application.Version < 12 Then
        oCn.Provider = "Microsoft.Jet.OLEDB.4.0;"
        str_prop = "Excel 8.0;"
    Else
        oCn.Provider = "Microsoft.ACE.OLEDB.12.0;"
        str_prop = "Excel 12.0;"
    End If

    ' data source
    Set wb = ThisWorkbook
    str_conn = "Data Source=" & wb.FullName & "; " & _
               "Extended Properties=""" & str_prop & "HDR=No;"";" ' no header to use F1,F2,F3

    'Debug.Print str_conn
    oCn.ConnectionString = str_conn
    
    ' open connection
    On Error Resume Next
    oCn.Open
    If oCn.Errors.Count > 0 Then
        msg = oCn.ConnectionString & vbCrLf
        For i = 1 To oCn.Errors.Count
            msg = msg & vbCrLf & oCn.Errors.Item(i-1).Description
        Next
        MsgBox msg, vbCritical, "Connection Error"
        Exit Sub
    End If
    On Error GoTo 0
    'Debug.Print Join(Split(oCn.ConnectionString, ";"), vbCrLf)
    
    ' execute
    Set oRs = oCn.Execute(SQL)
    MsgBox oRs.GetString, vbInformation

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多