空条件查询问题,非常急,在线等
Adodc1.RecordSource = "select sum(消费总金额) from data where 年='" & Text1.Text & "' and 月='" & Text2.Text & "' and 日='" & Text3.Text & "' and 消费人='" & Combo1.Text & "' and 消费地点='" & Combo2.Text & "'"
上面代码所示
我使用了ADO控件和SQL语句
条件在TEXT1/TEXT2/TEXT3里
我想知道如何在TEXT1(或TEXT2或TEXT3)里输入*时
代表条件为空
如何实现
问题点数:20、回复次数:8Top
1 楼zqfleaf(动力港湾)回复于 2003-06-03 16:48:12 得分 10
strsql = "select sum(消费总金额) from data where 1=1 "
if text1.text<> "*" then
strsql = strsql & " and 年='" & Text1.Text & "'"
end if
if text2.text<>"*" then
strsql = strsql & " and 月='" & Text1.Text & "'"
end if
if text3.text<> "*" then
strsql = strsql & " and 日='" & Text1.Text & "'"
end if
strsql = strsql & " and 消费人='" & Combo1.Text & "' and 消费地点='" & Combo2.Text & "'"
Top
2 楼victorycyz(--)回复于 2003-06-03 16:55:15 得分 10
dim strsql as string
dim haswhere as boolean
strsql="select sum(消费总金额) from data "
if text1<>"" then '注意,不要约定“*”代表空,多此一举。还不专业。
strsql=strsql & "where 年='" & text1 & "' "
haswhere=true
end if
if text2<>"" then
if haswhere then
strsql=strsql & "and 月='" & text2 & "' "
else
strsql=strsql & " where 月='" & text2 & "'"
end if
end if
if text3<>"" then 略
另,你的表为什么建得这么烦琐?年月日还要分成三个字段呀?还用的是字符型?真想不通。
Top
3 楼hedane(有球必应)回复于 2003-06-03 16:59:35 得分 0
用“*”表示空啊?那最好了:将你的等号全部换为“like”。Top
4 楼bacp(风中月)回复于 2003-06-03 17:04:39 得分 0
不好意思~
字段建的一点问题~
但是方便输入
呵呵Top
5 楼bacp(风中月)回复于 2003-06-03 17:10:06 得分 0
说句实话,上面的代码,还真有一点看不明白Top
6 楼planetike(胜哥哥)回复于 2003-06-03 17:16:27 得分 0
dim mysql as string
mysql="select sum(消费总金额) from data where 1=1"
if text1.value<>"*" then
mysql=mysql & "and 年='" & Text1.Text & "'"
end if
if text2.value<>"*"then
mysql=mysql & " and 月='" & Text2.Text & "'"
end if
if text3.value <>"*" then
mysql=mysql & " and 日='" & Text3.Text & "'"
end if
mysql=mysql & " and 消费人='" & Combo1.Text & "' and 消费地点='" & Combo2.Text & "'"
endif
Adodc1.RecordSource = mysqlTop
7 楼casta(casta)回复于 2003-06-03 17:50:07 得分 0
sql = " select sum(消费总金额) from data where 1 = 1 "
or
sql = " select sum(消费总金额) from data where 消费总金额 is not null "
后面连接相关的查询条件Top
8 楼bacp(风中月)回复于 2003-06-03 17:52:45 得分 0
接分Top



