1. 程式人生 > >機房收費系統(四)---組合查詢

機房收費系統(四)---組合查詢

  涉及的窗體有學生基本資訊維護,學生上機記錄查詢,學生上機統計查詢初次見到組合查詢介面,有種似曾相識的感覺。沒錯,我們在學生資訊管理系統裡面見過類似的。也可以說我們已經接觸過組合查詢了。不同的是這次的組合查詢要多一點,相對來講複雜一點。今天我就用學生基本資訊維護窗體給大家談談。

窗體

學生基本資訊維護

流程圖

流程圖

程式碼

1. 定義一個Field函式

  用來使VB程式中的欄位名與資料庫中的列名相對應。1 定義一個Field函式,用來使VB程式中的欄位名與資料庫中的列名相對應。

Public Function field( i As String) As String  
'定義一個函式過程  
Select Case i '欄位名與資料庫中的列名相對應 '學生基本資訊維護要用到的欄位 Case "學號" field = "studentno" Case "姓名" field = "studentName" Case "卡號" field = "cardno" Case "金額" field = "cash" Case "系別" field = "department"
Case "年級" field = "grade" Case "班級" field = "class" Case "性別" field = "sex" Case "狀態" field = "status" Case "備註" field = "explain" Case "型別" field = "type" Case
"日期" field = "date" Case "時間" field = "time" Case "或" field = "or " Case "與" field = "and " End Select End Function
2. 以查詢學生上機記錄為例的組合查詢程式碼:
Private Sub cmdselect_Click()
    Dim StutxtSQL As String
    Dim MsgText As String
    Dim mrc As ADODB.Recordset

    StutxtSQL = "select * from student_Info where"
    '在online_Info這張表中獲得整行記錄。其中*表示整行記錄,也可以換成你需要查詢的具體記錄。

    If Trim(combo1.Text) = "" Or Trim(combo2.Text) = "" Or Trim(Text1.Text) = "" Then
       MsgBox "請將選項內容填寫完整!", vbOKOnly, "提示"
       Exit Sub
    Else
       StutxtSQL = StutxtSQL & " " & field(combo1.Text) & " " & combo2.Text & "'" & Trim(Text1.Text) & "'"

       If combo7.Text <> "" Then
       '判斷第一個組合關係是否選中
           If combo3.Text = "" Or combo4.Text = "" Or Text2.Text = "" Then
           '如果選中,判斷第二行內容是否填寫完整,且符合要求
               MsgBox "請將第二行選項內容填寫完整!", vbOKOnly, "提示"
               Exit Sub
           Else
               StutxtSQL = StutxtSQL & " " & field(combo7.Text) & " " & field(combo3.Text) & " " & combo4.Text & "'" & Trim(Text2.Text) & "'"
               '將前兩行的條件聯絡起來,完成SQL語句
                If combo8.Text <> "" Then
                '判斷第二個組合關係是否選中
                     If combo5.Text = "" Or combo6.Text = "" Or Text3.Text = "" Then
                     '如果選中,判斷第二行內容是否填寫完整,且符合要求
                         MsgBox "請將第三行選項內容填寫完整!", vbOKOnly, "提示"
                         Exit Sub
                     Else
                         StutxtSQL = StutxtSQL & " " & field(combo8.Text) & " " & field(combo5.Text) & " " & Trim(combo6.Text) & "'" & Trim(Text3.Text) & "'"
                         '將三行的條件聯絡起來,完成SQL語句
                     End If
                End If
            End If
    End If

    Set mrc = ExecuteSQL(StutxtSQL, MsgText)

    If mrc.RecordCount = 0 Then         '如果要查詢的結果為空,則提醒使用者
        myflexgrid.Clear
        MsgBox "結果集為空!", vbOKOnly + vbExclamation, "警告"
        Exit Sub
    End If

    With myflexgrid         '把標題寫上
        .Row = 0
        .TextMatrix(.Row, 0) = "學號"
        .TextMatrix(.Row, 1) = "姓名"
        .TextMatrix(.Row, 2) = "卡號"
        .TextMatrix(.Row, 3) = "金額"
        .TextMatrix(.Row, 4) = "系別"
        .TextMatrix(.Row, 5) = "年級"
        .TextMatrix(.Row, 6) = "班級"
        .TextMatrix(.Row, 7) = "性別"
        .TextMatrix(.Row, 8) = "狀態"
        .TextMatrix(.Row, 9) = "備註"
    End With

    myflexgrid.Rows = mrc.RecordCount + 1           '設定大小

    With myflexgrid         '對查詢到的結果進行遍歷,顯示出來
        .Row = 0
        While mrc.EOF = False
            .Row = .Row + 1
            .TextMatrix(.Row, 0) = "  " & mrc.Fields(1)
            .TextMatrix(.Row, 1) = "  " & mrc.Fields(2)
            .TextMatrix(.Row, 2) = "  " & mrc.Fields(0)
            .TextMatrix(.Row, 3) = "  " & mrc.Fields(7)
            .TextMatrix(.Row, 4) = "  " & mrc.Fields(4)
            .TextMatrix(.Row, 5) = "  " & mrc.Fields(5)
            .TextMatrix(.Row, 6) = "  " & mrc.Fields(6)
            .TextMatrix(.Row, 7) = "  " & mrc.Fields(3)
            .TextMatrix(.Row, 8) = "  " & mrc.Fields(11)
            .TextMatrix(.Row, 9) = "  " & mrc.Fields(8)
            mrc.MoveNext
        Wend
    End With
    End If
End Sub

感受

  組合查詢其只要掌握好SQL語句的使用還有搞清楚邏輯就會很簡單。