机房收费系统(二)——组合查询

             一个组合查询做了两天,也纠结了两天,现在就晒晒这两天的经历吧。刚看到这个窗体,觉得很复杂,比起学生信息管理系统中的那个组合查询复杂多了,于是就选择最后做这个窗体。

        机房收费系统(二)——组合查询_第1张图片

首先说说刚开始我使用的方式:

        先对字段名、操作符、查询内容进行选择,然后再看组合关系,想着依照此窗体的操作顺序来进行代码的书写,可是到了最后,却发现一个问题,第一个组合查询做出来了,但是其他的组合查询虽然也能做出来,但是其代码量大,重复多,并且循环关系复杂,没有新意。这让我很是郁闷,也就放弃了按照窗体操作顺序来进行代码的书写。

现在使用的方式:

       后来听另一个朋友的讲解,我有了新思路。

        以组合关系作为判断依据,将SQL语句分为三类:

                第一类:如果没有选择组合关系;

                第二类:只选择了一个组合关系;

                第三类:两个组合关系均选择。

       如果选择了第一个组合关系,则blnjudge1=true,如果选择了第二个组合关系,则blnjudge2=true。

       然后再拼接SQL语句,这样就比前面那种方法简单多了。

       具体代码如下:

Private Sub cmdQuery_Click()  '查询

    Dim ObjRs As ADODB.Recordset
    Dim strtxtsql As String
    Dim strMsgText As String
    Dim blnJudge1 As Boolean
    Dim blnJudge2 As Boolean
    Dim strStdinfo(3) As String    '字段名
    Dim stroperator(3)  As String   '操作符
    Dim strQuery(3) As String      '查询语句
    Dim strRelationship(3) As String  '组合关系

    strtxtsql = "select * from studentinfo where"
    '字段名选择
    If Not TestText(Trim$(cmbStdInfo1.Text)) Then
        MsgBox "请选择字段名!", vbOKOnly + vbExclamation, "警告"
        cmbStdInfo1.SetFocus
        Exit Sub
    Else
        Select Case cmbStdInfo1.ListIndex
            Case 0
            strStdinfo(0) = "cardno"
            Case 1
            strStdinfo(0) = "studentid"
            Case 2
            strStdinfo(0) = "studentname"
            Case 3
            strStdinfo(0) = "sex"
            Case 4
            strStdinfo(0) = "dapartment"
            Case 5
            strStdinfo(0) = "grade"
            Case 6
            strStdinfo(0) = "class"
        End Select
    End If
    
    '操作符选择
    If Not TestText(Trim$(cmbOperator1.Text)) Then
        MsgBox "请选择操作符!", vbOKOnly + vbExclamation, "警告"
        cmbOperator1.SetFocus
        Exit Sub
    Else
        Select Case cmbOperator1.ListIndex
            Case 0
            stroperator(0) = "<"
            Case 1
            stroperator(0) = ">"
            Case 2
            stroperator(0) = "="
            Case 3
            stroperator(0) = "<>"
        End Select
    End If
    
    '查询内容输入
    If Not TestText(Trim$(txtQuery1.Text)) Then
        MsgBox "要查询的内容!"
        cmbStdInfo1.SetFocus
        Exit Sub
    Else
        strQuery(0) = Trim$(txtQuery1.Text)
    End If
    
    If Not TestText(Trim$(cmbRelationship1.Text)) Then
        strtxtsql = strtxtsql & " " & strStdinfo(0) & " " & stroperator(0) & "'" & strQuery(0) & "'"
        Set ObjRs = ExecuteSQL(strtxtsql, strMsgText)
        '若第一个判定符为空
        If ObjRs.EOF = True Then
            MsgBox "没有此记录!", vbOKOnly + vbExclamation, "警告"
            MSFlexGrid1.Clear
            Call viewData
            Exit Sub
        Else
            Call viewData
            Do While Not ObjRs.EOF
                With MSFlexGrid1
                    .Rows = .Rows + 1
                    .CellAlignment = 4
                    .TextMatrix(.Rows - 1, 0) = Trim$(ObjRs.Fields(0))
                    .TextMatrix(.Rows - 1, 1) = Trim$(ObjRs.Fields(1))
                    .TextMatrix(.Rows - 1, 2) = Trim$(ObjRs.Fields(2))
                    .TextMatrix(.Rows - 1, 3) = Trim$(ObjRs.Fields(3))
                    .TextMatrix(.Rows - 1, 4) = Trim$(ObjRs.Fields(4))
                    .TextMatrix(.Rows - 1, 5) = Trim$(ObjRs.Fields(5))
                    .TextMatrix(.Rows - 1, 6) = Trim$(ObjRs.Fields(6))
                    .TextMatrix(.Rows - 1, 7) = Trim$(ObjRs.Fields(7))
                    .TextMatrix(.Rows - 1, 8) = Trim$(ObjRs.Fields(8))
                    .TextMatrix(.Rows - 1, 9) = Trim$(ObjRs.Fields(9))
                    ObjRs.MoveNext
                End With
            Loop
            ObjRs.Close
            Exit Sub
        End If
    Else
        '第一个判定符不为空
        blnJudge2 = True
        Select Case cmbRelationship1.ListIndex
            Case 0
            strRelationship(0) = "or"
            Case 1
            strRelationship(0) = "and"
        End Select
        
        '选择字段
        If Not TestText(Trim$(cmbStdInfo2.Text)) Then
            MsgBox "请选择字段名!", vbOKOnly + vbExclamation, "警告"
            cmbStdInfo2.SetFocus
            Exit Sub
        Else
            Select Case cmbStdInfo2.ListIndex
                Case 0
                strStdinfo(1) = "cardno"
                Case 1
                strStdinfo(1) = "studentid"
                Case 2
                strStdinfo(1) = "studentname"
                Case 3
                strStdinfo(1) = "sex"
                Case 4
                strStdinfo(1) = "dapartment"
                Case 5
                strStdinfo(1) = "grade"
                Case 6
                strStdinfo(1) = "class"
            End Select
        End If
        
        '操作符选择
        If Not TestText(Trim$(cmbOperator2.Text)) Then
            MsgBox "请选择操作符!", vbOKOnly + vbExclamation, "警告"
            cmbOperator2.SetFocus
            Exit Sub
        Else
            Select Case cmbOperator2.ListIndex
                Case 0
                stroperator(1) = "<"
                Case 1
                stroperator(1) = ">"
                Case 2
                stroperator(1) = "="
                Case 3
                stroperator(1) = "<>"
            End Select
        End If
        
        '查询内容输入
        If Not TestText(Trim$(txtQuery2.Text)) Then
            MsgBox "请输入要查询的内容!"
            txtQuery2.SetFocus
            Exit Sub
        Else
            strQuery(1) = Trim$(txtQuery2.Text)
        End If
    End If
    
    '若选择了第二个判定符
    If TestText(Trim$(cmbRelationship2.Text)) Then
        blnJudge1 = True
        Select Case cmbRelationship2.ListIndex
            Case 0
            strRelationship(1) = "or"
            Case 1
            strRelationship(1) = "and"
        End Select
        
        '字段选择
        If Not TestText(Trim$(cmbStdInfo3.Text)) Then
            MsgBox "请选择字段名!"
            cmbStdInfo3.SetFocus
            Exit Sub
        Else
            Select Case cmbStdInfo3.ListIndex
                Case 0
                strStdinfo(2) = "cardno"
                Case 1
                strStdinfo(2) = "studentid"
                Case 2
                strStdinfo(2) = "studentname"
                Case 3
                strStdinfo(2) = "sex"
                Case 4
                strStdinfo(2) = "dapartment"
                Case 5
                strStdinfo(2) = "grade"
                Case 6
                strStdinfo(2) = "class"
            End Select
        End If
         
         '操作符选择
        If Not TestText(Trim$(cmbOperator3.Text)) Then
            MsgBox "请选择操作符!", vbOKOnly + vbExclamation, "警告"
            cmbOperator3.SetFocus
            Exit Sub
        Else
            Select Case cmbOperator3.ListIndex
                Case 0
                stroperator(2) = "<"
                Case 1
                stroperator(2) = ">"
                Case 2
                stroperator(2) = "="
                Case 3
                stroperator(2) = "<>"
            End Select
        End If
        
        '查询内容输入
        If Not TestText(Trim$(txtQuery3.Text)) Then
            MsgBox "请输入内容!"
            txtQuery3.SetFocus
            Exit Sub
        Else
            strQuery(2) = Trim$(txtQuery3.Text)
        End If
    End If
     
    If blnJudge2 = True And blnJudge1 = False Then
        '一真一假,且关系为and
        If strRelationship(0) = "and" Then
            strtxtsql = strtxtsql & " " & strStdinfo(0) & " " & stroperator(0) & " '" & strQuery(0) & "' and" & " " & strStdinfo(1) & " " & stroperator(1) & " '" & strQuery(1) & "'"
            Set ObjRs = ExecuteSQL(strtxtsql, strMsgText)
            If ObjRs.EOF = True Then
                MsgBox "没有此记录!", vbOKOnly + vbExclamation, "警告"
                MSFlexGrid1.Clear
                Call viewData
