3,组合查询:设计到“操作员”的三个权限,也就是实现三个窗体功能,做出一个来剩下两个粘贴复制再修改一下就OK了。一开始觉得很简单就按着自己的想法做,但是试了又试怎么也实现不了。后来了解到,要想实现这个组合查询,需要一个重要属性——Tag
三个组合查询分别是:学生基本信息维护;学生上机统计信息查询;操作员工作记录。
它们的窗体设计一样,如下图
以学生基本信息维护为例:
字段名:
操作符:
组合关系:
例如:第一个字段名选择“卡号”,第一个操作符选择“=”,查询内容为“1”(即查询卡号为“1”的学生的信息),第一个关系组合框选择“与”;第二个字段选择“学号”,第二个操作符选择“>”,查询内容为“2”(即查询学号为“2”的学生的信息),第二个关系组合框不做选择。
综上,查询条件为:卡号=1 与 学号>2的所有学生的信息。
所有学生的信息都存在,Student_info表中。
在写Sql查询语句时,如何将字段名组合框中所选内容表示为表中的字段名,将关系组合框中所选内容表示为逻辑运算符(And,Or)。这时候就用到了,容器Tag。先将:表中字段名、操作符、逻辑运算符存储,等到Sql查询时,释放。当然,存储时也要一一对应(例如:当窗体字段名选择为“卡号”时,则容器Tag存储“CardNo”;关系组合框选择“或”时,容器Tag存储“Or”)
具体实现,以查询第一个条件为例:
'判断第一个查询条件
If comboField1.Text = "卡号"Then '判断第一个字段名的选择,并存储
comboField1.Tag = "cardno"
End If
If comboField1.Text = "学号" Then
comboField1.Tag = "studentno"
End If
If comboField1.Text = "姓名" Then
comboField1.Tag ="studentname"
End If
If comboField1.Text = "性别" Then
comboField1.Tag = "sex"
End If
If comboField1.Text = "系" Then
comboField1.Tag ="department"
End If
If comboField1.Text = "年级" Then
comboField1.Tag = "grade"
End If
If comboField1.Text = "班级" Then
comboField1.Tag = "class"
End If
If comboOperate1.Text = "="Then '判断第一个操作符的选择,并存储
comboOperate1.Tag = "="
End If
If comboOperate1.Text = "<"Then
comboOperate1.Tag = "<"
End If
If comboOperate1.Text = ">"Then
comboOperate1.Tag = ">"
End If
If comboOperate1.Text ="<>" Then
comboOperate1.Tag ="<>"
End If
If comboAnd1.Text = "与"Then '判断第一个关系符的选择,并存储
comboAnd1.Tag = "And"
End If
If comboAnd1.Text = "或" Then
comboAnd1.Tag = "Or"
End If
查询语句为:
txtSQL ="select * from student_info where " & comboField1.Tag &comboOperate1.Tag & "'" & Trim(txtCheck1.Text) & "'" & "order by cardno"
Set mrc = ExecuteSQL(txtSQL, msgText)
(注:我这里的“order by cardno”的作用是:将查询出的信息按卡号排序)