1. 程式人生 > >VB查詢資料庫之組合查詢——機房收費總結(二)

VB查詢資料庫之組合查詢——機房收費總結(二)

     在機房收費系統中,組合查詢用的還是挺多的,像上機狀態查詢窗體、學生上機統計資訊窗體、操作員工記錄窗、基本資訊維護窗。這其中,學生基本資訊維護窗中的東西比較多,就以它為例子,說說組合查詢吧!

     學生基本資訊維護窗體如圖所示:

   

先把程式碼放在下面,然後再解說:

Option Explicit
Private Sub cboField1_Click()       '把選擇的欄位轉換成  sql語句中的欄位
    If cboField1.Text = "卡號" Then cboField1.Tag = "cardNo"
    If cboField1.Text = "學號" Then cboField1.Tag = "studentNo"
    If cboField1.Text = "姓名" Then cboField1.Tag = "studentName"
    If cboField1.Text = "性別" Then cboField1.Tag = "sex"
    If cboField1.Text = "學院" Then cboField1.Tag = "college"
    If cboField1.Text = "專業" Then cboField1.Tag = "major"
    If cboField1.Text = "年級" Then cboField1.Tag = "grade"
    If cboField1.Text = "班級" Then cboField1.Tag = "class"
End Sub

Private Sub cboField2_Click()        '把選擇的欄位轉換成  sql語句中的欄位
    If cboField2.Text = "卡號" Then cboField2.Tag = "cardNo"
    If cboField2.Text = "學號" Then cboField2.Tag = "studentNo"
    If cboField2.Text = "姓名" Then cboField2.Tag = "studentName"
    If cboField2.Text = "性別" Then cboField2.Tag = "sex"
    If cboField2.Text = "學院" Then cboField2.Tag = "college"
    If cboField2.Text = "專業" Then cboField2.Tag = "major"
    If cboField2.Text = "年級" Then cboField2.Tag = "grade"
    If cboField2.Text = "班級" Then cboField2.Tag = "class"
End Sub
        
Private Sub cboField3_Click()        '把選擇的欄位轉換成  sql語句中的欄位
    If cboField3.Text = "卡號" Then cboField3.Tag = "cardNo"
    If cboField3.Text = "學號" Then cboField3.Tag = "studentNo"
    If cboField3.Text = "姓名" Then cboField3.Tag = "studentName"
    If cboField3.Text = "性別" Then cboField3.Tag = "sex"
    If cboField3.Text = "學院" Then cboField3.Tag = "college"
    If cboField3.Text = "專業" Then cboField3.Tag = "major"
    If cboField3.Text = "年級" Then cboField3.Tag = "grade"
    If cboField3.Text = "班級" Then cboField3.Tag = "class"
End Sub

Private Sub cboMode1_Click()            '把選擇的欄位的組合關係轉換成SQL中的組合關係關鍵字,後面可以直接寫入查詢語句
    If cboMode1.Text = "與" Then cboMode1.Tag = "and"
    If cboMode1.Text = "或" Then cboMode1.Tag = "or"

    cboField2.Visible = True
    cboSign2.Visible = True
    txtInquire2.Visible = True
    cboMode2.Visible = True
End Sub

Private Sub cboMode2_Click()            '把選擇的欄位組合關係轉換成SQL中的組合關係關鍵字,後面可以直接寫入查詢語句
    If cboMode2.Text = "與" Then cboMode2.Tag = "and"
    If cboMode2.Text = "或" Then cboMode2.Tag = "or"

    cboField3.Visible = True
    cboSign3.Visible = True
    txtInquire3.Visible = True
End Sub

Private Sub cmdClear_Click()     '清空所有的屬性
    cboField1.Text = ""
    cboField2.Text = ""
    cboField3.Text = ""
    cboSign1.Text = ""
    cboSign2.Text = ""
    cboSign3.Text = ""
    txtInquire1.Text = ""
    txtInquire2.Text = ""
    cboMode1.Text = ""
    cboMode2.Text = ""
    
    cboField2.Visible = False       '把多查詢條件欄位先隱藏'隱藏起多欄位,這樣剛剛開始,只顯示第一條查詢,如果需要,選擇"與"或者"或",顯示下一條欄位,同理顯示第三條欄位。無形中告訴使用者,不要空掉第二條欄位,只用一三條,這樣會給編碼帶來麻煩的。
cboSign2.Visible = False txtInquire2.Visible = False cboMode2.Visible = False cboField3.Visible = False cboSign3.Visible = False txtInquire3.Visible = False myFlexGrid.Clear End Sub
Private Sub cmdcharge_Click()
    Dim txtSQL As String
    Dim MsgText As String
    Dim mrcSD As ADODB.Recordset
    
    txtSQL = "select * from student_Info where studentNo='" & Trim(myFlexGrid.TextMatrix(myFlexGrid.RowSel, 0)) & "'"
    Set mrcSD = ExecuteSQL(txtSQL, MsgText)
    
        If mrcSD.RecordCount = 0 Then
        MsgBox "請選中一條記錄!", vbOKCancel + vbExclamation, "警告"
        Exit Sub
    End If
    
    
    frmModifyStudentLog.Show'顯示查詢窗體