'                blnJudge = True
                Exit Sub
            Else
                Call viewData
                Do While Not ObjRs.EOF
                    With MSFlexGrid1
                        .Rows = .Rows + 1
                        .CellAlignment = 4
                        .TextMatrix(.Rows - 1, 0) = Trim$(ObjRs.Fields(0))
                        .TextMatrix(.Rows - 1, 1) = Trim$(ObjRs.Fields(1))
                        .TextMatrix(.Rows - 1, 2) = Trim$(ObjRs.Fields(2))
                        .TextMatrix(.Rows - 1, 3) = Trim$(ObjRs.Fields(3))
                        .TextMatrix(.Rows - 1, 4) = Trim$(ObjRs.Fields(4))
                        .TextMatrix(.Rows - 1, 5) = Trim$(ObjRs.Fields(5))
                        .TextMatrix(.Rows - 1, 6) = Trim$(ObjRs.Fields(6))
                        .TextMatrix(.Rows - 1, 7) = Trim$(ObjRs.Fields(7))
                        .TextMatrix(.Rows - 1, 8) = Trim$(ObjRs.Fields(8))
                        .TextMatrix(.Rows - 1, 9) = Trim$(ObjRs.Fields(9))
                        ObjRs.MoveNext
                    End With
                Loop
                ObjRs.Close
                Exit Sub
            End If
        Else
            '一真一假,且关系为or
            strtxtsql = strtxtsql & " " & strStdinfo(0) & " " & stroperator(0) & " '" & strQuery(0) & "' or" & " " & strStdinfo(1) & " " & stroperator(1) & " '" & strQuery(1) & "'"
            Set ObjRs = ExecuteSQL(strtxtsql, strMsgText)
            If ObjRs.EOF = True Then
                MsgBox "没有此记录!", vbOKOnly + vbExclamation, "警告"
                MSFlexGrid1.Clear
                Call viewData
