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

一开始让做组合查询的时候我是拒绝的,感觉之前的学生信息管理系统就做过了,感觉没有什么用并且挺简单的,也就没有去做,如果不是有人要求做的话,我一定还没有去做的,后来终于幡然悔悟,将组合查询认真的做完了。

【机房收费系统】组合查询_第1张图片

对于代码,还有一些必要的讲解,上面的控件以列的形式,每一列是一个控件数组 索引 Index都是是从0到2.

1、加载窗体。在窗体加载事件里有这样的代码,基本上就是向各个控件中赋值,并且在定义模块级变量,比较简单不多解释。

<span style="font-size:24px;">Dim Number As Integer							'用来存储控件数组的标记
Dim TeNumber As Integer									上面的数值,在下面的过程中有必要的临时存储
Dim txtSQL As String
Dim Msgtxt As String
Dim mrc As ADODB.Recordset</span>
<span style="font-size:24px;">Private Sub Form_Load()
    Header
    For Index = 0 To 2
        With cmbField1(Index)
            .AddItem "选择查询字段"
            .AddItem "卡号"
            .AddItem "姓名"
            .AddItem "上机日期"
            .AddItem "上机时间"
            .AddItem "下机日期"
            .AddItem "下机时间"
            .AddItem "消费金额"
            .AddItem "余额"
            .AddItem "备注"
        End With
    Next Index
    cmbField1(0).Text = "选择查询字段"
    For Index = 0 To 1
        With cmbMix(Index)
            .AddItem "选择条件连接关系"
            .AddItem "与"
            .AddItem "或"
        End With
    Next Index
    cmbMix(0).Text = "选择条件连接关系"
    
End Sub
</span></span>
该过程中涉及到一个自定义过程,也是赋值语句,JiLu为MSHFlexGrid控件
<span style="font-size:24px;">Private Sub Header()
    With JiLu
        .Clear
        .Rows = 2
        .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) = "备注"
    End With</span></span>

2、选择字段名。根据第一个控件数组的选择,将相应的索引值赋给Number变量,

<span style="font-size:24px;">Private Sub cmbField1_Click(Index As Integer)   '使 当点击控件数组的时候,进行适当的选择,
    Select Case Index              '并将其Index属性值赋给变量,调用自定义过程
    Case 0
        Number = Index
        AddOperator
    Case 1
        Number = Index
        AddOperator
    Case 2
        Number = Index
        AddOperator
    End Select
End Sub

</span></span>

下面是用到的AddOperator自定义过程,该过程可以通过前面 ‘字段名’ 控件选择的内容,向 ‘条件’ 控件中添加适当的内容,并且在 ‘内容’ 处选择对应的控件(比如选择 日期,则有对应的控件进行变换),该过程适用于控件数组中的每一个控件。

<span style="font-size:24px;">Private Sub AddOperator()           '本自定义过程主要功能是,根据前面不同字段的选择,使后面的控件进行相应的变化,并添加适当的内容
    If Trim(cmbField1(Number).Text) = "姓名" Or Trim(cmbField1(Number).Text) = "备注" Then  '如果控件内容是 姓名 或者 备注
        txtContent(Number).Visible = True                                '根据Number的赋值改变对应控件的visible值
        Date1(Number).Visible = False
        With cmbOperator1(Number)                                        '向相应的控件中赋值对应的内容
            .Clear
            .AddItem "="
            .AddItem "<>"
        End With
    ElseIf Trim(cmbField1(Number).Text) = "上机日期" Or Trim(cmbField1(Number).Text) = "下机日期" Then  '如果是控件内容是 上机日期 或者 下机日期
        txtContent(Number).Visible = False
        Date1(Number).Visible = True
        GoTo a                                                                      '跳转至a:行,主要是为了规避else语句
    Else
a:
        With cmbOperator1(Number)                                                   '向对应的控件添加内容
            .Clear
            .AddItem "="
            .AddItem ">"
            .AddItem "<"
            .AddItem "<>"
        End With
    End If
End Sub
</span></span>

3、选择组合关系。此处代码比较简单,没有太难的逻辑,基本上都是优化,但是,有在适当的位置给Number变量赋值。(如果不选择组合关系的话,就不运行下面代码,直接到4、)

