## WHAT:
首先先看一條學過的普通查詢語句txtSQL=“select * from table where 字段名 操作符 ' ” ?& 要查詢的內容 & "'",這樣的一條查詢語句查詢到的結果是我們事先知道要返回什么結果,即字段名是已知的。然后當我們想知道不同條件的結果各是什么時,這是這種預先設定好的查詢語句便不合時宜了。好比我們進行給三個數判定大小,并不知道具體是哪三個數,這時便用到了inputbox這個空間,同理,組合查詢便是將所有的可能組合用comboBox控件列出來便可以進行查詢了(如圖所示,并和普通的查詢語句進行比對)

## HOW
通過第一步分析已經知道什么是組合查詢以及組合查詢的便利之處,接下來的問題便是在如何將所選的條件一一進行對應變成計算機能過讀懂的語言之前,還要再次弄清圖中每個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
~~~
WHY? 剛剛接觸組合查詢的時候感覺難,太難,完全搞不清這三個comboBox到底是做什么用的,結合我們學過的簡單的語句和該組合要實現的功能,慢慢分析,思緒理清了,結果便不遠了。NOTICE 各種帶條件的查詢語句,難得不是理解,而是各種書寫規范,如空格和符號的使用,因為開始不能理解只能機械的模仿。就會發現因為一個空格而全局都受到影響,例如:
~~~
txtSQL = "select * from Line_Info where "
~~~
這條語句很簡單卻統攝著接下來的所有組合條件,而where后面的空格很容易被忽略。其空格的原因是很簡單,便是起到一個占位符的作用
##USE
以機房收費系統的上機查詢為例:
~~~
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
~~~
## summarize
第一次只是簡單的按照找到的代碼將其實現,并沒有去探討過這是什么,為什么,通過總結,代碼的熟悉是一方面,更多的是鍛煉了總結能力和思考能力,才發現原來是這樣,這次的意外收獲是sql的查詢語句中的各種符號基本弄懂。