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

           终于做到组合查询这里了,而且我也掌握了它的方法,根据帅博客改编:

       组合查询也不是那么难,之前看到别人总说这个话题,自己就先给它定位了,很难,现在看来真的不能给自己下定位,不能给自己没有接触过的东西随便下定义,就像小马过河的故事!

       我来说说我的成长经历:

       首先,我先自己想了一个办法,代码如下:

   

txtSQL = "select * from line_info where "
     If Combo3.Text = "卡号" Then

            '判断操作符和查询的内容是否为空
            If Trim(Combo6.Text) = "" Then
                MsgBox "请输入操作符!", vbOKOnly + vbExclamation
                Combo6.SetFocus
                Exit Sub
            ElseIf Trim(Text3.Text) = "" Then

                    MsgBox "请输入要查询的内容!", vbOKOnly + vbExclamation
                    Text3.SetFocus
                    Exit Sub
            ElseIf Not IsNumeric(Trim(Text3.Text)) Then
                        MsgBox "请输入数字!"

            Exit Sub
                     '如果不为空的话,看是否为”=“
            ElseIf Combo6.Text = "=" Then
                      '调出数据库中为=号的字段
                txtSQL = txtSQL & "cardno='" & Trim(Text3.Text) & "'"
                '同理,如果不是“=”,看是否为“<”
            ElseIf Combo6.Text = "<" Then
                 txtSQL = txtSQL & "cardno<'" & Trim(Text3.Text) & "'"
                 '同理,如果不是“=”,看是否为“<”
            ElseIf Combo6.Text = ">" Then
                 txtSQL = txtSQL & "cardno>'" & Trim(Text3.Text) & "'"
                 '这时,就剩下一个《》了
            Else
                txtSQL = txtSQL & "cardno<>'" & Trim(Text3.Text) & "'"
            End If

      ElseIf Combo3.Text = "姓名" Then

             '判断操作符和查询的内容是否为空
            If Trim(Combo6.Text) = "" Then
                MsgBox "请输入操作符!", vbOKOnly + vbExclamation
                Combo6.SetFocus
                Exit Sub
            ElseIf Trim(Text3.Text) = "" Then

                    MsgBox "请输入要查询的内容!", vbOKOnly + vbExclamation
                    Text3.SetFocus
                    Exit Sub
                     '如果不为空的话,看是否为”=“
            ElseIf Combo6.Text = "=" Then
                      '调出数据库中为=号的字段
                txtSQL = txtSQL & "studentname='" & Trim(Text3.Text) & "'"
                '同理,如果不是“=”,看是否为“<”
            ElseIf Combo6.Text = "<" Then
                 txtSQL = txtSQL & "studentname<'" & Trim(Text3.Text) & "'"
                 '同理,如果不是“=”,看是否为“<”
            ElseIf Combo6.Text = ">" Then
                 txtSQL = txtSQL & "studentname>'" & Trim(Text3.Text) & "'"
                 '这时,就剩下一个《》了
            Else
                txtSQL = txtSQL & "student<>'" & Trim(Text3.Text) & "'"
            End If


        ElseIf Combo3.Text = "上机日期" Then

             '判断操作符和查询的内容是否为空
            If Trim(Combo6.Text) = "" Then
                MsgBox "请输入操作符!", vbOKOnly + vbExclamation
                Combo6.SetFocus
                Exit Sub
            ElseIf Trim(Text3.Text) = "" Then

                    MsgBox "请输入要查询的内容!", vbOKOnly + vbExclamation
                    Text3.SetFocus
                    Exit Sub


            ElseIf Not IsDate(Text3.Text) Then
                   MsgBox "请输入正确的日期类型!(yyyy-mm-dd)"
                   Text3.SetFocus
                   Exit Sub
                   Text3.Text = Format(Text3.Text, "yyyy-mm-dd")
                     '如果不为空的话,看是否为”=“
            ElseIf Combo6.Text = "=" Then

                      '调出数据库中为=号的字段
                txtSQL = txtSQL & "ondate='" & Trim(Text3.Text) & "'"
                '同理,如果不是“=”,看是否为“<”
            ElseIf Combo6.Text = "<" Then
                 txtSQL = txtSQL & "ondate<'" & Trim(Text3.Text) & "'"
                 '同理,如果不是“=”,看是否为“<”
            ElseIf Combo6.Text = ">" Then
                 txtSQL = txtSQL & "ondate>'" & Trim(Text3.Text) & "'"
                 '这时,就剩下一个《》了
            Else
                txtSQL = txtSQL & "ondate<>'" & Trim(Text3.Text) & "'"
            End If

        ElseIf Combo3.Text = "上机时间" Then

             '判断操作符和查询的内容是否为空
            If Trim(Combo6.Text) = "" Then
                MsgBox "请输入操作符!", vbOKOnly + vbExclamation
                Combo6.SetFocus
                Exit Sub
            ElseIf Trim(Text3.Text) = "" Then

                    MsgBox "请输入要查询的内容!", vbOKOnly + vbExclamation
                    Text3.SetFocus
                    Exit Sub
            ElseIf Not IsDate(Text3.Text) Then
                   MsgBox "请输入正确的时间类型!(00:00:00)"
                   Text3.SetFocus
                   Exit Sub
                   Text3.Text = Format(Text3.Text, "00:00:00")

                     '如果不为空的话,看是否为”=“
            ElseIf Combo6.Text = "=" Then
                      '调出数据库中为=号的字段
                txtSQL = txtSQL & "ontime='" & Trim(Text3.Text) & "'"
                '同理,如果不是“=”,看是否为“<”
            ElseIf Combo6.Text = "<" Then
                 txtSQL = txtSQL & "ontime<'" & Trim(Text3.Text) & "'"
                 '同理,如果不是“=”,看是否为“<”
            ElseIf Combo6.Text = ">" Then
                 txtSQL = txtSQL & "ontime>'" & Trim(Text3.Text) & "'"
                 '这时,就剩下一个《》了
            Else
                txtSQL = txtSQL & "ontime<>'" & Trim(Text3.Text) & "'"
            End If

        ElseIf Combo3.Text = "下机日期" Then
             '判断操作符和查询的内容是否为空
            If Trim(Combo6.Text) = "" Then
                MsgBox "请输入操作符!", vbOKOnly + vbExclamation
                Combo6.SetFocus
                Exit Sub
            ElseIf Trim(Text3.Text) = "" Then

                    MsgBox "请输入要查询的内容!", vbOKOnly + vbExclamation
                    Text3.SetFocus
                    Exit Sub
            ElseIf Not IsDate(Text3.Text) Then
                   MsgBox "请输入正确的日期类型!(yyyy-mm-dd)"
                   Text3.SetFocus
                   Exit Sub
                   Text3.Text = Format(Text3.Text, "yyyy-mm-dd")

                     '如果不为空的话,看是否为”=“
            ElseIf Combo6.Text = "=" Then
                      '调出数据库中为=号的字段
                txtSQL = txtSQL & "offdate='" & Trim(Text3.Text) & "'"
                '同理,如果不是“=”,看是否为“<”
            ElseIf Combo6.Text = "<" Then
                 txtSQL = txtSQL & "offdate<'" & Trim(Text3.Text) & "'"
                 '同理,如果不是“=”,看是否为“<”
            ElseIf Combo6.Text = ">" Then
                 txtSQL = txtSQL & "offdate>'" & Trim(Text3.Text) & "'"
                 '这时,就剩下一个《》了
            Else
                txtSQL = txtSQL & "offdate<>'" & Trim(Text3.Text) & "'"
            End If


        ElseIf Combo3.Text = "下机时间" Then

                '判断操作符和查询的内容是否为空
            If Trim(Combo6.Text) = "" Then
                MsgBox "请输入操作符!", vbOKOnly + vbExclamation
                Combo6.SetFocus
                Exit Sub
            ElseIf Trim(Text3.Text) = "" Then

                    MsgBox "请输入要查询的内容!", vbOKOnly + vbExclamation
                    Text3.SetFocus
                    Exit Sub
            ElseIf Not IsDate(Text3.Text) Then
                   MsgBox "请输入正确的时间类型!(00:00:00)"
                   Text3.SetFocus
                   Exit Sub
                   Text3.Text = Format(Text3.Text, "00:00:00")


                     '如果不为空的话,看是否为”=“
            ElseIf Combo6.Text = "=" Then
                      '调出数据库中为=号的字段
                txtSQL = txtSQL & "offtime='" & Trim(Text3.Text) & "'"
                '同理,如果不是“=”,看是否为“<”
            ElseIf Combo6.Text = "<" Then
                 txtSQL = txtSQL & "offtime<'" & Trim(Text3.Text) & "'"
                 '同理,如果不是“=”,看是否为“<”
            ElseIf Combo6.Text = ">" Then
                 txtSQL = txtSQL & "offtime>'" & Trim(Text3.Text) & "'"
                 '这时,就剩下一个《》了
            Else
                txtSQL = txtSQL & "offtime<>'" & Trim(Text3.Text) & "'"
            End If

        ElseIf Combo3.Text = "消费金额" Then
              '判断操作符和查询的内容是否为空
            If Trim(Combo6.Text) = "" Then
                MsgBox "请输入操作符!", vbOKOnly + vbExclamation
                Combo6.SetFocus
                Exit Sub
            ElseIf Trim(Text3.Text) = "" Then

                    MsgBox "请输入要查询的内容!", vbOKOnly + vbExclamation
                    Text3.SetFocus
                    Exit Sub
            ElseIf Not IsNumeric(Trim(Text3.Text)) Then
                MsgBox "请输入数字!"
                Text3.SetFocus
                Exit Sub

                     '如果不为空的话,看是否为”=“
            ElseIf Combo6.Text = "=" Then
                      '调出数据库中为=号的字段
                txtSQL = txtSQL & "cash='" & Trim(Text3.Text) & "'"
                '同理,如果不是“=”,看是否为“<”
            ElseIf Combo6.Text = "<" Then
                 txtSQL = txtSQL & "cash<'" & Trim(Text3.Text) & "'"
                 '同理,如果不是“=”,看是否为“<”
            ElseIf Combo6.Text = ">" Then
                 txtSQL = txtSQL & "cash>'" & Trim(Text3.Text) & "'"
                 '这时,就剩下一个《》了
            Else
                txtSQL = txtSQL & "cash<>'" & Trim(Text3.Text) & "'"
            End If

        ElseIf Combo3.Text = "余额" Then
              '判断操作符和查询的内容是否为空
            If Trim(Combo6.Text) = "" Then
                MsgBox "请输入操作符!", vbOKOnly + vbExclamation
                Combo6.SetFocus
                Exit Sub
            ElseIf Trim(Text3.Text) = "" Then

                    MsgBox "请输入要查询的内容!", vbOKOnly + vbExclamation
                    Text3.SetFocus
                    Exit Sub
            ElseIf Not IsNumeric(Trim(Text3.Text)) Then
                MsgBox "请输入数字!"
                Text3.SetFocus
                Exit Sub

                     '如果不为空的话,看是否为”=“
            ElseIf Combo6.Text = "=" Then
                      '调出数据库中为=号的字段
                txtSQL = txtSQL & "cash='" & Trim(Text3.Text) & "'"
                '同理,如果不是“=”,看是否为“<”
            ElseIf Combo6.Text = "<" Then
                 txtSQL = txtSQL & "cash<'" & Trim(Text3.Text) & "'"
                 '同理,如果不是“=”,看是否为“<”
            ElseIf Combo6.Text = ">" Then
                 txtSQL = txtSQL & "cash>'" & Trim(Text3.Text) & "'"
                 '这时,就剩下一个《》了
            Else
                txtSQL = txtSQL & "cash<>'" & Trim(Text3.Text) & "'"
            End If

         Else  '这样在这里就是“备注”的代码了
               '判断操作符和查询的内容是否为空
            If Trim(Combo6.Text) = "" Then
                MsgBox "请输入操作符!", vbOKOnly + vbExclamation
                Combo6.SetFocus
                Exit Sub
            ElseIf Trim(Text3.Text) = "" Then

                    MsgBox "请输入备注的电脑名称!", vbOKOnly + vbExclamation
                    Text3.SetFocus
                    Exit Sub
                     '如果不为空的话,看是否为”=“
            ElseIf Combo6.Text = "=" Then
                      '调出数据库中为=号的字段
                txtSQL = txtSQL & "computer='" & Trim(Text3.Text) & "'"
                '同理,如果不是“=”,看是否为“<”
            ElseIf Combo6.Text = "<" Then
                 txtSQL = txtSQL & "computer<'" & Trim(Text3.Text) & "'"
                 '同理,如果不是“=”,看是否为“<”
            ElseIf Combo6.Text = ">" Then
                 txtSQL = txtSQL & "computer>'" & Trim(Text3.Text) & "'"
                 '这时,就剩下一个《》了
            Else
                txtSQL = txtSQL & "computer<>'" & Trim(Text3.Text) & "'"
            End If                                                            '判断操作符和查询的内容是否为空

      End If
             这个看起来很复杂吧,没错,我忽略了计算机的能力,没有让它给我做工(赋值,循环的改变数据)。

             我放弃了这个思路,决定参考别人的做法:


