VB中查询access。能否在查询结果中,再次查询?能否和几个check控件联动?比如3个check,根据不同的选择情

VB中查询access。能否在查询结果中,再次查询?能否将多个结果求并集或交集?能否和几个check控件联动?比如3个check,根据不同的选择情况得到不同的查询结果,共8种情况,写8个select太麻烦了。
最好带详细的例子和语句,谢了
if我用过,真假非别对应两个where的条件,但提示if未定义,请给个详细的语句例子。估计是我的语法不对。
combo,text,label的我会,我要根据check的值来做不同的查询。
再次查询是一个问题,check是另一个问题,主要是我不知道具体的语法,select * from ss where if(check1.value,name=1,name=2)就check选中,查询name=1,否则查询name=2,但语法错误。还有select * from ss where name=1 if(check1.value,or,and) name=2。check选中,查询用 or,否则用and。
OptionButton不能同时选择多个,而且要是写那么多个select的话,我还不如用check。
用union是否能解决这个问题呢?

access数据库可以在查询结果中再次查询,如(注意括号):
Sql = "select * from 领料单 where true and 图号 ='" & Text1.Text & "'"
If rqcx.Value = 1 Then Sql = "select * from (" & Sql & ") where 日期=#" & Text8.Text & "#"
Sql = "select * from (" & Sql & ") order by 图号"

以下针对楼上:
一条语句过多地使用IIf看起来非常不爽,语句过长看起来晦涩不说,添加不必要的条件(如1=1(实际为true)、1=2(实际为false))也是会影响查询速度的,特别是大型数据库进行复杂运算的时候,这种影响将非常明显(当然了,对三五万条记录的小型数据库来说差别不是很明显,也可能会以为没影响)。
可用If连接:
Dim myWhere'以下语句未判断Text中是否有单引号、是否为日期等情况
If Check1.Value Then myWhere = myWhere & " and 图号 ='" & Text1.Text & "' "
If Check2.Value Then myWhere = myWhere & " and 产品 ='" & Combo1.Text & "' and 名称 ='" & Combo2.Text & "' "
If rqcx.Value Then myWhere = myWhere & " and 日期 =#" & Text8.Text & "# "
If ll.Value + gf.Value + lf.Value Then
myWhere = myWhere & " and ("
If gf.Value Then myWhere = myWhere & " or 备注 = '工废'"
If lf.Value Then myWhere = myWhere & " or 备注 = '料废'"
If ll.Value Then myWhere = myWhere & " or 备注 is null"
myWhere = Replace(myWhere, "( or", "(") & ")"
End If
If myWhere <> "" Then myWhere = " where " & Mid(myWhere, 5)'去掉第一个and
Sql = "select * from 领料单 " & myWhere
温馨提示:答案为网友推荐,仅供参考
第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
相似回答
大家正在搜