# 组合查询,该怎么处理

www.myexceptions.net  网友分享于：2013-02-14  浏览：6次

'命名数组
Dim   tushu(0-8),sql(0   To   8),   sqlstr2   As   String
Dim   j   As   Boolean
tushu(0)   =   "书号 "
tushu(1)   =   "书名 "
tushu(2)   =   "出版社 "
tushu(3)   =   "作者 "
tushu(4)   =   "册数 "
tushu(5)   =   "条码 "
tushu(6)   =   "适用范围 "
tushu(7)   =   "购买日期 "
tushu(8)   =   "出版日期 "

j   =   False
sqlstr2   =   "select   *   from   图书信息   where   "
sql(0)   =   shuhao.Text
sql(1)   =   shuming.Text
sql(2)   =   chubanshe.Text
sql(3)   =   zuozhe.Text
sql(4)   =   cheshu.Text
sql(5)   =   tiaoma.Text
sql(6)   =   shiyongfw.Text
sql(7)   =   goumairiqi.Text
sql(8)   =   goumairiqi2.Text

'组合查询用户输入的条件
If   sql(0)   =   " "   And   sql(1)   =   " "   And   sql(2)   =   " "   _
And   sql(3)   =   " "   And   sql(4)   =   " "   And   sql(5)   =   " "   _
And   sql(6)   =   " "   And   sql(7)   =   " "   And   sql(8)   =   " "   Then
sqla   =   "select   *   from   图书信息 "       '如果没有输入条件,则显示全部图书
Else
sqla   =   sqlstr2
For   i   =   0   To   8

If   sql(i)   <>   " "   Then
If   j   =   False   Then
sqla   =   sqla   +   tushu(i)   +   "   =   ' "   +   sql(i)   +   " ' "
Else
sqla   =   sqla   +   "   and   "   +   tushu(i)   +   "   =   ' "   +   sql(i)   +   " '   "
j   =   True
End   If
Else
End   If
Next
End   If

------解决方案--------------------
If j = False Then
sqla = sqla + tushu(i) + " = ' " + sql(i) + " ' "
j = True ××××××××××××××××××××这个！
Else
sqla = sqla + " and " + tushu(i) + " = ' " + sql(i) + " ' "
End If