Private Sub Command2_Click()
    If Combo1.Text = "" Or Combo4.Text = "" Or Text1.Text = "" Then
        MsgBox "请输入正确的查询内容!"
    Exit Sub
    End If
    
     txtSQL = "select * from line_info where " '这叫模糊查询
     
     If Combo7.Text = "" And Combo8.Text = "" Then
        '这是查询的第一行
        txtSQL = txtSQL & field(Combo1.Text) & Trim(Combo4.Text) & "'" & Text1.Text & "'"
        
    ElseIf Combo7.Text <> "" And Combo8.Text = "" Then
        If Combo2.Text = "" Or Combo4.Text = "" Or Text2.Text = "" Then
            MsgBox "请输入正确的查询方式!"
        Exit Sub
        End If
        '先查第一行,在它的基础上再进行and or 第二行
        txtSQL = txtSQL & field(Combo1.Text) & Trim(Combo4.Text) & "'" & Text1.Text & "'"
        '查询第二行
        txtSQL = txtSQL & field(Combo7.Text) & " " & field(Combo2.Text) & Trim(Combo5.Text) & "'" & Text2.Text & "'"
    ElseIf Combo7.Text = "" And Combo8.Text <> "" Then
        If Combo3.Text = "" Or Combo5.Text = "" Or Text3.Text = "" Then
            MsgBox "请输入正确的查询方式!"
        Exit Sub
        End If
    '查询第一行
        txtSQL = txtSQL & field(Combo1.Text) & Trim(Combo4.Text) & "'" & Text1.Text & "'"
    '查询第三行
        txtSQL = txtSQL & field(Combo8.Text) & " " & field(Combo3.Text) & Trim(Combo6.Text) & "'" & Text3.Text & "'"
    Else
        If Combo2.Text = "" Or Combo4.Text = "" Or Text2.Text = "" Or Combo3.Text = "" Or Combo5.Text = "" Or Text3.Text = "" Then
            MsgBox "请输入正确的查询方式!"
        
        Exit Sub
        End If
    '三行都写下来
        txtSQL = txtSQL & field(Combo1.Text) & Trim(Combo4.Text) & "'" & Text1.Text & "'"
        txtSQL = txtSQL & field(Combo7.Text) & " " & field(Combo2.Text) & Trim(Combo5.Text) & "'" & Text2.Text & "'"
        txtSQL = txtSQL & field(Combo8.Text) & " " & field(Combo3.Text) & Trim(Combo6.Text) & "'" & Text3.Text & "'"
   End If
   
       

        
        
  
      Set mrc = ExecuteSQL(txtSQL, msgText)
      
      With myflexgrid
        .Rows = 2
        .CellAlignment = 4
        .TextMatrix(1, 0) = "卡号"
        .TextMatrix(1, 1) = "学号"
        .TextMatrix(1, 2) = "学生姓名"
        .TextMatrix(1, 3) = "系别"
        .TextMatrix(1, 4) = "性别"
        .TextMatrix(1, 5) = "上机日期"
        .TextMatrix(1, 6) = "上机时间"
        .TextMatrix(1, 7) = "下机日期"
        .TextMatrix(1, 8) = "下机时间"
        .TextMatrix(1, 9) = "上机时间"
        .TextMatrix(1, 10) = "花费金额"
        .TextMatrix(1, 11) = "状态"
        .TextMatrix(1, 12) = "机器名"
        
        
        Do While Not mrc.EOF
            .Rows = .Rows + 1
            .CellAlignment = 4
            .TextMatrix(.Rows - 1, 0) = mrc.Fields(1)
            .TextMatrix(.Rows - 1, 1) = mrc.Fields(2)
            .TextMatrix(.Rows - 1, 2) = mrc.Fields(3)
            .TextMatrix(.Rows - 1, 3) = mrc.Fields(4)
            .TextMatrix(.Rows - 1, 4) = mrc.Fields(5)
            .TextMatrix(.Rows - 1, 5) = mrc.Fields(6)
            .TextMatrix(.Rows - 1, 6) = mrc.Fields(7)
            .TextMatrix(.Rows - 1, 7) = mrc.Fields(8)
            .TextMatrix(.Rows - 1, 8) = mrc.Fields(9)
            .TextMatrix(.Rows - 1, 9) = mrc.Fields(10)
            .TextMatrix(.Rows - 1, 10) = mrc.Fields(12)
            .TextMatrix(.Rows - 1, 11) = mrc.Fields(13)
            .TextMatrix(.Rows - 1, 12) = mrc.Fields(14)
            
            mrc.MoveNext
        Loop
    End With
    mrc.Close
    

    
    
    
End Sub

                 这个看起来就简单多了吧,这里用到了select的查找替换的功能,在这里,我还学会了怎么定义一个自己的过程:


    
Public Function field(name As String) As String
    Select Case name
        Case "卡号"
            field = "cardno"
        Case "姓名"
            field = "studentname"
        Case "上机日期"
            field = "ondate"
        Case "上机时间"
            field = "ontime"
        Case "下机日期"
            field = "offdate"
        Case "下机时间"
            field = "offtime"
        Case "消费金额"
            field = "cash"
        Case "机器名"
            field = "computer"
        Case "与"
            field = "and"
        Case "或"
            field = "or"
            
    End Select
    
            
End Function
    这里,其中的name 可以随便写,它不参与别的代码的书写,只在这里出现,专门给field赋值用的。


     这样,用一个function 和click事件下的代码,就可以简单的实现了!


ps:上面所用到的“”和‘’一定要看仔细!

你可能感兴趣的:(数据库,编程代码)