'                blnJudge = True
                Exit Sub
            Else
                Call viewData
                Do While Not ObjRs.EOF
                    With MSFlexGrid1
                        .Rows = .Rows + 1
                        .CellAlignment = 4
                        .TextMatrix(.Rows - 1, 0) = Trim$(ObjRs.Fields(0))
                        .TextMatrix(.Rows - 1, 1) = Trim$(ObjRs.Fields(1))
                        .TextMatrix(.Rows - 1, 2) = Trim$(ObjRs.Fields(2))
                        .TextMatrix(.Rows - 1, 3) = Trim$(ObjRs.Fields(3))
                        .TextMatrix(.Rows - 1, 4) = Trim$(ObjRs.Fields(4))
                        .TextMatrix(.Rows - 1, 5) = Trim$(ObjRs.Fields(5))
                        .TextMatrix(.Rows - 1, 6) = Trim$(ObjRs.Fields(6))
                        .TextMatrix(.Rows - 1, 7) = Trim$(ObjRs.Fields(7))
                        .TextMatrix(.Rows - 1, 8) = Trim$(ObjRs.Fields(8))
                        .TextMatrix(.Rows - 1, 9) = Trim$(ObjRs.Fields(9))
                        ObjRs.MoveNext
                    End With
                Loop
                ObjRs.Close
                Exit Sub
            End If
        End If
    End If

    If blnJudge1 = True And blnJudge2 = True Then
        '两个均为真,且两者均为and
        If strRelationship(1) = "and" And strRelationship(0) = "and" Then
            strtxtsql = strtxtsql & " " & strStdinfo(0) & " " & stroperator(0) & " '" & strQuery(0) & "' and" & " " & strStdinfo(1) & " " & stroperator(1) & " '" & strQuery(1) & "' and" & " " & strStdinfo(2) & " " & stroperator(2) & " '" & strQuery(2) & "'"
            Set ObjRs = ExecuteSQL(strtxtsql, strMsgText)
            If ObjRs.EOF = True Then
                MsgBox "没有此记录!", vbOKOnly + vbExclamation, "警告"
                MSFlexGrid1.Clear
                Call viewData
