selectlogging时SQL语句中的语法错误

我想在Excel中运行以下语句:

Dim myquery As String myquery = "select * from batchinfo where " + "datapath='" + dpath + "' and analystname='" + aname + "' and reportname='" + rname + "' and batchstate='" + bstate + "'" rs.Open myquery, cn, adOpenKeyset, adLockOptimistic, adCmdTable ' deleting batchinfo and from other tables with rowid if duplicate exists If Not rs.EOF Then RowId_batchinfo = rs.Fields("rowid") cn.Execute "delete from batchinfo where rowid=" + RowId_batchinfo cn.Execute "delete from calibration where rowid='" + RowId_batchinfo + "'" cn.Execute "delete from qvalues where rowid='" + RowId_batchinfo + "'" End If With rs .AddNew ' create a new record ' add values to each field in the record .Fields("datapath") = dpath .Fields("analysistime") = atime .Fields("reporttime") = rtime .Fields("lastcalib") = lcalib .Fields("analystname") = aname .Fields("reportname") = rname .Fields("batchstate") = bstate .Fields("instrument") = Instrument .Fields("macrowriter") = Environ$("computername") .Update ' stores the new record capture_id = .Fields(0) End With ' get the last id 'MsgBox capture_id rs.Close 

但在rs.Open行我得到一个错误:

 incorrect syntax near the keyword 'select' 

我究竟做错了什么?

这里是sql语句的样子:

 "select * from batchinfo where datapath='F:\MassHunter\DATA\44612_PAIN\QuantResults\44612.batch.bin' and analystname='MLABS\nalidag' and reportname='MLABS\nalidag' and batchstate='Processed'" 

我想你的最后一个选项adCmdTable是不正确的。 您可能需要adCmdText

adCmdTable适用于传递的文本只是表名的情况。 既然你正在给一个SQL语句, adCmdText是更合适的。