1. 程式人生 > >機房收費系統——組合查詢 1.0

機房收費系統——組合查詢 1.0

一、引言

組合查詢這個問題是我機房收費系統遇到的第一個難題!而且困擾了我很久!
主要有兩個問題:
第一是邏輯上的問題!
第二是資料庫查詢上的問題!
接下來就說說這兩個問題。

二、組合邏輯

學生基本資訊組合查詢
一共有三組查詢條件,兩個組合!
難點:使用者選擇選擇條件及其組合情況的判斷
最簡單的解決辦法就算是按照正常的邏輯,一條一條的進行判斷!
簡單的畫了一個流程圖!
組合條件判斷
這個流程非常容易理解!程式碼也很容易實現!

三、組合查詢語句

1、只有一個條件的時候的查詢

txtSQL = "select * from student_info where " & field(Trim(ziduancombo1.Text
)) & Trim(caozuofucombo1.Text) & "'" & Trim(txtchaxun1.Text) & "'"

注:field為用來轉換combo中的選項,將他們與資料庫中的列名對應起來!
field用之前需要定義!

Public Function field(i As String) As String
Select Case ziduancombo1.Text
    Case "卡號"
field = "cardno"
    Case "學號"
field = "studentno"
    Case "姓名"
field = "studentname"
Case "性別" field = "sex" Case "班級" field = "class" Case "年級" field = "grade" Case "系別" field = "department" Case "日期" field = "date" Case "時間" field = "time" End Function

2、兩個條件進行組合查詢語句

txtSQL = txtSQL & strzuhecombo1 & " " & field(Trim(ziduancombo2.Text
)) & Trim(caozuofucombo2.Text) & "'" & Trim(txtchaxun2.Text) & "'"

注:strzuhecombo1需要定義,用來轉換“或”和“與”,使之能夠被資料庫識別

  Select Case zuhecombo1.Text
      Case "與"
         strzuhecombo1 = "and"
      Case "或"
             strzuhecombo1 = "or"
  End Select

3、三個條件進行組合查詢語句

txtSQL = txtSQL & strzuhecombo2 & " " & field(Trim(ziduancombo3.Text)) & Trim(caozuofucombo3.Text) & "'" & Trim(txtchaxun3.Text) & "'"

四、整個過程程式碼

Private Sub cmdok_Click()
If Trim(ziduancombo1.Text) = "" Then
    MsgBox "請選擇欄位!", vbOKOnly + vbExclamation, "警告"
    ziduancombo1.SetFocus