'                blnJudge = True
                Exit Sub
            Else
                Call viewData
                Do While Not ObjRs.EOF
                    With MSFlexGrid1
                        .Rows = .Rows + 1
                        .CellAlignment = 4
                        .TextMatrix(.Rows - 1, 0) = Trim$(ObjRs.Fields(0))
                        .TextMatrix(.Rows - 1, 1) = Trim$(ObjRs.Fields(1))
                        .TextMatrix(.Rows - 1, 2) = Trim$(ObjRs.Fields(2))
                        .TextMatrix(.Rows - 1, 3) = Trim$(ObjRs.Fields(3))
                        .TextMatrix(.Rows - 1, 4) = Trim$(ObjRs.Fields(4))
                        .TextMatrix(.Rows - 1, 5) = Trim$(ObjRs.Fields(5))
                        .TextMatrix(.Rows - 1, 6) = Trim$(ObjRs.Fields(6))
                        .TextMatrix(.Rows - 1, 7) = Trim$(ObjRs.Fields(7))
                        .TextMatrix(.Rows - 1, 8) = Trim$(ObjRs.Fields(8))
                        .TextMatrix(.Rows - 1, 9) = Trim$(ObjRs.Fields(9))
                        ObjRs.MoveNext
                    End With
                Loop
                ObjRs.Close
                Exit Sub
            End If
        End If
         
         '两者均为or
        If strRelationship(1) = "or" And strRelationship(0) = "or" Then
            strtxtsql = strtxtsql & " " & strStdinfo(0) & " " & stroperator(0) & " '" & strQuery(0) & "' or" & " " & strStdinfo(1) & " " & stroperator(1) & " '" & strQuery(1) & "' or" & " " & strStdinfo(2) & " " & stroperator(2) & " '" & strQuery(2) & "'"
            Set ObjRs = ExecuteSQL(strtxtsql, strMsgText)
            If ObjRs.EOF = True Then
                MsgBox "没有此记录!", vbOKOnly + vbExclamation, "警告"
                MSFlexGrid1.Clear
                Call viewData
'                blnJudge = True
                Exit Sub
            Else
                Call viewData
                Do While Not ObjRs.EOF
                    With MSFlexGrid1
                        .Rows = .Rows + 1
                        .CellAlignment = 4
                        .TextMatrix(.Rows - 1, 0) = Trim$(ObjRs.Fields(0))
                        .TextMatrix(.Rows - 1, 1) = Trim$(ObjRs.Fields(1))
                        .TextMatrix(.Rows - 1, 2) = Trim$(ObjRs.Fields(2))
                        .TextMatrix(.Rows - 1, 3) = Trim$(ObjRs.Fields(3))
                        .TextMatrix(.Rows - 1, 4) = Trim$(ObjRs.Fields(4))
                        .TextMatrix(.Rows - 1, 5) = Trim$(ObjRs.Fields(5))
                        .TextMatrix(.Rows - 1, 6) = Trim$(ObjRs.Fields(6))
                        .TextMatrix(.Rows - 1, 7) = Trim$(ObjRs.Fields(7))
                        .TextMatrix(.Rows - 1, 8) = Trim$(ObjRs.Fields(8))
                        .TextMatrix(.Rows - 1, 9) = Trim$(ObjRs.Fields(9))
                        ObjRs.MoveNext
                    End With
                Loop
                ObjRs.Close
                Exit Sub
            End If
        End If
        
        '一个为and一个为or
        If strRelationship(1) = "and" And strRelationship(0) = "or" Then
            strtxtsql = strtxtsql & " " & strStdinfo(0) & " " & stroperator(0) & " '" & strQuery(0) & "' or" & " " & strStdinfo(1) & " " & stroperator(1) & " '" & strQuery(1) & "' and" & " " & strStdinfo(2) & " " & stroperator(2) & " '" & strQuery(2) & "'"
            Set ObjRs = ExecuteSQL(strtxtsql, strMsgText)
            If ObjRs.EOF = True Then
                MsgBox "没有此记录!", vbOKOnly + vbExclamation, "警告"
                MSFlexGrid1.Clear
                Call viewData
