機房收費系統之組合查詢

【前言】

      組合查詢在機房收費系統中出現的較多,同時個人認爲組合查詢還是對大腦的邏輯思維要求蠻高的,因此,在這篇博客裏以學生基本信息查詢窗體爲例來總結一下機房收費系統之組合查詢。

 

【正文】

窗體:

邏輯思路:

 

(一)對於窗體中的需要填寫的內容設置爲非空:

 

<span style="font-size:18px;">'***************第一行所有內容不爲空
If Trim(comboFieldname(0).Text = "") Then
    MsgBox "請選擇字段名", vbOKOnly, "警告"
    comboFieldname(0).SetFocus
    Exit Sub
End If

If Trim(comboOperator(0).Text = "") Then
    MsgBox "請選擇字段名", vbOKOnly, "警告"
    comboFieldname(0).SetFocus
    Exit Sub
End If

If Trim(txtContent(0).Text = "") Then
    MsgBox "請選擇字段名", vbOKOnly, "警告"
    comboFieldname(0).SetFocus
    Exit Sub
End If

blnMark(0) = True
'**************************************
        
'***************第二行所有內容和組合關係不爲空
If Not comboRelation(0).Text = "" Then
    If Trim(comboFieldname(1).Text = "") Then
        MsgBox "請選擇字段名", vbOKOnly, "警告"
        comboFieldname(1).SetFocus
        Exit Sub
    End If
    
    If Trim(comboOperator(1).Text = "") Then
        MsgBox "請選擇字段名", vbOKOnly, "警告"
        comboFieldname(1).SetFocus
        Exit Sub
    End If
    
    If Trim(txtContent(1).Text = "") Then
        MsgBox "請選擇字段名", vbOKOnly, "警告"
        comboFieldname(1).SetFocus
        Exit Sub
    End If
    
    If Trim(comboRelation(0).Text = "") Then
        MsgBox "請選擇組合關係", vbOKOnly, "提醒"
    End If
    
    blnMark(1) = True
End If
'**************************************
        
''***************第三行所有內容和組合關係不爲空
If blnMark(0) = True And blnMark(1) = True And comboRelation(1).Text <> "" Then
    If Trim(comboFieldname(2).Text = "") Then
        MsgBox "請選擇字段名", vbOKOnly, "警告"
        comboFieldname(2).SetFocus
        Exit Sub
    End If

    If Trim(comboOperator(2).Text = "") Then
        MsgBox "請選擇字段名", vbOKOnly, "警告"
        comboFieldname(2).SetFocus
        Exit Sub
    End If

    If Trim(txtContent(2).Text = "") Then
        MsgBox "請選擇字段名", vbOKOnly, "警告"
        comboFieldname(2).SetFocus
        Exit Sub
    End If
    
    If Trim(comboRelation(1).Text = "") Then
        MsgBox "請選擇組合關係", vbOKOnly, "提醒"
    End If
    blnMark(2) = True
End If
'**************************************</span>

 

(二)查詢

1、將窗體上組合框中所選擇的內容轉換爲數據庫的查詢:

 

<span style="font-size:18px;">'**************將組合框裏的內容轉換爲數據庫語言
Public Function Filename(i As String) As String
    
    Select Case i
        Case "卡號"
         Filename = "cardno"
        Case "學號"
         Filename = "studentno"
        Case "姓名"
         Filename = "studentname"
        Case "性別"
         Filename = "sex"
        Case "系別"
         Filename = "department"
        Case "年級"
         Filename = "grade"
        Case "班級"
         Filename = "class"
        Case "與"
         Filename = "and"
        Case "或"
         Filename = "or"
    End Select
End Function
'******************************</span>

2、查詢語句編寫:

<span style="font-size:18px;">'*****************分別將每一行的內容看爲一個整體
    strA = Filename(ComboFieldName(0).Text) & Trim(ComboOperator(0).Text) & Trim(txtContent(0).Text) & " "
    </span><span style="font-size:18px;">strB = Filename(ComboRelation(0).Text) & " " & Filename(ComboFieldName(1).Text) & Trim(ComboOperator(1).Text) & Trim(txtContent(1).Text) & " "
    strC = Filename(ComboRelation(1).Text) & " " & Filename(ComboFieldName(2).Text) & Trim(ComboOperator(2).Text) & Trim(txtContent(2).Text)
'***************************************
    
If blnMark(0) = False Then     '如果第一行內容不完整則出現提示
     MsgBox "請選擇查詢條件!", vbOKOnly, "提醒"
    Exit Sub
Else
    txtSQL = txtSQL & strA     '根據窗體中第一行的內容所對應的查詢語句
     If blnMark(1) = True Then
       txtSQL = txtSQL & strB '如果連接第一行和第二行的組合關係被選則需查詢窗體中第一行和第二行的組合查詢
         If blnMark(2) = True Then
          txtSQL = txtSQL & strC  '如果連接第一行和第二行的組合關係,第二行和第三行的組合關係被選則需查詢窗體中三行的組合查詢
          End If
     End If
End If</span>

 

3、將查詢出來的結果顯示在MSFlexGrid1中:

Set mrc = ExecuteSQL(txtSQL, Msgtext) '連接數據庫

With MSFlexGrid1
    .Rows = 1
    .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(0, 9) = "狀態"
    .TextMatrix(0, 10) = "日期"
    .TextMatrix(0, 11) = "時間"
    .TextMatrix(0, 12) = "類型"
Do While mrc.EOF = False
    .CellAlignment = 4
    .Rows = .Rows + 1
    .TextMatrix(.Rows - 1, 0) = mrc.Fields(0)
    .TextMatrix(.Rows - 1, 1) = mrc.Fields(1)
    .TextMatrix(.Rows - 1, 2) = mrc.Fields(2)
    .TextMatrix(.Rows - 1, 3) = mrc.Fields(3)
    .TextMatrix(.Rows - 1, 4) = mrc.Fields(4)
    .TextMatrix(.Rows - 1, 5) = mrc.Fields(5)
    .TextMatrix(.Rows - 1, 6) = mrc.Fields(6)
    .TextMatrix(.Rows - 1, 7) = mrc.Fields(7)
    .TextMatrix(.Rows - 1, 8) = mrc.Fields(8)
    .TextMatrix(.Rows - 1, 9) = mrc.Fields(9)
    .TextMatrix(.Rows - 1, 10) = mrc.Fields(12)
    .TextMatrix(.Rows - 1, 11) = mrc.Fields(13)
    .TextMatrix(.Rows - 1, 12) = mrc.Fields(14)
    mrc.MoveNext
Loop
End With

 

4、組合查詢完成  PS:不可缺少的會定義相應的變量

    Dim mrc As ADODB.Recordset
    Dim txtSQL As String, Msgtext As String
    Dim strA As String, strB As String, strC As String
    Dim blnMark(0 To 2) As Boolean
    
    txtSQL = "select * from student_Info where "

 

【總結】

一、及時對我們所理解的內容進行總結,當時的感覺是最“新鮮”的!

二、在很多時候,邏輯是關鍵,把邏輯關係整理清楚後一切問題就會迎刃而解!

三、遇到問題時首先自己要先嚐試着去做,自己獨立寫出自己的邏輯,那樣不僅更有成就感,同時收穫也是非同一般的!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章