dim tmp1 as string dim tmp2 as string ... dim strsql as stringif trim(combobox1.text)="" then tmp1="" else tmp1=" and 字段1='"& combobox1.text &"'" end if if trim(combobox2.text)="" then tmp2="" else tmp2=" and 字段2='"& combobox2.text &"'" end if ...'构造SQL语句 strsql=" select * from tablename where 1=1 " & tmp1 & tmp2 if rs.state=adstateopen then rs.close rs.open strsql,cn,adopenkeyset,adlockreadonly
dim sqlstr as string sqlstr="select * from table where 1=1"if combo1.text<>"" then sqlstr=sqlstr+" and columnname='"&combo1.text&"'" endifif combo2.text<>"" then sqlstr=sqlstr+" and columnname='"&combo2.text&"'" endif............................... 要不就是用控件数组 case 1 sqlstr=sqlstr+" and columnname='"&combo1.text&"'"case 2 sqlstr=sqlstr+" and columnname='"&combo2.text&"'"
为什么不这样写 if trim(combobox1.text)="" then tmp1="1=1" else tmp1=" and 字段1='"& combobox1.text &"'" end ifif trim(combobox2.text)="" then tmp2="1-1" else tmp2=" and 字段2='"& combobox2.text &"'" end if...如果条件某个条件没选就变成了... And 1=1 and ... ,问题就过了
sSql= "Select * From Table Where cKey='??' if 条件1 then sSql=sSql + " And 字段1='" & combo1.text & "'" if 条件2 then sSql=sSql + " And 字段2='" & combo1.text & "'" if 条件3 then sSql=sSql + " And 字段3='" & combo1.text & "'" if 条件4 then sSql=sSql + " And 字段4='" & combo1.text & "'" 关键是 Where cKey='??' 必须要添一个字段 cKey='??' 比如说 某个关键 cKey is Not Null 不过这样可能会影响速度 完全满足你的要求
sSql= "Select * From Table Where 1=1" if 条件1 then sSql=sSql + " And 字段1='" & combo1.text & "'" if 条件2 then sSql=sSql + " And 字段2='" & combo1.text & "'" if 条件3 then sSql=sSql + " And 字段3='" & combo1.text & "'" if 条件4 then sSql=sSql + " And 字段4='" & combo1.text & "'"
dim tmp2 as string
...
dim strsql as stringif trim(combobox1.text)="" then
tmp1=""
else
tmp1=" and 字段1='"& combobox1.text &"'"
end if
if trim(combobox2.text)="" then
tmp2=""
else
tmp2=" and 字段2='"& combobox2.text &"'"
end if
...'构造SQL语句
strsql=" select * from tablename where 1=1 " & tmp1 & tmp2
if rs.state=adstateopen then rs.close
rs.open strsql,cn,adopenkeyset,adlockreadonly
sqlstr="select * from table where 1=1"if combo1.text<>"" then
sqlstr=sqlstr+" and columnname='"&combo1.text&"'"
endifif combo2.text<>"" then
sqlstr=sqlstr+" and columnname='"&combo2.text&"'"
endif...............................
要不就是用控件数组
case 1
sqlstr=sqlstr+" and columnname='"&combo1.text&"'"case 2
sqlstr=sqlstr+" and columnname='"&combo2.text&"'"
StrSql = IIf(bAndNeed = True, StrSql & " AND ", StrSql & " WHERE ")
StrSql = StrSql & " s_kigen_cd ='" & A.TEXT & "'"
bAndNeed = True
End If
你参考一下我在这个贴子中的回复,应该对你的问题有帮助
http://expert.csdn.net/Expert/topic/1653/1653028.xml?temp=.7921106
谢谢上边的师傅,但我不是说要全部的条件都要!比如说我只要第一、第四呢?这样不就多了一个“and" 了吗?(我的条件是装在datacombobox里的,有好多个),查全部的条件当然没问题,但如果要挑选个别的就不知道如何写了。请教各位高手。
StrSql = StrSql & IIf(StrSql="", & " where ", " and ") & 条件
http://expert.csdn.net/Expert/topic/1653/1653028.xml?temp=.7921106
发表的东西,它是不会存在你所说的多一个and的问题的
if trim(combobox1.text)="" then
tmp1="1=1"
else
tmp1=" and 字段1='"& combobox1.text &"'"
end ifif trim(combobox2.text)="" then
tmp2="1-1"
else
tmp2=" and 字段2='"& combobox2.text &"'"
end if...如果条件某个条件没选就变成了... And 1=1 and ... ,问题就过了
if 条件1 then sSql=sSql + " And 字段1='" & combo1.text & "'"
if 条件2 then sSql=sSql + " And 字段2='" & combo1.text & "'"
if 条件3 then sSql=sSql + " And 字段3='" & combo1.text & "'"
if 条件4 then sSql=sSql + " And 字段4='" & combo1.text & "'"
关键是 Where cKey='??' 必须要添一个字段 cKey='??' 比如说 某个关键 cKey is Not Null 不过这样可能会影响速度
完全满足你的要求
if 条件1 then sSql=sSql + " And 字段1='" & combo1.text & "'"
if 条件2 then sSql=sSql + " And 字段2='" & combo1.text & "'"
if 条件3 then sSql=sSql + " And 字段3='" & combo1.text & "'"
if 条件4 then sSql=sSql + " And 字段4='" & combo1.text & "'"