第1个回答 2010-11-06
由你的提问可以看出你自己已经是有思路的了,再坚持自己思考,你一定能解决这个问题的。
给点我的思路参考:
无需写八个select,写一个就够。在这个select语句中,根据哪个check被选中,对应if checkbox1 is true
来使对应的查询条件生效即可。
补充:
combo,text,label你都会了,根据check的值来做不同的查询是一样的道理。
你已经接近解决问题的状态了,给你思路好过给你代码,你懂的。
不是“在查询结果中,再次查询“,而是在你选中哪个checkbox时再次执行checked事件,对应执行SQL语句,来达到你查询不同组合的查询结果的目的。
注意在每个checkbox的checked事件都要select语句.
哦,你的select有问题。
if判断应该在外面,来控制select语句的不同情况。如:
select * from ss where
If Check1.Value = 1 Then
name=1
else
name=2
end if
SQL语句应该这样写的,参考下。
--------------------------------
你是用adodc来查询的吗?下面代码给你参考下:(注意双引号的使用)
Dim TmpSource As String
TmpSource = "Select LogId as 编号,LogDate as 日期,LogTime as 时间,UserName as 操作用户"
TmpSource = TmpSource & ",Title as 标题,Body as 内容 from SysLog Where LogDate>='" & tmpFromTime & "' and LogDate<='" & tmpToTime & "'"
If dcboEventType.Text <> "" Then
TmpSource = TmpSource & " And LogType='" & dcboEventType.Text & "'"
End If
If dcboEventUser.Text <> "" Then
TmpSource = TmpSource & " And UserName='" & dcboEventUser.Text & "'"
End If
TmpSource = TmpSource & "Order by LogId"
' MsgBox TmpSource
Adodc1.RecordSource = TmpSource
Adodc1.Refresh本回答被提问者和网友采纳
第2个回答 2010-11-06
你的这个情况建议使用视图比较合理。
如果你使用check控件,不好操作,建议使用OptionButton控件,只能使用IF语句:
Private Sub Option1_Click(Index As Integer)
If Option1(0).Value = True Then
'选择第一个单选钮控件代码
ElseIf Option1(1).Value = True Then
'选择第二个单选钮控件代码
ElseIf Option1(2).Value = True Then
'选择三个单选钮控件代码
End If
End Sub
注意:IF语句不能厥套在from语句中
第3个回答 2010-11-08
等了几天也没个正经的答案,难道我问的不明白吗?看到一个iif的例子我恍然大悟,我自己完成了。用的是iif。具体如下
SQL = "select * from 领料单 where 1 = 1 " + IIf(Check1.Value, "and 图号 ='" & Text1.Text & "' ", "") + IIf(Check2.Value, "and 产品 ='" & Combo1.Text & "' and 名称 ='" & Combo2.Text & "' ", "") + IIf(rqcx.Value, "and 日期 ='" & Text8.Text & "' ", "") + "and (" + IIf(gf.Value, "备注 = '工废'", "1 = 2") + " or " + IIf(lf.Value, "备注 = '料废'", "1 = 2") + " or " + IIf(ll.Value, "备注 is null", "1 = 2") + " or " + IIf(ll.Value + gf.Value + lf.Value, "1 = 2)", "1 = 1)")
功能:“图号”和“产品(名称)”不能同时选,其他选项可以任意组合去查询。
虽然有点长,但其实就是一句select。后面每个iif运算后,得出一个带引号的字符串,再用“+”把一段一段的字符串连接起来的。不管几个check的值怎样,SQL的值都是一个完整的select语句。
我以前的错误是iif中的字符串没加引号。而且直接把iif放到select语句内部去运算了。应该是运算完了再组成select语句。
如果有什么可以优化的地方或更好的方法请指出。
第4个回答 2010-11-06
这是我做的一个多项查询例子
添加四个comol,一个Adodc,一个DataGrid控件
Private Sub Command1_Click()
Adodc1.CommandType = adCmdText
Adodc1.RecordSource = "select * from ss where 国籍 like '" & Combo1.Text & "' and 性别 like '" & Combo2.Text & "'and 年龄段 like '" & Combo3.Text & "'and 百分比 like '" & Combo4.Text & "'"
Adodc1.Refresh
If Adodc1.Recordset.RecordCount = 0 Then
MsgBox "无数据"
Else
Set DataGrid1.DataSource = Adodc1
DataGrid1.Refresh
End If
End Sub
Private Sub Form_Load()
Adodc1.ConnectionString = "provider=microsoft.jet.oledb.4.0;data source=aa.mdb"
End Sub