VBA代码语法错误运行时错误'-2147217900(80040e14)'没有注释

问题描述 投票:0回答:1

遇到了我无法克服的错误

出发
Sub Step2_CreateReportSheetWithConn()
    Dim ws_from$, ws_to$, group_name$
    ws_from = "Data"
    ws_to = "Result"

    Set conn = CreateObject("ADODB.Connection")
    conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ThisWorkbook.FullName& ";Extended Properties='Excel 12.0 Xml;HDR=YES;IMEX=1';"
    conn.Open

    Dim aggQuery$
    aggQuery = testQ()

    Set rs = CreateObject("ADODB.Recordset")
    ThisWorkbook.Sheets(ws_from).Range("A:B,J:L").NumberFormat = "0" 
    rs.Open aggQuery, conn, 1, 3 ' Run Time error '-2147217900(80040e14)' with no comments    
    If Not rs.EOF Then
        ThisWorkbook.Sheets(ws_to).Cells(6, 1).CopyFromRecordset rs
        rs.Close
    End If
        
    conn.Close
    Set rs = Nothing
    Set conn = Nothing
End Sub
Function testQuery()
''' Returns result query from parts combine '''
    
    q_1 = _
        "SELECT [seg], [inn], SUM([kg]) " & _
        "FROM [Данные$] " & _
        "GROUP BY [seg], [inn] "
            
    q_2 = _
        "SELECT [seg], COUNT([inn]), SUM([kg]) " & _
        "FROM [Данные$] " & _
        "GROUP BY [seg] "

    testQuery = _
        "SELECT " & _
            "c.[seg], c.[sku]," & _
       "COUNT(c.[inn]) / COUNT(b.[inn]) as DistrNV, " & _
            "SUM(a.[kg]) / SUM(b.[kg]) as DistrV, " & _
        "FROM [Данные$] AS c " & _
        "LEFT JOIN (" & q_1 & ") AS a ON c.[inn] = a.inn AND c.[seg] = a.seg " & _
        "LEFT JOIN (" & q_2 & ") AS b ON c.[seg] = b.seg " & _
        "GROUP BY c.[seg], c.[sku]"
End Function

据我所知,这是问题所在:

        "LEFT JOIN (" & q_1 & ") AS a ON c.[inn] = a.inn AND c.[seg] = a.seg " & _
        "LEFT JOIN (" & q_2 & ") AS b ON c.[seg] = b.seg " & _

如果我删除1行2行,就不会出现错误。

我尝试交换键,交换“JOIN”行。 请求的其他部分已成功完成,但此时出现了一些麻烦。

sql excel vba join left-join
1个回答
0
投票

两个连接的语法是:

(c LEFT JOIN q_1 AS a ON c.inn=a.inn AND c.inn=a.inn) LEFT JOIN q_2 AS b ON c.seg= b.seg;

检查是否需要括号。

© www.soinside.com 2019 - 2024. All rights reserved.