在機房收費系統當中,有好幾個窗體設計到了組合查詢的問題,在這篇博客當中我就說一下組合查詢的事
首先,組合查詢的窗體如下所示:
爲簡單起見給圖中控件的命名ComboBox控件的命名左邊三列分別爲combo(1,2,3),中間的爲combo(4,5,6),右邊的爲(7,8);文本框的命名就是Text(1,2,3)
查詢語句如下所示:
Private Sub cmdCheck_Click()
Dim ctrl As Control
Dim mrc As ADODB.Recordset
Dim txtSQL As String
Dim Msgtext As String
Dim i As Integer
If Combo7.Text = "" Then
If Trim(Text1.Text) = "" Or Trim(Combo1.Text) = "" Or Trim(Combo4.Text) = "" Then
MsgBox "請輸入完整的查詢條件", , "提示"
Exit Sub
Else
txtSQL = "select * from Line_info where " & Trim(field(Combo1.Text)) & Trim(Combo4.Text) & "'" & Trim(Text1.Text) & "'"
End If
Else
If Combo8.Text = "" Then
If Trim(Text2.Text) = "" Or Trim(Combo2.Text) = "" Or Trim(Combo5.Text) = "" Then
MsgBox "請輸入完整的查詢條件", , "提示"
Exit Sub
Else
txtSQL = "select * from Line_info where " & Trim(field(Combo1.Text)) & Trim(Combo4.Text) & "'" & Trim(Text1.Text) & "'" & " " & Trim(field(Combo7.Text)) & " " & Trim(field(Combo2.Text)) & Trim(Combo5.Text) & "'" & Trim(Text2.Text) & "'"
End If
Else
If Trim(Text3.Text) = "" Or Trim(Combo3.Text) = "" Or Trim(Combo6.Text) = "" Then
MsgBox "請輸入完整的查詢條件", , "提示"
Exit Sub
Else
txtSQL = "select * from Line_info where " & Trim(field(Combo1.Text)) & Trim(Combo4.Text) & "'" & Trim(Text1.Text) & "'" & " " & Trim(field(Combo7.Text)) & " " & Trim(field(Combo2.Text)) & Trim(Combo5.Text) & "'" & Trim(field(Text2.Text)) & "'" & " " & Trim(field(Combo8.Text)) & " " & Trim(field(Combo3.Text)) & Trim(Combo6.Text) & "'" & Trim(Text3.Text) & "'"
End If
End If
End If
Set mrc = ExecuteSQL(txtSQL, Msgtext)
If mrc.EOF = True Then '檢查信息是否存在,如果不存在給出提示並清空所有文本框
MsgBox "沒有查詢到結果,請檢查輸入信息!", vbOKOnly + vbExclamation, "提示"
Call Cancel
Exit Sub
End If
With MSHFlexGrid1
.Rows = 1
.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
.TextMatrix(.Rows - 1, 0) = mrc.Fields(1).Value & ""
.TextMatrix(.Rows - 1, 1) = mrc!studentname
.TextMatrix(.Rows - 1, 2) = Trim(mrc.Fields(6).Value & "")
.TextMatrix(.Rows - 1, 3) = Trim(mrc.Fields(7).Value & "")
.TextMatrix(.Rows - 1, 4) = Trim(mrc.Fields(8).Value & "")
.TextMatrix(.Rows - 1, 5) = Trim(mrc.Fields(9).Value & "")
.TextMatrix(.Rows - 1, 6) = Trim(mrc.Fields(10).Value & "")
.TextMatrix(.Rows - 1, 7) = Trim(mrc.Fields(11).Value & "")
.TextMatrix(.Rows - 1, 8) = Trim(mrc.Fields(12).Value & "")
mrc.MoveNext
Loop
End With
mrc.Close
End Sub
語句雖然是看似簡單,但是其中所含的信息無窮啊。在敲代碼的時候千萬不能忘記的空格的使用。
當然,大家也看到了在代碼部分有一個調用“call cancel”,大家可能會說這是什麼啊,沒見過啊!
這是我爲了方便書寫而定義的一個過程,代碼如下:
Public Function Cancel()
Combo1.Text = ""
Combo2.Text = ""
Combo3.Text = ""
Combo4.Text = ""
Combo5.Text = ""
Combo6.Text = ""
Combo7.Text = ""
Combo8.Text = ""
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
With MSHFlexGrid1
.Rows = 2
.CellAlignment = 4
.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(1, 0) = ""
.TextMatrix(1, 1) = ""
.TextMatrix(1, 2) = ""
.TextMatrix(1, 3) = ""
.TextMatrix(1, 4) = ""
.TextMatrix(1, 5) = ""
.TextMatrix(1, 6) = ""
.TextMatrix(1, 7) = ""
.TextMatrix(1, 8) = ""
End With
End Function
這個過程主要是用於清空已填寫的數據的
還有一個要說明的就是field過程,這個過程是爲了方便我們查詢數據庫而存在的,定義過程如下:
Public Function field(comboX As String) As String
Select Case comboX
Case "卡號"
field = "cardno"
Case "姓名"
field = "studentName"
Case "上機日期"
field = "ondate"
Case "上機時間"
field = "ontime"
Case "下機日期"
field = "offdate"
Case "下機時間"
field = "offtime"
Case "消費金額"
field = "consume"
Case "餘額"
field = "cash"
Case "備註"
field = "status"
Case "與"
field = "and"
Case "或"
field = "or"
End Select
End Function
這篇文章的內容就這麼多了,就上圖的“導出爲Excel功能”請看《機房收費系統問題集錦(三)——導出爲Excel》