为什么在vba代码中使用SQL会收到错误消息?

为什么在vba代码中使用SQL会收到错误消息?,sql,vba,Sql,Vba,你能帮我运行这个代码吗?我犯了一个错误 令牌无效 因为您没有向sql添加换行符,所以至少应该在openig之后或关闭之前添加空间。 您将concats发布到某个乱七八糟的地方的sql。 为了便于阅读,我建议: Dim sql as String sql = sql & vbCRLF & "SELECT concat(q.CHSeries, q.CHNumber) as Chassino,q.Model, " sql = sql & vbCRLF & "Case q

你能帮我运行这个代码吗?我犯了一个错误

令牌无效


因为您没有向sql添加换行符,所以至少应该在openig之后或关闭之前添加空间。 您将concats发布到某个乱七八糟的地方的sql。 为了便于阅读,我建议:

Dim sql as String
sql = sql & vbCRLF & "SELECT concat(q.CHSeries, q.CHNumber) as Chassino,q.Model, "
sql = sql & vbCRLF & "Case q.BRANCH when '6' then 'branch 6' when '2' then 'branch 2'  Else 'XX' end Branch, "
sql = sql & vbCRLF & "q.ORDER,q.indt  DeofInvoice,  q.invn Invoice_no, q.customer Customer, "
sql = sql & vbCRLF & "(select coalesce(sum(amnt*ICVL),0) from lbr2 where pcno<>0 and q.order=orno) + (select coalesce(sum(amnt*ICVL),0) from parts2 where pcno<>0 and q.order=orno)+ "
sql = sql & vbCRLF & "(select coalesce(sum(amnt*ICVL),0) from lbr1 where pcno<>0 and q.order=orno) + (select coalesce(sum(amnt*ICVL),0) from parts1 where pcno<>0 and q.order=orno) "
sql = sql & vbCRLF & "Internal_Sales, "
sql = sql & vbCRLF & "coalesce(case q.InvoiceCredit when 'F' then  (q.RetailwithV-q.V) else -1*(q.RetailwithV-q.V)end,0)  External_Sales "
sql = sql & vbCRLF & "FROM (select VIN2, VIN3, PRDT, GNN2, orno,  indt, invn, ca30, itot, vx,cd2 "
sql = sql & vbCRLF & "FROM HEADER1 , PARTS1 WHERE BRNN=header1.GNN2 AND ORNO=header1.ORNO union select VIN2, VIN3, PRDT, GNN2, orno,  indt, invn, ca30,  itot, vx,cd2 "
sql = sql & vbCRLF & "FROM HEADER1, LBR1 WHERE BRNN=header1.GNN2 AND ORNO=header1.ORNO "
sql = sql & vbCRLF & "Union select VIN2, VIN3, PRDT, GNN2, orno, indt, invn, ca30, itot, vx,cd2 "
sql = sql & vbCRLF & "FROM HEADER2 , PARTS2 WHERE BRNN=header2.GNN2 AND ORNO=header2.ORNO union select VIN2, VIN3, PRDT, GNN2, orno, indt, invn, ca30,  itot, vx, cd2 "
sql = sql & vbCRLF & "FROM HEADER2 , LBR2 WHERE BRNN=header2.GNN2 AND ORNO=header2.ORNO) "
sql = sql & vbCRLF & "where indt ='2019-09-30'  order by branch "

    Set rs = con.Execute(sql)

啊。您能在数据库中创建一个视图吗?这是一些通过VBA呈现的讨厌的SQL。不是不可能,但做起来并不有趣。我第一眼就看出你在每个人面前都没有空位。我想你的情况也不太对劲。我习惯的语法是condition1然后result1。在外部销售和CD2将SQL存储在工作表单元格中并从中读取后,是否缺少空格?可以避免所有的连接…@Scott Holtzman你是对的,问题是在from和一个符号之前没有空格&在虚拟表q之前,因此,感谢您的提示和帮助:
Dim sql as String
sql = sql & vbCRLF & "SELECT concat(q.CHSeries, q.CHNumber) as Chassino,q.Model, "
sql = sql & vbCRLF & "Case q.BRANCH when '6' then 'branch 6' when '2' then 'branch 2'  Else 'XX' end Branch, "
sql = sql & vbCRLF & "q.ORDER,q.indt  DeofInvoice,  q.invn Invoice_no, q.customer Customer, "
sql = sql & vbCRLF & "(select coalesce(sum(amnt*ICVL),0) from lbr2 where pcno<>0 and q.order=orno) + (select coalesce(sum(amnt*ICVL),0) from parts2 where pcno<>0 and q.order=orno)+ "
sql = sql & vbCRLF & "(select coalesce(sum(amnt*ICVL),0) from lbr1 where pcno<>0 and q.order=orno) + (select coalesce(sum(amnt*ICVL),0) from parts1 where pcno<>0 and q.order=orno) "
sql = sql & vbCRLF & "Internal_Sales, "
sql = sql & vbCRLF & "coalesce(case q.InvoiceCredit when 'F' then  (q.RetailwithV-q.V) else -1*(q.RetailwithV-q.V)end,0)  External_Sales "
sql = sql & vbCRLF & "FROM (select VIN2, VIN3, PRDT, GNN2, orno,  indt, invn, ca30, itot, vx,cd2 "
sql = sql & vbCRLF & "FROM HEADER1 , PARTS1 WHERE BRNN=header1.GNN2 AND ORNO=header1.ORNO union select VIN2, VIN3, PRDT, GNN2, orno,  indt, invn, ca30,  itot, vx,cd2 "
sql = sql & vbCRLF & "FROM HEADER1, LBR1 WHERE BRNN=header1.GNN2 AND ORNO=header1.ORNO "
sql = sql & vbCRLF & "Union select VIN2, VIN3, PRDT, GNN2, orno, indt, invn, ca30, itot, vx,cd2 "
sql = sql & vbCRLF & "FROM HEADER2 , PARTS2 WHERE BRNN=header2.GNN2 AND ORNO=header2.ORNO union select VIN2, VIN3, PRDT, GNN2, orno, indt, invn, ca30,  itot, vx, cd2 "
sql = sql & vbCRLF & "FROM HEADER2 , LBR2 WHERE BRNN=header2.GNN2 AND ORNO=header2.ORNO) "
sql = sql & vbCRLF & "where indt ='2019-09-30'  order by branch "

    Set rs = con.Execute(sql)