Else
    If Trim(caozuofucombo1.Text) = "" Then
        MsgBox "請選擇操作符!", vbOKOnly + vbExclamation, "警告"
        caozuofucombo1.SetFocus
    Else
        If Trim(txtchaxun1.Text) = "" Then
            MsgBox "請輸入要查詢的內容!", vbOKOnly + vbExclamation, "警告"
            txtchaxun1.SetFocus
            Exit Sub
        Else
            '執行查詢
             txtSQL = "select * from student_info where " & field(Trim(ziduancombo1.Text)) & Trim(caozuofucombo1.Text) & "'" & Trim(txtchaxun1.Text) & "'"
            '判斷第一個組合條件時為空
            If Trim(zuhecombo1.Text) = "" Then '此時沒有組合查詢
            Set mrc = executeSQL(txtSQL, msgtext)
                If mrc.EOF = True Then
                    MsgBox "沒有該記錄!", vbOKOnly + vbExclamation, "提示"
                    Exit Sub
                End If
            Else
                '選擇了組合條件1時進行組合查詢

                    If Trim(ziduancombo2.Text) = "" Then
                        MsgBox "請將查詢條件填寫完整!", vbOKOnly + vbExclamation, "提示"
                        ziduancombo2.SetFocus
                    Else
                        If Trim(caozuofucombo2.Text) = "" Then
                            MsgBox "請將查詢條件填寫完整!", vbOKOnly + vbExclamation, "提示"
                            caozuofucombo2.SetFocus
                        Else
                            If Trim(txtchaxun2.Text) = "" Then
                                MsgBox "請將查詢條件填寫完整!", vbOKOnly + vbExclamation, "提示"
                                txtchaxun2.SetFocus
                                Exit Sub
                            Else
                                If Trim(zuhecombo2.Text) = "" Then '第二個組合條件為空,進行前兩個的組合查詢
                                '執行查詢
                                Dim strzuhecombo1, strzuhecombo2 '定義變數用來轉換與和或
                                If zuhecombo1.Text <> "" Then
                                 Select Case zuhecombo1.Text
                                  Case "與"
                                   strzuhecombo1 = "and"
                                  Case "或"
                                   strzuhecombo1 = "or"
                                End Select
                                End If
                                 txtSQL = txtSQL & strzuhecombo1 & " " & field(Trim(ziduancombo2.Text)) & Trim(caozuofucombo2.Text) & "'" & Trim(txtchaxun2.Text) & "'"
                                Set mrc = executeSQL(txtSQL, msgtext)
                                    If mrc.EOF = True Then
                                        MsgBox "沒有此條記錄!", vbOKOnly + vbExclamation, "提示"
                                        ziduancombo1.Text = ""
                                        ziduancombo2.Text = ""
                                        caozuofucombo1.Text = ""
                                        caozuofucombo2.Text = ""
                                        txtchaxun1.Text = ""
                                        txtchaxun2.Text = ""
                                        zuhecombo1.Text = ""
                                        Exit Sub
                                     End If
                                Else            '如果第二個組合條件不為空時,對第三行的條件進行檢查
                                    If Trim(ziduancombo3.Text) = "" Then
                                        MsgBox "請將查詢條件填寫完整!", vbOKOnly + vbExclamation, "提示"
                                        ziduancombo3.SetFocus
                                    Else
                                        If Trim(caozuofucombo3.Text) = "" Then
                                            MsgBox "請將查詢條件填寫完整", vbOKOnly + vbExclamation, "提示"
                                            caozuofucombo3.SetFocus
                                        Else
                                            If Trim(txtchaxun3.Text) = "" Then
                                                MsgBox "請將查詢條件填寫完整!", vbOKOnly + vbExclamation, "提示"
                                                txtchaxun3.SetFocus
                                                Exit Sub
                                            Else    '都滿足條件後,進行組合查詢!
                                            Select Case zuhecombo2.Text
                                                Case "與"
                                                    strzuhecombo2 = "and"
                                                Case "或"
                                                    strzuhecombo2 = "or"
                                            End Select
                                                txtSQL = txtSQL & strzuhecombo2 & " " & field(Trim(ziduancombo3.Text)) & Trim(caozuofucombo3.Text) & "'" & Trim(txtchaxun3.Text) & "'"
                                                Set mrc = executeSQL(txtSQL, msgtext)
                                                If mrc.EOF = True Then
                                                    MsgBox "沒有此條查詢記錄!", vbOKOnly + vbExclamation, "提示"
                                                    ziduancombo1.Text = ""
                                                    ziduancombo2.Text = ""
                                                    ziduancombo3.Text = ""
                                                    caozuofucombo1.Text = ""
                                                    caozuofucombo2.Text = ""
                                                    caozuofucombo3.Text = ""
                                                    txtchaxun1.Text = ""
                                                    txtchaxun2.Text = ""
                                                    txtchaxun3.Text = ""
                                                    zuhecombo1.Text = ""
                                                    zuhecombo2.Text = ""
                                                End If
                                            End If
                                        End If
                                    End If
                                End If
                            End If
                        End If
                    End If
            End If
        End If
    End If
End If
'顯示查詢結果
            With MSFgxinxichaxun
         .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 Not mrc.EOF
             .Rows = .Rows + 1
             .CellAlignment = 4
             .TextMatrix(.Rows - 1, 0) = Trim(mrc.Fields(1))
             .TextMatrix(.Rows - 1, 1) = Trim(mrc.Fields(2))
             .TextMatrix(.Rows - 1, 2) = Trim(mrc.Fields(0))
             .TextMatrix(.Rows - 1, 3) = Trim(mrc.Fields(7))
             .TextMatrix(.Rows - 1, 4) = Trim(mrc.Fields(4))
             .TextMatrix(.Rows - 1, 5) = Trim(mrc.Fields(5))
             .TextMatrix(.Rows - 1, 6) = Trim(mrc.Fields(6))
             .TextMatrix(.Rows - 1, 7) = Trim(mrc.Fields(3))
             .TextMatrix(.Rows - 1, 8) = Trim(mrc.Fields(10))
             .TextMatrix(.Rows - 1, 9) = Trim(mrc.Fields(8))
             .TextMatrix(.Rows - 1, 10) = Trim(mrc.Fields(14))
             .TextMatrix(.Rows - 1, 11) = Trim(mrc.Fields(12))
             .TextMatrix(.Rows - 1, 12) = Trim(mrc.Fields(13))
             '移動到下一條記錄
             mrc.MoveNext
         Loop
         End With
End Sub

一篇文章寫下來,覺得組合查詢並沒有那麼難!凡事都需要一個過程!學過了就要學會沉澱,這樣才能走的更遠!