机房收费系统——组合查询

    在组合查询中,我最喜欢是下面的这两个函数。
    由于需要重复的使用的原因 ,所以,编写函数。用时,直接调用call即可。
1、此函数解决了vb中的条件转化为数据库识别的字段问题。

<span style="font-size:18px;">Public Function FieldName(strFieldName As String) As String
'此函数可以让汉子转化为数据库中的字段
Select Case strFieldName
    Case "学号"
        FieldName = "studentNo"
    Case "卡号"
        FieldName = "cardno"
    Case "姓名"
        FieldName = "studentName"
    Case "金额"
        FieldName = "cash"
    Case "系别"
        FieldName = "department"
    Case "年级"
        FieldName = "grade"
    Case "班级"
        FieldName = "class"
    Case "性别"
        FieldName = "sex"
    Case "状态"
        FieldName = "status"
    Case "备注"
        FieldName = "explain"
    Case "类型"
        FieldName = "type"
    Case "日期"
        FieldName = "date"
    Case "时间"
        FieldName = "time"
End Select


End Function
</span>

2、此函数显示数据库中的信息。

<span style="font-size:18px;">Public Function View()
    With myflexgrid
            .Rows = 2
            Do While mrc.EOF = False
            .Rows = .Rows
            .CellAlignment = 4
            .TextMatrix(.Rows - 1, 0) = mrc.Fields(1)
                .TextMatrix(.Rows - 1, 1) = mrc.Fields(2)
                .TextMatrix(.Rows - 1, 2) = mrc.Fields(0)
                .TextMatrix(.Rows - 1, 3) = mrc.Fields(7)
                .TextMatrix(.Rows - 1, 4) = mrc.Fields(4)
                .TextMatrix(.Rows - 1, 5) = mrc.Fields(5)
                .TextMatrix(.Rows - 1, 6) = mrc.Fields(6)
                .TextMatrix(.Rows - 1, 7) = mrc.Fields(3)
                .TextMatrix(.Rows - 1, 8) = mrc.Fields(10)
                .TextMatrix(.Rows - 1, 9) = mrc.Fields(8)
                .TextMatrix(.Rows - 1, 10) = mrc.Fields(14)
                .TextMatrix(.Rows - 1, 11) = mrc.Fields(12)
                .TextMatrix(.Rows - 1, 12) = mrc.Fields(13)
                .Rows = .Rows + 1
                mrc.MoveNext
            Loop
            .Rows = .Rows - 1
        End With
End Function</span>

接下来就是组合 查询的关键了。

<span style="font-size:18px;">txtsql = "select * from student_Info where "
    '查询
    If CboRelation(0).Text = "" And CboRelation(1).Text = "" Then
        '单一条件查询
        If CboFile(0).Text = "" Or CboOperate(0).Text = "" Or txtInquire(0).Text = "" Then
            MsgBox "请输入完整信息", vbOKOnly & vbExclamation, "警告"
            CboFile(0).SetFocus
            Exit Sub
        Else
            txtsql = txtsql & FieldName(CboFile(0).Text) & Trim(CboOperate(0).Text) & "'" & txtInquire(0).Text & "'"
           
                    
        End If
    End If
        '两个组合的查询
    If CboRelation(0).Text <> "" And CboRelation(1).Text = "" Then
           If CboFile(0).Text = "" Or CboFile(1).Text = "" Or CboOperate(0).Text = "" Or CboOperate(1).Text = "" Or txtInquire(0).Text = "" Or txtInquire(1).Text = "" Then
                MsgBox "请输入完整信息", vbOKOnly & vbExclamation, "警告"
                CboFile(0).SetFocus
                Exit Sub
           Else
                txtsql = txtsql & FieldName(CboFile(0).Text) & Trim(CboOperate(0).Text) & "'" & txtInquire(0).Text & "'"
                If CboRelation(0).Text = "或" Then
                    txtsql = txtsql & " or " & FieldName(CboFile(1).Text) & Trim(CboOperate(1).Text) & "'" & txtInquire(1).Text & "'"
                End If
                If CboRelation(0).Text = "与" Then
                    txtsql = txtsql & " and " & FieldName(CboFile(1).Text) & Trim(CboOperate(1).Text) & "'" & txtInquire(1).Text & "'"
                End If
            End If
    End If
    
    '三个条件的组合
    If CboRelation(0).Text <> "" And CboRelation(1).Text <> "" Then
       
        If CboFile(0).Text = "" Or CboFile(1).Text = "" Or CboFile(2).Text = "" Or CboOperate(0).Text = "" Or CboOperate(1).Text = "" Or CboOperate(2).Text = "" Or txtInquire(0).Text = "" Or txtInquire(1).Text = "" Then
            MsgBox "请输入完整信息", vbOKOnly & vbExclamation, "警告"
            CboFile(0).SetFocus
            Exit Sub
        Else
            txtsql = txtsql & FieldName(CboFile(0).Text) & Trim(CboOperate(0).Text) & "'" & txtInquire(0).Text & "'"

            'txtsql = txtsql & FieldName(CboFile(0).Text) & Trim(CboOperate(0).Text) & "'" & txtInquire(0).Text & "'"
            If CboRelation(0).Text = "或" Then

                txtsql = txtsql & " or " & FieldName(CboFile(1).Text) & Trim(CboOperate(1).Text) & "'" & txtInquire(1).Text & "'"
           
                If CboRelation(1).Text = "或" Then
                    txtsql = txtsql & " or " & FieldName(CboFile(2).Text) & Trim(CboOperate(2).Text) & "'" & txtInquire(2).Text & "'"
              
                End If
                If CboRelation(1).Text = "与" Then
                    txtsql = txtsql & " and " & FieldName(CboFile(2).Text) & Trim(CboOperate(2).Text) & "'" & txtInquire(2).Text & "'"
               
                End If
            End If
            
            If CboRelation(0).Text = "与" Then
                txtsql = txtsql & " and " & FieldName(CboFile(1).Text) & Trim(CboOperate(1).Text) & "'" & txtInquire(1).Text & "'"
                If CboRelation(1).Text = "或" Then
                    txtsql = txtsql & " or " & FieldName(CboFile(2).Text) & Trim(CboOperate(2).Text) & "'" & txtInquire(2).Text & "'"
                End If
                If CboRelation(1).Text = "与" Then
                    txtsql = txtsql & " and " & FieldName(CboFile(2).Text) & Trim(CboOperate(2).Text) & "'" & txtInquire(2).Text & "'"
                End If
            End If
        End If
    End If

'打开数据库
    Set mrc = ExecuteSQL(txtsql, msgtext)
    If mrc.EOF = True Then
        MsgBox "没有相关信息", vbOKOnly & vbExclamation, "警告"
        CboFile(0).SetFocus
        Exit Sub
    Else
        Call View
    End If
            </span>

    在做组合查询之前,就听好多人说 组合 查询时机房中的难点之一。于是,自己也就把它放到了后边,也不敢那么轻易的开始了。开始之前,看了好多师哥师姐的博客,最后,决定一种思路,结合了一下他们的 优点。整理了一种适合我的思路。
    我发现看似复杂的事情,当你站在巨人的肩膀上时,真的就没有那么难了。






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