'                blnJudge = True
                Exit Sub
            Else
                Call viewData
                Do While Not ObjRs.EOF
                    With MSFlexGrid1
                        .Rows = .Rows + 1
                        .CellAlignment = 4
                        .TextMatrix(.Rows - 1, 0) = Trim$(ObjRs.Fields(0))
                        .TextMatrix(.Rows - 1, 1) = Trim$(ObjRs.Fields(1))
                        .TextMatrix(.Rows - 1, 2) = Trim$(ObjRs.Fields(2))
                        .TextMatrix(.Rows - 1, 3) = Trim$(ObjRs.Fields(3))
                        .TextMatrix(.Rows - 1, 4) = Trim$(ObjRs.Fields(4))
                        .TextMatrix(.Rows - 1, 5) = Trim$(ObjRs.Fields(5))
                        .TextMatrix(.Rows - 1, 6) = Trim$(ObjRs.Fields(6))
                        .TextMatrix(.Rows - 1, 7) = Trim$(ObjRs.Fields(7))
                        .TextMatrix(.Rows - 1, 8) = Trim$(ObjRs.Fields(8))
                        .TextMatrix(.Rows - 1, 9) = Trim$(ObjRs.Fields(9))
                        ObjRs.MoveNext
                    End With
                Loop
                ObjRs.Close
                Exit Sub
            End If
        End If
        
        '一个为or一个为and
        If strRelationship(1) = "or" And strRelationship(0) = "and" Then
            strtxtsql = strtxtsql & " " & strStdinfo(0) & " " & stroperator(0) & " '" & strQuery(0) & "' and" & " " & strStdinfo(1) & " " & stroperator(1) & " '" & strQuery(1) & "' or" & " " & strStdinfo(2) & " " & stroperator(2) & " '" & strQuery(2) & "'"
            Set ObjRs = ExecuteSQL(strtxtsql, strMsgText)
            If ObjRs.EOF = True Then
                MsgBox "没有此记录!", vbOKOnly + vbExclamation, "警告"
                MSFlexGrid1.Clear
                Call viewData
'                blnJudge = True
                Exit Sub
            Else
                Call viewData
                Do While Not ObjRs.EOF
                    With MSFlexGrid1
                        .Rows = .Rows + 1
                        .CellAlignment = 4
                        .TextMatrix(.Rows - 1, 0) = Trim$(ObjRs.Fields(0))
                        .TextMatrix(.Rows - 1, 1) = Trim$(ObjRs.Fields(1))
                        .TextMatrix(.Rows - 1, 2) = Trim$(ObjRs.Fields(2))
                        .TextMatrix(.Rows - 1, 3) = Trim$(ObjRs.Fields(3))
                        .TextMatrix(.Rows - 1, 4) = Trim$(ObjRs.Fields(4))
                        .TextMatrix(.Rows - 1, 5) = Trim$(ObjRs.Fields(5))
                        .TextMatrix(.Rows - 1, 6) = Trim$(ObjRs.Fields(6))
                        .TextMatrix(.Rows - 1, 7) = Trim$(ObjRs.Fields(7))
                        .TextMatrix(.Rows - 1, 8) = Trim$(ObjRs.Fields(8))
                        .TextMatrix(.Rows - 1, 9) = Trim$(ObjRs.Fields(9))
                        ObjRs.MoveNext
                    End With
                Loop
                ObjRs.Close
                Exit Sub
            End If
        End If
    End If
    
End Sub
Sub viewData()


    With MSFlexGrid1
        .Rows = 1
        .Cols = 10
        .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) = "备注"
    End With


End Sub

       

   

        

   

        

你可能感兴趣的:(机房收费系统(二)——组合查询)