【第一次機房收費系統】組合查詢
阿新 • • 發佈:2019-02-11
所有的偉大隻源於一個勇敢地開始。
組合查詢之前以為很難,但是真正去開始敲了,你就會發現並不難。敲完了第一個,基本上就算是敲完了。
這是機房收費系統裡面組合查詢的標準樣式,之所以叫組合查詢,是因為我們要將若干個條件選擇出來之後組合在一起來查詢。首先,組合查詢分為三組查詢條件和兩個組合關係,組合關係兩兩連線組合條件。所以當給你選擇了第一個組合關係之後,必須要有前兩行的查詢條件,選擇了第二個組合關係之後三行條件必須全部選好。反之如果不選組合關係,那麼只能查詢第一行的查詢內容。
以學生基本資訊維護為例來做一個講解。
首先,思路要清晰,所以流程圖先畫好。
之後,根據流程圖來寫程式碼。
這是組合查詢部分程式碼。
Dim txtsql As String
Dim msgText As String
Dim mrc As ADODB.Recordset
Dim ctl As Control
Dim ctl1 As Control
txtsql = "select * from student_info where"
'檢查輸入條件
If Trim(comboField1.Text) = "" Or Trim(comboOpSign1.Text) = "" Or Trim(Text1.Text) = "" Then
MsgBox "請輸入完整的查詢條件", , "蘇軾提醒您"
Exit Sub
End If
' 將第一行查詢內容組合
txtsql = txtsql & " " & Trim(Field(comboField1.Text)) & " " & Trim((comboOpSign1.Text)) & " " & "'" & Trim(Text1.Text) & "'"
If Trim(comboCombineRe1.Text <> "") Then '第一個組合關係存在
If Trim(comboField2.Text) = "" Or Trim(comboOpSign2.Text = "") Or Trim(Text2.Text = "") Then
MsgBox "你已經選擇了第一個組合關係,請輸入第二行查詢條件", , "蘇軾提醒您"
Exit Sub
Else
txtsql = txtsql & " " & Field(Trim(comboCombineRe1.Text)) & " " & Field(comboField2.Text) & " " & comboOpSign2.Text & " " & "' " & Trim(Text2.Text) & "'"
End If
End If
If Trim(comboCombineRe2.Text <> "") Then '第二個組合關係存在
If Trim(comboField3.Text) = "" Or Trim(comboOpSign3.Text = "") Or Trim(Text3.Text = "") Then
MsgBox "你已經選擇了第二個組合關係,請輸入第三行查詢條件", , "蘇軾提醒您"
Exit Sub
Else
txtsql = txtsql & " " & Field(Trim(comboCombineRe2.Text)) & " " & Field(comboField3.Text) & " " & comboOpSign3.Text & " " & "'" & Trim(Text2.Text) & "'"
End If
End If
Set mrc = ExecuteSQL(txtsql, msgText)
If mrc.EOF = True Then '檢查資訊是否存在,如果不存在給出提示並清空所有文字框
MsgBox "沒有查詢到結果,可能你輸入的資訊不存在,或者資訊矛盾。"
For Each ctl In Controls
'清除所有文字
……
Exit Sub
End If
'匹配
With myFlexGrid
.rows = 1 '第一行賦名
.CellAlignment = 4 '單元格的內容居中,居中對齊
.TextMatrix(0, 0) = "卡號"
……
Do While Not mrc.EOF
.rows = .rows + 1
.CellAlignment = 4
.TextMatrix(.rows - 1, 0) = Trim(mrc.Fields(0))
……
mrc.MoveNext
Loop
End With
基本上把上面這些程式碼弄明白,組合查詢就差不多了。
另外,新增幾個小技巧。
一鍵清除所有文字。本來想把程式碼弄到模組了,到時候直接呼叫,但是沒有成功,有大神會的可以來教教我。
Dim ctl As Control
Dim ctl1 As Control
For Each ctl In Controls
'清除所有文字
If TypeOf ctl Is TextBox Then
ctl.Text = ""
End If
Next ctl
'清除combobox文字
For Each ctl1 In Controls
If TypeOf ctl1 Is ComboBox Then
ctl1.Text = ""
End If
Next ctl1
myFlexGrid.clear
定義一個函式讓text裡的文字和資料庫裡的欄位對應。
Public Function Field(i As String) As String
'定義一個函式讓text裡的文字和資料庫裡的欄位對應
Select Case i
Case "卡號"
Field = "cardno"
Case "學號"
Field = "studentno"
……
Case "與"
Field = "and"
……
End Select
End Function
當點選上機日期,上機時間這樣的條件的時候,記得提醒輸入日期查詢各式。可以設定一個溫馨彈窗。另外像姓名、性別這類的條件操作符只有“=”和“<>”,所以我們可以做一個優化。
Private Sub comboField1_click()
If comboField1.Text = "上機日期" Or comboField1.Text = "上機時間" Or comboField1.Text = "下機日期" Or comboField1.Text = "下機時間" Then
MsgBox "日期查詢格式為yyyy/mm/dd,時間查詢格式為hh:mm:ss!", vbOKOnly + vbExclamation, "蘇軾提醒您"
End If
Select Case comboField1.Text
Case "姓名", "備註"
comboOpSign1.clear
comboOpSign1.AddItem "="
comboOpSign1.AddItem "<>"
Case "卡號", "上機日期", "上機時間", "下機日期", "下機時間", "消費金額", "餘額"
comboOpSign1.clear
comboOpSign1.AddItem "="
comboOpSign1.AddItem "<>"
comboOpSign1.AddItem "<"
comboOpSign1.AddItem ">"
End Select
End Sub
敲過之後感覺不難,所以我們不能等有感覺了才開始行動。
加油。