我无法弄清楚这段代码的哪一部分写得不正确。
在我的查询字符串中,我正在尝试收集sFrom和sTo值之间的一系列记录。基本上,我想这样做,以便从我的数据库中收集的所有控制数中,唯一显示的是用户输入的起始范围(sFrom)和结束范围(sTo)。< / p>
sQuery = "SELECT " & sFields & " FROM " & sTable &
" Where store_id Like 'XXX'" & sFilter &
" Where control_no > sFrom and < sTo " &
order by " & sOrderBy
答案 0 :(得分:2)
此:
" Where control_no > sFrom and < sTo "
应该是这样的:
" Where control_no >= " & sFrom & " and control_no <= " & sTo
(当然假设sFrom和sTo是用来自用户的数字输入填充的字符串变量)
答案 1 :(得分:1)
您不能在&lt;
之前删除control_nosQuery = "SELECT " & sFields & " FROM " & sTable & " Where store_id Like 'XXX'" & sFilter & " Where control_no > sFrom and control_no < sTo " & order by " & sOrderBy
答案 2 :(得分:1)
Like 'XXX'" & sFilter
中,X
'和下一个字符串之间没有空格。sFrom
和sTo
是文字字符串。您可能打算将它们用作变量。control_no
和sTo
之间没有比较。sFilter
与周围的SQL之间没有声明。order by
之前没有双引号。现在,把它们放在一起:
sQuery = "SELECT " & sFields & " FROM " & sTable & _
" WHERE store_id LIKE 'XXX'" & _
" AND " & sFilter & _
" AND control_no > " & sFrom & _
" AND control_no < " & sTo & _
" ORDER BY " & sOrderBy
答案 3 :(得分:1)
调试此方法的最佳方法是查看生成的SQL语句是什么,并从那里开始工作。
您目前评估的内容类似于
SELECT field, field2 FROM table Where store_id Like 'XXX'FilterString Where control_no > sFrom and < sTo order by field1
sFilter
包含是非常错误的,但这取决于确切的值。sFrom
和sTo
作为文字包含在内,未评估(请参阅tcarvin's answer)WHERE
子句的每个部分都需要完整和完整。您无法省略要检查的字段(请参阅Darrel's answer)