<span style="font-size:24px;">Private Sub cmbMix_Click(Index As Integer)
    Select Case Index
    Case 0
        If cmbMix(0).Text = "选择条件连接关系" Then
            cmbMix(1).Enabled = False
            cmbMix(1).Text = "选择条件连接关系"
            cmbField1(1).Enabled = False
            cmbField1(1).Text = "选择查询字段"
            Number = 0
            cmbOperator1(1).Enabled = False
            txtContent(1).Enabled = False
            txtContent(1).Text = ""
            Date1(1).Enabled = False
        Else
            Number = 1
            cmbMix(1).Enabled = True
            cmbField1(1).Enabled = True
            cmbOperator1(1).Enabled = True
            txtContent(1).Enabled = True
            Date1(1).Enabled = True
        End If
        AddMix
    Case 1
        If cmbMix(1).Text = "选择条件连接关系" Then
            cmbField1(2).Enabled = False
            cmbField1(2).Text = "选择查询字段"
            Number = 1
            cmbOperator1(2).Enabled = False
            txtContent(2).Enabled = False
            txtContent(2).Text = ""
            Date1(2).Enabled = False
        Else
            Number = 2
            cmbField1(2).Enabled = True
            cmbOperator1(2).Enabled = True
            txtContent(2).Enabled = True
            Date1(2).Enabled = True
        End If
        AddMix
    End Select
End Sub
</span>
然后此处涉及到一个简单的自定义过程

<span style="font-size:24px;">Private Function AddMix() As String
    Select Case cmbMix(0).Text
    Case "与"
        AddMix = "and"
    Case "或"
        AddMix = "or"
    End Select
    Select Case cmbMix(1).Text
    Case "与"
        AddMix = "and"
    Case "与"
        AddMix = "or"
    End Select
End Function</span>

4、点击查询。将内容填写完整之后,点击查询,如果信息不完整会提示。

<span style="font-size:24px;">Private Sub Query_Click()
    txtSQL = "select * from MachineRecord where "                   '将一段字符串赋给变量,为下面的组合奠定基础
    TeNumber = Number                                               '将
        For Index = 0 To Number
            If cmbField1(Index).Text = "选择查询字段" Or cmbOperator1(Index).Text = "" Then     '先判断第一条中的前两个空
                MsgBox "请将要查询的条件补充完整"               '如果为空,提示
                Exit Sub
            Else                                              '不为空的话,再分情况判断第三个空
                If cmbField1(Index).Text = "上机日期" Or cmbField1(Index).Text = "下机日期" Then
                    Exit For
                Else
                    If txtContent(Index).Text = "" Then
                        MsgBox "请将要查询的条件补充完整"
                        Exit Sub
                    End If
                End If
            End If
        Next Index
        
        If Number = 2 Then
            If cmbField1(2).Text = "上机日期" Or cmbField1(2).Text = "下机日期" Then
                txtSQL = txtSQL & AddField & cmbOperator1(2) & "'" & Date1(2).Value & "'" & " " & AddMix & " "
            Else
                txtSQL = txtSQL & AddField & cmbOperator1(2) & txtContent(2).Text & " " & AddMix & " "
            End If
            Number = 1
            GoTo In2
        End If
        
        If Number = 1 Then
In2:
            If cmbField1(1).Text = "上机日期" Or cmbField1(1).Text = "下机日期" Then
                txtSQL = txtSQL & AddField & cmbOperator1(1) & "'" & Date1(1).Value & "'" & " " & AddMix & " "
            Else
                txtSQL = txtSQL & AddField & cmbOperator1(1) & txtContent(1).Text & " " & AddMix & " "
            End If
            Number = 0
            GoTo In1
        End If
        If Number = 0 Then
In1:
            If cmbField1(0).Text = "上机日期" Or cmbField1(0).Text = "下机日期" Then
                txtSQL = txtSQL & AddField & cmbOperator1(0) & "'" & Date1(0).Value & "'" & "and status='正常下机'"
            Else
                txtSQL = txtSQL & AddField & cmbOperator1(0) & txtContent(0).Text & "and status='正常下机'"
            End If
        End If
        Number = TeNumber

a:
    Set mrc = ExecuteSQL(txtSQL, Msgtxt)
    If mrc.EOF = False Then
        Header
        With JiLu
            Do While Not mrc.EOF
                .TextMatrix(.Rows - 1, 0) = mrc.Fields(0)
                .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) = Format(mrc.Fields(9), "0.0")
                .TextMatrix(.Rows - 1, 7) = Format(mrc.Fields(10), "0.0")
                .TextMatrix(.Rows - 1, 8) = mrc.Fields(8)
                .Rows = .Rows + 1
                mrc.MoveNext
            Loop
            .Rows = .Rows - 1
        End With
        mrc.Close
    Else
        Header
        mrc.Close
    End If
End Sub
</span>

5、显示数据。直接将查询到的数据显示到下面的表格,如果没有数据的时候下面的表格是清空的,但是后来我发现,这样其实挺不好的,如果没有响应的给用户一些反馈的话,用户也可能会误会是不是系统有问题。

小结:从一开始的不重视,就已经有问题了,制作组合查询的过程中,就发现远比想象中难,有点不自量力。不要看轻任何一件事,不要小人任何一个人。




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