End Sub

Private Sub cmdExit_Click()
    Unload Me
End Sub

Private Sub cmdselect_Click()
    Dim txtSQL As String
    Dim MsgText As String
    Dim mrc As ADODB.Recordset
    
    If cboMode1.Text = "" Then
        MsgBox "請輸入需要查詢的欄位", vbOKOnly + vbExclamation, "警告"
       Unload Me
    End If
    
    '首先寫一個不完全的SQL語句,一會兒補充
    txtSQL = "select * from student_Info where (" & cboField1.Tag & cboSign1.Text & "'" & txtInquire1.Text & "'"
    
    If cboMode1.Text <> "" Then      '檢驗第二個組合欄位的正確性
        If cboField2.Text = "" Then
            MsgBox "請選擇欄位名!", vbOKOnly + vbExclamation, "警告"
            cboField2.SetFocus
            Exit Sub
        End If
        If cboSign2.Text = "" Then
            MsgBox "請輸入操作符!", vbOKOnly + vbExclamation, "警告"
            cboSign2.SetFocus
            Exit Sub
        End If
        If txtInquire2.Text = "" Then
            MsgBox "請輸入要查詢的內容", vbOKOnly + vbExclamation, "警告"
            txtInquire2.SetFocus
            Exit Sub
        End If
        
        '第二個組合欄位正確,開始新增資訊
        txtSQL = txtSQL & " " & cboMode1.Tag & " " & cboField2.Tag & cboSign2.Text & "'" & txtInquire2.Text & "'"
    End If
    
    If cboMode2.Text <> "" Then     '檢驗第三組 組合欄位的正確性
        If cboField3.Text = "" Then
            MsgBox "請選擇欄位名!", vbOKOnly + vbExclamation, "警告"
            cboField3.SetFocus
            Exit Sub
        End If
        If cboSign3.Text = "" Then
            MsgBox "請輸入操作符!", vbOKOnly + vbExclamation, "警告"
            cboSign3.SetFocus
            Exit Sub
        End If
        If txtInquire3.Text = "" Then
            MsgBox "請輸入要查詢的內容", vbOKOnly + vbExclamation, "警告"
            txtInquire3.SetFocus
            Exit Sub
        End If
        
        '第三個組合欄位正確,開始新增資訊
        txtSQL = txtSQL & " " & cboMode2.Tag & " " & cboField3.Tag & cboSign3.Text & "'" & txtInquire3.Text & "'"
    End If
    
    txtSQL = txtSQL & ") "   '把SQL語句補充完整
    'Debug.Print txtSQL   用於除錯程式使用
    Set mrc = ExecuteSQL(txtSQL, 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 Sub


Private Sub Form_Load()
    cboField1.AddItem "卡號"
    cboField1.AddItem "學號"
    cboField1.AddItem "姓名"
    cboField1.AddItem "性別"
    cboField1.AddItem "學院"
    cboField1.AddItem "專業"
    cboField1.AddItem "年級"
    cboField1.AddItem "班級"
    
    cboField2.AddItem "卡號"
    cboField2.AddItem "學號"
    cboField2.AddItem "姓名"
    cboField2.AddItem "性別"
    cboField2.AddItem "學院"
    cboField2.AddItem "專業"
    cboField2.AddItem "年級"
    cboField2.AddItem "班級"
   
    cboField3.AddItem "卡號"
    cboField3.AddItem "學號"
    cboField3.AddItem "姓名"
    cboField3.AddItem "性別"
    cboField3.AddItem "學院"
    cboField3.AddItem "專業"
    cboField3.AddItem "年級"
    cboField3.AddItem "班級"
    
    
    cboSign1.AddItem "="
    cboSign1.AddItem "<"
    cboSign1.AddItem ">"
    cboSign1.AddItem "<>"
    
    cboSign2.AddItem "="
    cboSign2.AddItem "<"
    cboSign2.AddItem ">"
    cboSign2.AddItem "<>"
    
    cboSign3.AddItem "="
    cboSign3.AddItem "<"
    cboSign3.AddItem ">"
    cboSign3.AddItem "<>"
    
    cboMode1.AddItem "與"
    cboMode1.AddItem "或"
    
    cboMode2.AddItem "與"
    cboMode2.AddItem "或"
End Sub

     在編寫這個窗體的時候,剛開始總是無法查詢,各種報錯,後來,看了同學的部落格,利用tag將文字語言轉換為sql語言,直接寫入查詢語句,這就方便了很多。之後,在查詢的時候,剛開始想直接在窗體中顯示所有查詢語句,但是,在執行的時候,往往因為第二條空著,而第三條能寫入了查詢條件而出錯。各種限制方法非常麻煩,我就直接限制了窗體顯示的語句條數,算是個“懶辦法”吧!

    這個窗體還真是用了不少時間,剛剛開始是因為不知道如何寫查詢語句,使用了tag之後,又出現了查詢時遍歷錯誤,總之,麻煩還是挺多的。不過,解決了一個窗體,其他的組合查詢都差不多了。

     我的總結,希望大家幫忙找出缺點,改正後一起進步。下一篇VB寫入資料庫。