1. 程式人生 > >楊子潁 廊坊師範學院資訊科技提高班十四期

楊子潁 廊坊師範學院資訊科技提高班十四期

組合查詢我們用到了Student_Info,首先你要告訴自己,它一點都不難,稍微用心下,就很容易明

白其中的道理了。

 一、程式碼思路0-3

0.首先,老規矩,先判斷是否為空。

1.當沒有組合關係時,直接從學生表裡查詢資訊,並顯示資料;

值得注意的是,剛開始,當沒有選擇組合關係時,也可以進行多行條件的組合查詢。所以後來,我進行了

優化,就是若未選擇組合關係“或”或“與”,後面的combo和text則不能使用;只有選擇了第一個組合

關係“或”或“與”時,後面的combo和text才可以使用。

此處的程式碼很簡單,如下:

Private Sub Combo7_Click()
    
    If Combo7.Text = "" Then       '當選擇了第一個組合關係,限制後面的combo和text不能使用
        Combo2.Enabled = False
        Combo3.Enabled = False
        Combo5.Enabled = False
        Combo6.Enabled = False
        Combo8.Enabled = False
        Text2.Enabled = False
        Text3.Enabled = False
    Else                           '否則,第二行條件和第二個組合關係可以輸入
        Combo2.Enabled = True
        Combo5.Enabled = True
        Combo8.Enabled = True
        Text2.Enabled = True
        
    End If
    
End Sub

Private Sub Combo8_Click()
    
    If Combo8.Text <> "" Then     '當選擇了第二個組合關係,第三行的combo和text可以使用
        Combo3.Enabled = True
        Combo6.Enabled = True
        Text3.Enabled = True
    End If
    
End Sub
(注: 我沒有使用陣列,其中,Combo7為第一個組合關係框,Combo8為第二個組合關係框)

效果如下圖:

0).選擇組合關係前


1).選擇組合關係後


2.當有一個組合關係時,必須填寫完整前兩行資訊,進行組合查詢;

3.當有二個組合關係時,必須填寫完整三行資訊,進行組合查詢。

二、組合查詢程式碼:

Private Function fieldname(i As String) As String
    
    Select Case i
    '定義一個函式過程,因為資料庫在進行工作時使用英文,在這裡相當於是進行了翻譯
        Case "卡號"
            fieldname = "cardno"
        Case "學號"
            fieldname = "studentno"
        Case "姓名"
            fieldname = "studentname"
        Case "性別"
            fieldname = "sex"
        Case "系別"
            fieldname = "department"
        Case "年級"
            fieldname = "grade"
        Case "班級"
            fieldname = "class"
        Case "與"
            fieldname = "and"
        Case "或"
            fieldname = "or"
    End Select
        
End Function

Public Sub cmdInquire_Click()
    
    Dim mrc As ADODB.Recordset
    Dim mrcc As ADODB.Recordset
    Dim txtSQL As String
    Dim MsgText As String
        
    MSHFlexGrid1.SelectionMode = flexSelectionByRow     '單擊時選擇整行
    MSHFlexGrid1.FocusRect = flexFocusNone              '在當前但願的周圍畫一個焦點框
    MSHFlexGrid1.HighLight = flexHighlightWithFocus     '該值決定了所選定的單元是否突出顯示
        
    txtSQL = "select * from student_Info where "
    '組合SQL語句
    
    '判斷是否為空,不為空則查詢
    If Trim(Combo1.Text) = "" Or Trim(Combo4.Text) = "" Or Trim(Text1.Text) = "" Then
        MsgBox "請在第一行新增完整查詢條件!", vbOKOnly, "蘇轍提示您"
        Exit Sub
    Else
        txtSQL = txtSQL & fieldname(Trim(Combo1.Text)) & Trim(Combo4.Text) & "'" _
        & Trim(Text1.Text) & "'"
        
        If Combo7 <> "" Then
        '若第一個組合關係存在
            If Trim(Combo2.Text) = "" Or Trim(Combo5.Text) = "" Or Trim(Text2.Text) = "" Then
                MsgBox "您選擇了第一個組合關係,請在第二行新增完整查詢條件!", vbOKOnly, "蘇轍提示您"
                Exit Sub
            Else
                txtSQL = txtSQL & fieldname(Combo7.Text) & " " & fieldname(Trim(Combo2.Text)) & _
                "" & (Trim(Combo5.Text)) & "'" & Trim(Text2.Text) & "'"
            End If
            
        ElseIf Trim(Combo8.Text <> "") Then
        '第二個組合關係存在
            If Trim(Combo3.Text) = "" Or Trim(Combo6.Text) = "" Or Trim(Text3.Text) = "" Then
                MsgBox "您選擇了第二個組合關係,請在第三行新增完整查詢條件!", vbOKOnly, "蘇轍提示您"
                Exit Sub
            Else
            txtSQL = txtSQL & "" & fieldname(Combo3.Text) & "" & fieldname(Combo2.Text) & Trim(Combo5.Text) & _
            "'" & Trim(Text3.Text) & "'"
            End If
        End If
        Set mrcc = ExecuteSQL(txtSQL, MsgText)
        
    With MSHFlexGrid1
        .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) = "時間"
        '0代表表頭不空行,從第一行開始顯示
        
    If Not (mrcc.EOF Or mrcc.BOF) Then
    '判斷是否移動到資料集物件的最後一條記錄
    Do While Not mrcc.EOF
        .Rows = .Rows + 1
        .CellAlignment = 4
        .TextMatrix(.Rows - 1, 0) = Trim(mrcc.Fields(0))
        .TextMatrix(.Rows - 1, 1) = Trim(mrcc.Fields(1))
        .TextMatrix(.Rows - 1, 2) = Trim(mrcc.Fields(2))
        .TextMatrix(.Rows - 1, 3) = Trim(mrcc.Fields(3))
        .TextMatrix(.Rows - 1, 4) = Trim(mrcc.Fields(4))
        .TextMatrix(.Rows - 1, 5) = Trim(mrcc.Fields(5))
        .TextMatrix(.Rows - 1, 6) = Trim(mrcc.Fields(6))
        .TextMatrix(.Rows - 1, 7) = Trim(mrcc.Fields(7)) & ""
        .TextMatrix(.Rows - 1, 8) = Trim(mrcc.Fields(8)) & ""
        .TextMatrix(.Rows - 1, 9) = Trim(mrcc.Fields(10)) & ""
        .TextMatrix(.Rows - 1, 10) = Trim(mrcc.Fields(12)) & ""
        .TextMatrix(.Rows - 1, 11) = Trim(mrcc.Fields(13)) & ""
        mrcc.MoveNext
      Loop
    Else
        MsgBox "查詢無果!", vbOKOnly, "很遺憾"
    End If
    End With
    mrcc.Close
    End If
    
End Sub