简体   繁体   English

查询表达式VBA中的语法错误(缺少运算符)

[英]Syntax error (missing operator) in query expression VBA

I'm getting an error message when I want to run my query (syntax error (missing operator) in query expression). 我想运行查询时收到错误消息(查询表达式中的语法错误(缺少运算符))。 I don't know where the issue can be. 我不知道问题可能在哪里。

I have created a macro (VBA) which should show all manager level in my company. 我创建了一个宏(VBA),该宏应显示我公司中的所有经理级别。

The columns should be like this: 列应如下所示:

Top manager, directors, managers, team leaders, supervisors, employees. 最高经理,董事,经理,团队负责人,主管,员工。

Sub TEST()
    Application.ScreenUpdating = False

    Dim cnStr                 As String
    Dim rs                    As ADODB.Recordset
    Dim query                 As String
    Dim fileName              As String
    pom4 = "';"

    fileName = "C:\sciezka\DEU.xlsx"

    cnStr = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
            "Data Source=" & fileName & ";" & _
            "Extended Properties=Excel 12.0"


    query = "SELECT L1.[DEU1$].[Supervisor ID] AS N0, L1.[DEU1$].[Emplid] AS N1, L2.[DEU1$].[Emplid] AS N2, L3.[DEU1$].[Emplid] AS N3, " & _
            "L4.[DEU1$].[Emplid] AS N4, L5.[DEU1$].[Emplid] AS N5, L6.[DEU1$].[Emplid] AS N6, L7.[DEU1$].[Emplid] AS N7 FROM [DEU1$] L1 " & _
            "LEFT JOIN [DEU1$] L2 ON (L1.[Emplid]=L2.[Supervisor ID]) LEFT JOIN [DEU1$] L3 ON (L2.[Emplid]=L3.[Supervisor ID]) " & _
            "LEFT JOIN [DEU1$] L4 ON (L3.[Emplid]=L4.[Supervisor ID]) LEFT JOIN [DEU1$] L5 ON (L4.[Emplid]=L5.[Supervisor ID]) " & _
            "LEFT JOIN [DEU1$] L6 ON (L5.[Emplid]=L6.[Supervisor ID]) LEFT JOIN [DEU1$] L7 ON (L6.[Emplid]=L7.[Supervisor ID]) WHERE L1.[Supervisor ID] LIKE '%15981%';"

    Set rs = New ADODB.Recordset

    rs.Open query, cnStr, adOpenUnspecified, adLockUnspecified

    Cells.Clear

    Dim cell As Range, i      As Long

    With Range("A16").CurrentRegion
        For i = 0 To rs.Fields.Count - 1
            .Cells(1, i + 1).Value = rs.Fields(i).Name
        Next i
        Range("A17").CopyFromRecordset rs
        .Cells.Select
        .EntireColumn.AutoFit
    End With

    rs.Close
    Application.ScreenUpdating = True
End Sub

I think you just need to add some parentheses as well as not using both the alias name and the actual table name: 我认为您只需要添加一些括号,而不要同时使用别名和实际的表名:

query = "SELECT L1.[Supervisor ID] AS N0, L1.[Emplid] AS N1, L2.[Emplid] AS N2, L3.[Emplid] AS N3, " & _
        "L4.[Emplid] AS N4, L5.[Emplid] AS N5, L6.[Emplid] AS N6, L7.[Emplid] AS N7 FROM ((((([DEU1$] L1 " & _
        "LEFT JOIN [DEU1$] L2 ON L1.[Emplid] = L2.[Supervisor ID]) " & _
        "LEFT JOIN [DEU1$] L3 ON L2.[Emplid] = L3.[Supervisor ID]) " & _
        "LEFT JOIN [DEU1$] L4 ON L3.[Emplid] = L4.[Supervisor ID]) " & _
        "LEFT JOIN [DEU1$] L5 ON L4.[Emplid] = L5.[Supervisor ID]) " & _
        "LEFT JOIN [DEU1$] L6 ON L5.[Emplid] = L6.[Supervisor ID]) " & _
        "LEFT JOIN [DEU1$] L7 ON L6.[Emplid]=L7.[Supervisor ID];"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 查询表达式“ PopID =”中的VBA语法错误(缺少运算符) - VBA Syntax error (missing operator) in query expression 'PopID =' 查询表达式中的语法错误(缺少运算符) - VBA 和 Access - syntax error (missing operator) in query expression - VBA and Access 查询表达式“”中的语法错误(缺少运算符) - Syntax error (missing operator) in query expression “” 错误:查询表达式 VBA 中缺少运算符 - Error: Missing Operator in Query Expression VBA SQL / Excel / VBA - UPDATE查询:'查询表达式中的语法错误(缺少运算符)' - SQL/Excel/VBA - UPDATE query: 'Syntax Error (missing operator) in query expression' 多个内部联接时查询表达式VBA中的语法错误(缺少运算符) - Syntax error (missing operator) in query expression VBA while multiple inner join Excel导入VBA:查询表达式中的运行时3075语法错误(缺少运算符) - Excel Import VBA: Run-time 3075 Syntax error (missing operator) in query expression 查询表达式中的Microsoft ODBC Excel驱动程序语法错误(缺少运算符) - Microsoft ODBC Excel Driver Syntax Error (missing operator) in query expression excel vba中的语法错误(缺少运算符) - Syntax error (Missing operator) in excel vba 链接到Excel时语法错误(缺少运算符),查询运行正常 - Syntax error (missing operator) when link to Excel, query runs ok
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM