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

 

     機房收費系統在磕磕絆絆中過來了,這期間遇到問題,解決問題,最後收穫的特別多,在敲得過程中,不斷的學習新知識,應該說組合查詢是收費系統的一個小難點了吧,起初我是真的不知道該從哪裏下手,總是有種剪不斷理還亂的感覺,分析分析就繞進去了,我總是把問題想的很複雜,其實只要一句SQL語句,一切都解決了,根本用不到好多好多的IF,FOr 什麼的,下面是我自己在實現此過程所需要的代碼:以收費系統中基本信息維護爲例子,其實其他的也都大同小異

 

     只要一句正確的SQL,其他全OK!

     剛着手時看到這個確實有點迷糊,該從何下手呢?理清楚了才明白,其實很簡單,在這裏我是用組合關係爲點,進行判斷,如果組合關係中什麼都沒有選擇,那麼就認爲查詢條件爲第一行,而且必須選擇完全,如果選擇了組合關係,那麼相對應的2或3行條件就需要填充完整,接着就是按照條件在數據庫中查詢內容了。而我的字段名選擇是漢字,要想在數據庫中查詢字段,根本就不認識,這就需要我將漢字轉換爲相應的字符串,代碼中的FieldName 就是我要利用到的一個函數

Private Sub cmdInquire_Click()

 

    Dim mrc As ADODB.Recordset

    Dim txtSQL As String

    Dim MsgText As String

 

    txtSQL = "select * from student_Info where "

    '判斷字段的選擇是否爲空

    If Not Testtxt(comboFiledName1.Text) Then

        MsgBox "請選擇字段!", vbOKOnly + vbExclamation, "警告"

        comboFiledName1.SetFocus

        Exit Sub

    End If

    '判斷操作符的選擇是否爲空

    If Not Testtxt(comboOperate1.Text) Then

        MsgBox "請選擇操作符!", vbOKOnly + vbExclamation, "警告"

        comboOperate1.SetFocus

        Exit Sub

    End If

    '判斷要查詢的內容是否爲空

    If Not Testtxt(txtContent1.Text) Then

        MsgBox "請在輸入要查詢的內容", vbOKOnly + vbExclamation, "警告"

        txtContent1.SetFocus

        Exit Sub

    End If

    txtSQL = txtSQL & FiledName(comboFiledName1.Text) & " " & comboOperate1.Text & "'" & txtContent1.Text & "'"

 

    '利用模版函數查看是否是組合查詢第一行爲空時,查詢無效

    '開始組合查詢

    If Trim(comboRelation1.Text <> "") Then

        If Trim(comboFiledName2.Text) = "" Or Trim(comboOperate2.Text) = "" Or Trim(txtContent2.Text) = "" Then

            MsgBox "您選擇了組合關係,請輸入數據之後再查詢", vbOKOnly, "提示信息"

            Exit Sub

        Else

            txtSQL = txtSQL & FiledName(comboRelation1.Text) & " " & FiledName(comboFiledName2.Text) & comboOperate2.Text & "'" & Trim(txtContent2.Text) & "'"

        End If

    End If

    If Trim(comboRelation2.Text) <> "" Then

        If Trim(comboFiledName3.Text) = "" Or Trim(comboOperate3.Text) = "" Or Trim(txtContent3.Text) = "" Then

            MsgBox "您選擇了第二個組合,請輸入數據之後在查詢", vbOKOnly, "提示"

            Exit Sub

        Else

            txtSQL = txtSQL & FiledName(comboRelation2.Text) & " " & FiledName(comboFiledName3.Text) & comboOperate3.Text & "'" & Trim(txtContent3.Text) & "'"

        End If

    End If

 

    '開始進行查找

 

    Set mrc = ExecuteSQL(txtSQL, MsgText)

 

    If mrc.RecordCount = 0 Then

        MsgBox "沒有您要查找的學生上機記錄!", vbOKOnly + vbCritical, "查詢提示"

        comboFiledName1.SetFocus

        MSHFlexGrid1.Rows = 1

        '存在上機記錄查詢成功,填充到表格

    Else

 

        MSHFlexGrid1.Rows = 1

        Do While Not mrc.EOF

 

            With MSHFlexGrid1

                .CellAlignment = 4

                .Rows = .Rows + 1

                .TextMatrix(.Rows - 1, 0) = mrc!studentNo

                .TextMatrix(.Rows - 1, 1) = mrc!studentName

                .TextMatrix(.Rows - 1, 2) = mrc!cardno

                .TextMatrix(.Rows - 1, 3) = mrc!cash

                .TextMatrix(.Rows - 1, 4) = mrc!department

                .TextMatrix(.Rows - 1, 5) = mrc!grade

                .TextMatrix(.Rows - 1, 6) = mrc!Class

                .TextMatrix(.Rows - 1, 7) = mrc!Sex

                .TextMatrix(.Rows - 1, 8) = mrc!Status

                .TextMatrix(.Rows - 1, 9) = mrc!explain

            End With

            mrc.MoveNext

        Loop

    End If

    Exit Sub

End Sub


 

'將漢字轉換爲相應的字段名字

Public Function FiledName(StrFiledName As String) As String

    Select Case StrFiledName

    Case "卡號"

        FiledName = "cardno"

    Case "學號"

        FiledName = "studentNo"

    Case "姓名"

        FiledName = "studentName"

    Case "性別"

        FiledName = "Sex"

    Case "學院"

        FiledName = "department"

    Case "年級"

        FiledName = "grade"

    Case "班級"

        FiledName = "class"

    Case "與"

        FiledName = "and"

    Case "或"

        FiledName = "or"

 

    End Select

End Function


    這樣一個組合查詢完成了,重其中我也收穫了財富,遇到錯誤的時候就是進步的時候,實現這個功能最大的感慨就是SQL語句真的很強大,而我還處在最低級,以後還要努力加油了!

 

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