首先先看一条学过的普通查询语句txtSQL=“select * from table where 字段名 操作符 ' ” & 要查询的内容 & "'",这样的一条查询语句查询到的结果是我们事先知道要返回什么结果,即字段名是已知的。然后当我们想知道不同条件的结果各是什么时,这是这种预先设定好的查询语句便不合时宜了。好比我们进行给三个数判定大小,并不知道具体是哪三个数,这时便用到了inputbox这个空间,同理,组合查询便是将所有的可能组合用comboBox控件列出来便可以进行查询了(如图所示,并和普通的查询语句进行比对)
通过第一步分析已经知道什么是组合查询以及组合查询的便利之处,接下来的问题便是在如何将所选的条件一一进行对应变成计算机能过读懂的语言之前,还要再次弄清图中每个comboBox框在txtSQL查询语句中所起的作用,这样便会发现so easy!(如图所示)
看懂这个图之后,便会发现如果直接将数据库中的字段名如cardno、status、teacher、offtime等直接放入列表框另计算机读懂是完全没有问题的,只需将三条查询语句按照一定的逻辑关系排列起来便可。而我们所做的系统除了性能和功能还要考虑界面友好,用这些英文很容易给使用者造成误解,所以我们在另用户进行选择的时候应该使他们最熟悉的中文,这便涉及到了如何进行转换的问题。方法有很多,介绍一种便是利用含有select case语句的一个自定义的函数。例:
Private Function Filename(i As String) As String Select Case i Case "卡号" Filename = "cardno"
End Select End Function
</pre><strong><span style="font-size:48px; background-color:rgb(255,102,102)"> WHY</span></strong><p></p><p><strong><span style="font-size:18px"> 刚刚接触组合查询的时候感觉难,太难,完全搞不清这三个comboBox到底是做什么用的,结合我们学过的简单的语句和该组合要实现的功能,慢慢分析,思绪理清了,结果便不远了。</span></strong></p><p><strong><span style="font-size:48px; background-color:rgb(255,102,102)">NOTICE</span></strong></p><p><strong><span style="font-size:18px"> 各种带条件的查询语句,难得不是理解,而是各种书写规范,如空格和符号的使用,因为开始不能理解只能机械的模仿。就会发现因为一个空格而全局都受到影响,例如:</span></strong></p><p><strong><span style="font-size:18px"> </span></strong></p><pre name="code" class="html"> txtSQL = "select * from Line_Info where "
这条语句很简单却统摄着接下来的所有组合条件,而where后面的空格很容易被忽略。其空格的原因是很简单,便是起到一个占位符的作用
以机房收费系统的上机查询为例:
Private Function Filename(i As String) As String Select Case i Case "卡号" Filename = "cardno" Case "姓名" Filename = "studentName" Case "上机日期" Filename = "ontime" Case "下机日期" Filename = "offtime" Case "消费金额" Filename = "consume" Case "余额" Filename = "cash" Case "备注" Filename = "status" Case "或" Filename = "or" Case "与" Filename = "and" End Select End Function Private Sub cmdquery_Click() Dim mrc As ADODB.Recordset Dim txtSQL As String Dim MsgText As String If txt1.Visible = True Then If Trim(cmbfield1.Text) = "" Or Trim(cmbsign1.Text) = "" Or Trim(txt1.Text) = "" Then MsgBox "请输入完整的查寻条件", vbOKOnly, "警告" Exit Sub End If Else If txt1.Visible = False And dt1.Visible = True Then If Trim(cmbfield1.Text) = "" Or Trim(cmbsign1.Text) = "" Or dt1.Value = "" Then MsgBox "请输入完整的查寻条件", vbOKOnly, "警告" Exit Sub End If End If End If txtSQL = "select * from Line_Info where " If txt1.Visible = True Then txtSQL = txtSQL & Filename(cmbfield1.Text) & Trim(cmbsign1.Text) & "'" & Trim(txt1.Text) & "'" Else txtSQL = txtSQL & Filename(cmbfield1.Text) & Trim(cmbsign1.Text) & "'" & Trim(dt1.Value) & "'" End If If Trim(cmdrelation1.Text <> "") Then If txt2.Visible = True Then If Trim(cmbfield2.Text) = "" Or Trim(cmdsign2.Text) = "" Or Trim(txt2.Text) = "" Then MsgBox "你选择了第一个组合关系,请将第二组查询条件补充完整", vbOKOnly, "警告" Exit Sub Else txtSQL = txtSQL & Filename(cmdrelation1.Text) & " " & Filename(cmbfield2.Text) & Trim(cmdsign2.Text) & "'" & Trim(txt2.Text) & "'" End If Else If Trim(cmbfield2.Text) = "" Or Trim(cmdsign2.Text) = "" Or Trim(dt2.Value) = "" Then MsgBox "你选择了第一个组合关系,请将第二组查询条件补充完整", vbOKOnly, "警告" Exit Sub Else txtSQL = txtSQL & Filename(cmdrelation1.Text) & " " & Filename(cmbfield2.Text) & Trim(cmdsign2.Text) & "'" & Trim(dt2.Value) & "'" End If End If End If If Trim(cmdrelation2.Text <> "") Then If txt3.Visible = True Then If Trim(cmbfield3.Text) = "" Or Trim(cmbsign3.Text) = "" Or Trim(txt3.Text) = "" Then MsgBox "你选择了第一个组合关系,请将第二组查询条件补充完整", vbOKOnly, "警告" Exit Sub Else txtSQL = txtSQL & Filename(cmdrelation2.Text) & " " & Filename(cmbfield3.Text) & Trim(cmbsign3.Text) & "'" & Trim(txt3.Text) & "'" End If Else If Trim(cmbfield3.Text) = "" Or Trim(cmbsign3.Text) = "" Or Trim(dt3.Value) = "" Then MsgBox "你选择了第一个组合关系,请将第二组查询条件补充完整", vbOKOnly, "警告" Exit Sub Else txtSQL = txtSQL & Filename(cmdrelation2.Text) & " " & Filename(cmbfield3.Text) & Trim(cmbsign3.Text) & "'" & Trim(dt3.Value) & "'" End If End If End If Set mrc = ExecuteSQL(txtSQL, MsgText) With mflg1 .Rows = 1 .CellAlignment = 4 On Error GoTo P .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) = "备注" Do While Not mrc.EOF .Rows = .Rows + 1 .CellAlignment = 4 .TextMatrix(.Rows - 1, 0) = mrc.Fields(1) .TextMatrix(.Rows - 1, 1) = mrc.Fields(3) .TextMatrix(.Rows - 1, 2) = mrc.Fields(6) .TextMatrix(.Rows - 1, 3) = mrc.Fields(7) .TextMatrix(.Rows - 1, 4) = mrc.Fields(8) .TextMatrix(.Rows - 1, 5) = mrc.Fields(9) .TextMatrix(.Rows - 1, 6) = mrc.Fields(11) .TextMatrix(.Rows - 1, 7) = mrc.Fields(12) .TextMatrix(.Rows - 1, 8) = mrc.Fields(13) mrc.MoveNext Loop autocolwidth Me, mflg1 End With mrc.Close End Sub