首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

动态生成sql有关问题~求救

2012-02-07 
动态生成sql问题~!求救!有4个控件,然后如果控件上有值,就生成sql语句,没有则不生成该控件的查询!我试图这

动态生成sql问题~!求救!
有4个控件,然后如果控件上有值,就生成sql语句,没有则不生成该控件的查询!
我试图这么做

Private   Sub   combo1_Click()
ab=ab   &   "   编号= ' "   &   combo1.text   &   " '   "
End   Sub
private   sub   combo2_click()
ab=ab   &   "   姓名= ' "   &   combo2.text   &   " '   "
end   sub
private   sub   command1_click()
dim   sql   as   string
sql= "select   *   from   table   where   "   &   ab

但是如果搜索一个还可以,搜索2个就需要and   连接,如果每个都+and则第一个条件就多出一个and
请问各位高人怎么办呢?

[解决办法]
1.combo下面没有代码
2
private sub command1_click()
dim sql as string,sand as string
sql= "select * from table where "

if combox1.txt <> " " then
sql=sql & " name = ' " &combox1.text & ' "
band= " and "
end if

if combox2.txt <> " " then
sql =sql & band & " ID= ' " & combox2.text & " ' "
band= " and "
end if

。。。。

够清楚么
[解决办法]
private sub command1_click()
dim strcbo1 as string
dim strcbo2 as string
dim sql as string

if combo1.text= " " then
strcbo1= " "
else
strcbo1= " and 编号= ' "& combo1.text & " ' "
end if

if combo2.text= " " then
strcbo2= " "
else
strcbo2= " and 姓名= ' "& combo2.text & " ' "
end if


sql= "select * from tablename where 1=1 " & strcbo1 & strcbo2

热点排行