问题在ADO-excel,SQL查询

ADOD中的SQL查询工作正常,只有在ADODC1表中存在Sect列。
如果ADODC1表单没有Sect列,则会引发错误。
请纠正我下面的查询…

 strSQL = "Select Name,Dept,IIF(ISNULL(Sect),'',Sect) AS Sect from [Adodc1$] UNION Select Name,Dept,Sect from [Adodc2$];" 

如果Sect列不在ADODC1表中,则需要将结果清空。

你的意思是“需要结果为空”。 如果你错过了错误,你会得到一个封闭的logging集。 这是“空的”吗?

我怀疑这是你需要做的(伪代码):

 On Error Resume Next strSQL = "Select Name,Dept,IIF(ISNULL(Sect),'',Sect) AS Sect from [Adodc1$]" & _ " UNION Select Name,Dept,Sect from [Adodc2$];" Set rs = cn.Execute(strSQL) If rs.State <> adStateOpen Then strSQL = "Select Name,Dept,NULL AS Sect from [Adodc1$]" & _ " UNION Select Name,Dept,Sect from [Adodc2$];" Set rs = cn.Execute(strSQL) If rs.State <> adStateOpen Then ' Deeper problems...