【机房收费系统】--组合查询

   机房收费系统有三个窗体用到了组合查询,在机房中也占重要的一部分。根据多个条件查询。

   首先我看到这个窗体是懵的,看着都难。 我先把基本框架弄了出来,然后心里有底之后再连接数据库查询,没有人会一遍就敲对的,都要一步一步调试,都是细节问题。这几个窗体还要需要把一些VB里的字段转换为数据库里面的字段,为了方便简洁,定义一个函数,用到的时候直接调用就可以,下面是代码。

Public Function Field(i As String) As String
    Select Case i
        Case "与"
            Field = "and"
        Case "或"
            Field = "or"
        Case "卡号"
            Field = "cardno"
        Case "姓名"
            Field = "studentname"
        Case "学号"
            Field = "studentno"
        Case "系别"
            Field = "department"
        Case "年级"
            Field = "grade"
        Case "班级"
            Field = "class"
        Case "性别"
            Field = "sex"
    End Select
End Function

   下面是组合查询的代码

    Dim mrc As ADODB.Recordset
    Dim txtsql As String
    Dim msgtext As String

    If Combo1.Text = "" Or Combo4.Text = "" Or Text1.Text = "" Then
        MsgBox "请填完整第一行查询条件!", 48, "提示"
        Exit Sub
    Else
        If Combo7.Text <> "" Then
            If Combo2.Text = "" Or Combo5.Text = "" Or Text2.Text = "" Then
                MsgBox "请填完整第二行查询条件!", 48, "提示"
            Exit Sub
            Else
                If Combo8.Text <> "" Then
                    If Combo3.Text = "" Or Combo6.Text = "" Or Text3.Text = "" Then
                        MsgBox "请填完整第三行查询条件!", 48, "提示"
                        Exit Sub
                    End If
                End If
            End If
        End If
    End If

    If Combo7.Text = "" And Combo8.Text = "" Then
        txtsql = "select * from student_info where " & Trim(Field(Combo1.Text)) & Trim(Combo4.Text) & "'" & Trim(Text1.Text) & "'"
        Set mrc = ExecuteSQL(txtsql, msgtext)

        If mrc.EOF Then
            MsgBox "没有记录!", 48, "提示"
        Exit Sub
        End If

    End If

    If Combo7.Text <> "" And Combo8.Text = "" Then
        txtsql = "select * from student_info where " & Trim(Field(Combo1.Text)) & Trim(Combo4.Text) & "'" & Trim(Text1.Text) & "'"
        txtsql = txtsql & Trim(Field(Combo7.Text)) & " " & Field(Combo2.Text) & (Combo5.Text) & "'" & Text2.Text & "'"
        Set mrc = ExecuteSQL(txtsql, msgtext)

        If mrc.EOF Then
            MsgBox "没有记录!", 48, "提示"
        Exit Sub
        End If
    End If

    If Combo7.Text <> "" And Combo8.Text <> "" Then
        txtsql = "select * from student_info where " & Trim(Field(Combo1.Text)) & Trim(Combo4.Text) & "'" & Trim(Text1.Text) & "'"
        txtsql = txtsql & Trim(Field(Combo7.Text)) & " " & Field(Combo2.Text) & (Combo5.Text) & "'" & Text2.Text & "'"
        txtsql = txtsql & Trim(Field(Combo8.Text)) & " " & Field(Combo3.Text) & (Combo6.Text) & "'" & Text3.Text & "'"
        Set mrc = ExecuteSQL(txtsql, msgtext)

        If mrc.EOF Then
            MsgBox "没有记录!", 48, "提示"
        Exit Sub
        End If
    End If

        With MSFlexGrid1
        .Rows = 1
        .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) = "日期"
        .TextMatrix(0, 12) = "时间"
        Do While Not mrc.EOF
            .ColAlignment(0) = flexAlignCenterCenter
            .Rows = .Rows + 1
            .TextMatrix(.Rows - 1, 0) = Trim(mrc.Fields(1))
            .TextMatrix(.Rows - 1, 1) = Trim(mrc.Fields(2))
            .TextMatrix(.Rows - 1, 2) = Trim(mrc.Fields(0))
            .TextMatrix(.Rows - 1, 3) = Trim(mrc.Fields(7))
            .TextMatrix(.Rows - 1, 4) = Trim(mrc.Fields(4))
            .TextMatrix(.Rows - 1, 5) = Trim(mrc.Fields(5))
            .TextMatrix(.Rows - 1, 6) = Trim(mrc.Fields(6))
            .TextMatrix(.Rows - 1, 7) = Trim(mrc.Fields(3))
            .TextMatrix(.Rows - 1, 8) = Trim(mrc.Fields(10))
            .TextMatrix(.Rows - 1, 9) = Trim(mrc.Fields(8))
            .TextMatrix(.Rows - 1, 10) = Trim(mrc.Fields(14))
            .TextMatrix(.Rows - 1, 11) = Trim(mrc.Fields(12))
            .TextMatrix(.Rows - 1, 12) = Trim(mrc.Fields(13))
            mrc.MoveNext
        Loop
    End With

    mrc.Close

猜你喜欢

转载自blog.csdn.net/ywq1016243402/article/details/80462253