我在WHERE行的'='符号周围出现错误,我无法弄明白

时间:2018-06-13 17:16:26

标签: excel-vba vba excel

Set rs = conn.Execute("SELECT unitcp_qual_var.instance_id," & _
                          "*FROM qualified_data INNER JOIN unitcp_qual_var ON qualified_data.unit_id = unitcp_qual_var.unit_id AND qualified_data.baseline_id = unitcp_qual_var.baseline_id AND qualified_data.qualified_id = unitcp_qual_var.qualified_id" & _
                          "WHERE (qualified_data.unit_id = 19419) AND (unitcp_qual_var.port_id = 1) AND (qualified_data.unit_id = 19419) AND (qualified_data.baseline_id = 2) AND (qualified_data.mdu_id = 622) AND (unitcp_qual_var.instance_id = 2);")
                          ' "ORDER BY qualified_data.das_time Asc;")

1 个答案:

答案 0 :(得分:1)

这是一个快速重写,在您的ON条件和WHERE条款之间添加所需的空格,并合并ORDER BY。我正在添加一个变量来保存SQL字符串,以便可以将其转储到您的立即/调试窗口,这样您就可以看到输出和测试,而不是处理棘手的错误消息:

strSQL="SELECT unitcp_qual_var.instance_id, *" & _
  " FROM qualified_data INNER JOIN unitcp_qual_var ON qualified_data.unit_id = unitcp_qual_var.unit_id AND qualified_data.baseline_id = unitcp_qual_var.baseline_id AND qualified_data.qualified_id = unitcp_qual_var.qualified_id" & _
  " WHERE (qualified_data.unit_id = 19419) AND (unitcp_qual_var.port_id = 1) AND (qualified_data.unit_id = 19419) AND (qualified_data.baseline_id = 2) AND (qualified_data.mdu_id = 622) AND (unitcp_qual_var.instance_id = 2)" & _
  " ORDER BY qualified_data.das_time Asc;"

'dump out to immediate window/pane (View>>Immediate  to activate)
debug.print strSQL

'Run the sql into rs recordset
Set rs = conn.Execute(strSQL)