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

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

前言:組合查詢的實現用了三天時間,剛開始看這個窗體確實有點難度,開始鑽研原版的系統多點點會發現規律,與學生的查詢思路相似,所以多去實踐,錯了也沒有關係

以學生基本資訊維護為例

邏輯思路:組合查詢即為多條件查詢(查詢語句where後即為條件,全部在此做文章)窗體中最多是三個條件,兩個及兩個以上即為組合查詢。用的是學生查詢中語句拼接,以一個組合關係為節點判斷是否為組合查詢,如果是將這兩個條件組合起來即可,下面看

流程圖:

依據流程圖敲程式碼即可

自定義函式:

Public Function Field(i As String) As String

    Select Case i

        Case "與"

            Field = "and"

        Case "或"

            Field = "or"

        Case "卡號"

            Field = "cardno"

        Case "姓名"

            Field = "studentname"

        Case "學號"

            Field = "studentno"

        Case "系別"

            Field = "department"

        Case "年級"

            Field = "grade"

        Case "班級"

            Field = "class"

        Case "性別"

            Field = "sex"

    End Select

End Function

實現功能:將資料內容轉化為where子句可識別的資料表中的欄位名,方便呼叫

新增資料:

    Dim a, b, c

       For a = 0 To 2

        With Combo1(a)

            .AddItem "卡號"

            .AddItem "學號"

            .AddItem "姓名"

            .AddItem "性別"

            .AddItem "系別"

            .AddItem "年級"

            .AddItem "班級"

        End With

    Next a

    For b = 0 To 2

        With Combo2(b)

            .AddItem "="

            .AddItem "<"

            .AddItem ">"

            .AddItem "<>"

         End With

    Next b

For c = 0 To 1

        With Combo3(c)

            .AddItem "與"

            .AddItem "或"

        End With

    Next c

功能:利用陣列控制元件新增內容,減少程式碼量

查詢程式碼如下:

    stuSQL = "select * from student_info where "         '查詢基本的條件

    If Combo1(0).Text = "" Or Combo2(0).Text = "" Or Text1.Text = "" Then    '判斷第一行資料是否為空

        MsgBox "請輸入內容", 0 + 48, "提示"

    Else

        stuSQL = stuSQL & Field(Combo1(0).Text) & Combo2(0).Text & "'" & Text1.Text & "'"  '查詢語句加上第一行資料條件

    End If

    '通過第一個組合關係框判斷是否是組合查詢

    If Combo3(0).Text <> "" Then

        If Combo1(1).Text = "" Or Combo2(1).Text = "" Or Text2.Text = "" Then             '新增第二行資料

            MsgBox "請輸入內容", 0 + 48, "提示"

        Else

            Select Case Combo3(0).Text

            Case "與"

                stuSQL = stuSQL & " and " & Field(Combo1(1).Text) & Combo2(1).Text & "'" & Text2.Text & "'" '組合關係條件確定

            Case "或"

                stuSQL = stuSQL & " or " & Field(Combo1(1).Text) & Combo2(1).Text & "'" & Text2.Text & "'"

            End Select

        End If

    End If

    '通過第二個組合關係框判斷是否是組合查詢

    If Combo3(1).Text <> "" Then

        If Combo1(2).Text = "" Or Combo2(2).Text = "" Or Text3.Text = "" Then            '新增第三行資料

            MsgBox "請輸入內容", 0 + 48, "提示"

        Else

            Select Case Combo3(1).Text

            Case "與"

                stuSQL = stuSQL & " and " & Field(Combo1(1).Text) & Combo2(1).Text & "'" & Text2.Text & "'"

            Case "或"

                stuSQL = stuSQL & " or " & Field(Combo1(1).Text) & Combo2(1).Text & "'" & Text2.Text & "'"

            End Select

        End If

    End If

    Set stumrc = ExecuteSQL(stuSQL, stuText)            '開始查詢

    If stumrc.RecordCount = 0 Then

        MsgBox "查詢無記錄", 0 + 48, "提示"

        Combo1(0).SetFocus

        MSHFlexGrid1.Rows = 1

    Else

        With MSHFlexGrid1                      '填入標題

            .Rows = 1

            .CellAlignment = 4

            .ColAlignment = 2

             .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 stumrc.EOF          '顯示查詢資料

            .Rows = .Rows + 1

            .TextMatrix(.Rows - 1, 0) = stumrc.Fields(0)

            .TextMatrix(.Rows - 1, 1) = stumrc.Fields(1)

            .TextMatrix(.Rows - 1, 2) = stumrc.Fields(2)

            .TextMatrix(.Rows - 1, 3) = stumrc.Fields(3)

            .TextMatrix(.Rows - 1, 4) = stumrc.Fields(4)

            .TextMatrix(.Rows - 1, 5) = stumrc.Fields(5)

            .TextMatrix(.Rows - 1, 6) = stumrc.Fields(6)

            .TextMatrix(.Rows - 1, 7) = stumrc.Fields(7)

            .TextMatrix(.Rows - 1, 8) = stumrc.Fields(8)

            .TextMatrix(.Rows - 1, 9) = stumrc.Fields(10)

            .TextMatrix(.Rows - 1, 10) = stumrc.Fields(12)

            .TextMatrix(.Rows - 1, 11) = stumrc.Fields(13)

            .TextMatrix(.Rows - 1, 12) = stumrc.Fields(14)

            stumrc.MoveNext

        Loop

        End With

    End If

   stumrc.Close

總結:搞清邏輯之後,其實實現起來並不難,感謝大家閱讀,如有錯誤,還請大家指正