在机房收费系统中,有好几个窗体都涉及到了组合查询,那么就以“学生上机统计信息查询”窗体为例,只有理清思路,弄清楚它的逻辑关系,我们才能够更好的完成这类窗体。
当我们对示例窗体运行一遍后,我们可以大致了解到组合框之间的关系,即
- 当只有一层查询时,没有组合关系
- 第一个下拉框和第二个下拉框选中相关内容时,为第一个组合关系,组合关系为“与”或者是“或”关系
- 三个下拉框都选中时,为第二个组合关系,组合关系为“与与”、“与或”、“或与”或者是“或或”关系
组合查询代码:
Private Sub cmdQuery_Click()
'第一个条件
Dim txtSQL As String
Dim MsgText As String
'第一个条件的数据集
Dim mrc As ADODB.Recordset
'查询sutdent_info表中的所有记录
txtSQL = "select * from student_Info where "
'判断字段的选择是否为空
If Not Testtxt(comboFiledName1.Text) Then
MsgBox "请选择字段!", vbOKOnly + vbExclamation, "警告"
comboFiledName1.SetFocus
Exit Sub
End If
'判断“操作符”下拉框的选择是否为空
If Not Testtxt(comboOperate1.Text) Then
MsgBox "请选择操作符!", vbOKOnly + vbExclamation, "警告"
comboOperate1.SetFocus
Exit Sub
End If
'判断要查询的内容是否为空
If Not Testtxt(txtContent1.Text) Then
MsgBox "请输入要查询的内容!", vbOKOnly + vbExclamation, "警告"
Exit Sub
End If
txtSQL = txtSQL & FiledName(comboFiledName1.Text) & " " & comboOperate1.Text & "'" & txtContent1.Text & "'"
'利用模版函数查看是否是组合查询第一行为空时,查询无效
'[comboOperate1.Text & "'" & txtContent1.Text & "'"]操作符&要查询的内容
'等号后边的txtSQL代表 txtSQL = "select * from student_Info where "
'开始组合查询
If Trim(comboRelation1.Text <> "") Then
'判断第一个组合关系是否选中
If Trim(comboFiledName2.Text) = "" Or Trim(comboOperate2.Text) = "" Or Trim(txtContent2.Text) = "" Then
'如果选中,判断第二行内容是否填写完整,且符合要求
MsgBox "您选择了组合关系,请输入数据之后再查询", vbOKOnly, "提示信息"
Exit Sub
Else
txtSQL = txtSQL & FiledName(comboRelation1.Text) & " " & FiledName(comboFiledName2.Text) & comboOperate2.Text & "'" & Trim(txtContent2.Text) & "'"
'将前两行的条件联系起来,完成SQL语句
End If
End If
If Trim(comboRelation2.Text) <> "" Then
'判断第二个组合关系是否选中
If Trim(comboFiledName3.Text) = "" Or Trim(comboOperate3.Text) = "" Or Trim(txtContent3.Text) = "" Then
'如果选中,判断第二行内容是否填写完整,且符合要求
Exit Sub
Else
'将第三行的条件联系起来,完成SQL语句
txtSQL = txtSQL & FiledName(comboRelation2.Text) & " " & FiledName(comboFiledName3.Text) & comboOperate3.Text & "'" & Trim(txtContent3.Text) & "'"
End If
End If
'开始进行查找
Set mrc = ExecuteSQL(txtSQL, MsgText)
'判断查询结果是否为空
If mrc.EOF Then
MSFlexGrid1.Clear
MsgBox "结果集为空!", vbOKOnly + vbExclamation, "提示"
Exit Sub
End If
With MSFlexGrid1
.Rows = 1
.CellAlignment = 9
.TextMatrix(0, 0) = "卡号"
.TextMatrix(0, 1) = "学号"
.TextMatrix(0, 2) = "姓名"
.TextMatrix(0, 3) = "性别"
.TextMatrix(0, 4) = "类型"
.TextMatrix(0, 5) = "系别"
.TextMatrix(0, 6) = "年级"
.TextMatrix(0, 7) = "班级"
.TextMatrix(0, 8) = "金额"
.TextMatrix(0, 9) = "状态"
.TextMatrix(0, 10) = "备注"
End With
'存在上机记录查询成功,填充到表格
MSFlexGrid1.Rows = mrc.RecordCount + 1
With MSFlexGrid1
.Rows = 1
While mrc.EOF = False
.Rows = .Rows + 1
.TextMatrix(.Rows - 1, 0) = mrc!cardno
.TextMatrix(.Rows - 1, 1) = mrc!studentNo
.TextMatrix(.Rows - 1, 2) = mrc!studentName
.TextMatrix(.Rows - 1, 3) = mrc!sex
.TextMatrix(.Rows - 1, 4) = mrc!Type
.TextMatrix(.Rows - 1, 5) = mrc!department
.TextMatrix(.Rows - 1, 6) = mrc!grade
.TextMatrix(.Rows - 1, 7) = mrc!Class
.TextMatrix(.Rows - 1, 8) = mrc!cash
.TextMatrix(.Rows - 1, 9) = mrc!Status
.TextMatrix(.Rows - 1, 10) = mrc!explain
mrc.MoveNext
Wend
End With
End Sub