现在的位置: 首页 > 综合 > 正文

机房收费系统之组合查询

2017年08月05日 ⁄ 综合 ⁄ 共 4308字 ⁄ 字号 评论关闭

机房收费系统中一个重要的部分就是组合查询,刚开始,我以一个复杂的思想来看待这个问题,逻辑就是:当第一行的查询条件不为空,第二行查询条件中有空的时候,如果第一个组合条件为空,则就只按照第一行的查询条件进行查询;如果第一个组合条件不为空,就会提示:因为已经选择了第一个组合条件,所以必须将第二行的查询条件填写完整之后才能进行查询!当第一行和第二行的查询条件都不为空时,若第一个组合条件为空,就提示。之后就依次类推:当第一行和第二行的查询条件不为空并且第一个组合条件不为空时,若第二个组合条件为空,就按照前两行的查询条件进行查询,如果第二个组合条件不为空并且第三行的查询条件有空的话,就提示,如果第二个组合条件和第三行查询条件均不为空,就按照三行条件进行查询。现在说着这个逻辑,我自己都头晕了,更别说写代码的时候了,那哪是一个“乱”字能形容的呀!

当我听了小梅的逻辑之后,我就果断的改了:首先让两个组合条件框都不能用,第二行和第三行的查询条件都不能用,当第一行的查询条件填满了之后,让第一个组合条件能用,当一个组合条件填好之后,让第二行查询条件能用,当第二行查询条件填满之后,让第二个组合条件框能用,当第二个组合条件填好之后,让第三行的查询条件能用,就这样根据哪个可以用,哪个填好了,进行查询。

想想就觉得后一种思维清晰了好多,主要问题时两个逻辑的最终目的是一样的,但是第二个就少走了好多的路……果断应用呀!!!

下面是相关的一些代码:

(这是学生基本信息维护窗体的部分代码)

1、首先在窗体加载时让第一、二个组合条件和第二、三 行的查询条件框不能用:

Private Sub Form_Load()
       '选中一整行
      flexgrid1.SelectionMode = flexSelectionByRow
      flexgrid1.FocusRect = flexFocusNone
      flexgrid1.HighLight = flexHighlightWithFocus
      cmboziduan2.Enabled = False
      cmbocaozuofu2.Enabled = False
      txtinquire2.Enabled = False
'      cmbozuhe1.Enabled = False
      cmboziduan3.Enabled = False
      cmbocaozuofu3.Enabled = False
      txtinquire3.Enabled = False
      cmbozuhe2.Enabled = False
End Sub

2、第一个组合条件判断时:

Private Sub cmbozuhe1_Click()
  If Not (cmbozuhe1.Text = "") Then  '当第一个组合关系不为空的时候
     '第二行的条件可用
     cmboziduan2.Enabled = True
     cmbocaozuofu2.Enabled = True
     txtinquire2.Enabled = True
     cmbozuhe2.Enabled = True
     
  Else
     cmboziduan2.Enabled = False
     cmbocaozuofu2.Enabled = False
     txtinquire2.Enabled = False
     
     
  End If
End Sub

3、第二个组合条件判断时:

Private Sub cmbozuhe2_Click()
   If Not (cmbozuhe2.Text = "") Then  '当第二个组合关系不为空的时候
     '第三行的条件可用
     cmboziduan3.Enabled = True
     cmbocaozuofu3.Enabled = True
     txtinquire3.Enabled = True
  Else
     cmboziduan3.Enabled = False
     cmbocaozuofu3.Enabled = False
     txtinquire3.Enabled = False
  End If
End Sub

4、所有判断条件都进行了之后,查询(查询按钮单机代码):

Private Sub cmdinquire_Click()
         
         '如果第一行输入内容有空,提示信息
         If Trim(cmboziduan1.Text) = "" Or Trim(cmbocaozuofu1.Text) = "" Or Trim(txtinquire1.Text) = "" Then
            MsgBox "请输入完整的查询条件!", "提示"
            Exit Sub
          End If
          
          '当第一行条件不为空的时候
             
             '取数据
             txtSQL = "select * from student_Info where "
             txtSQL = txtSQL & field(cmboziduan1.Text) & Trim(cmbocaozuofu1.Text) & "'" & Trim(txtinquire1.Text) & "'"
'            txtSQL = txtSQL & field(cmboziduan1.Text) & Trim(cmbocaozuofu1.Text) & "'" & Trim(txtinquire1.Text) & "'"
      If Trim(cmbozuhe1.Text) <> "" Then  '当组合条件1不为空时
         '判断第二行的条件是否为空
          If Trim(cmboziduan2.Text) = "" Or Trim(cmbocaozuofu2.Text) = "" Or Trim(txtinquire2.Text) = "" Then

              MsgBox "您选择了第一个组合条件,请填写完整第二行查询条件再进行查询!"
              Exit Sub
         Else
              '查询第一、二行条件
              txtSQL = txtSQL & field(cmbozuhe1.Text) & " " & field(cmboziduan2.Text) & cmbocaozuofu2.Text & "'" & Trim(txtinquire2.Text) & "'"
         End If
          
          
      End If
       
       '当第二个组合关系不为空时
      If Trim(cmbozuhe2.Text) <> "" Then
         If Trim(cmboziduan3.Text) = "" Or Trim(cmbocaozuofu3.Text) = "" Or Trim(txtinquire3.Text) = "" Then
            MsgBox "您选择了第二个组合关系,请填写完整第三行的查询条件再进行查询!"
            Exit Sub
         Else
            '查询第一、二、三行的条件
             txtSQL = txtSQL & field(cmbozuhe2.Text) & " " & field(cmboziduan3.Text) & cmbocaozuofu3.Text & "'" & Trim(txtinquire3.Text) & "'"
         End If
      End If
Set mrc = ExecuteSQL(txtSQL, Msgtext)
      With flexgrid1
         .Rows = 1
         .ColWidth(9) = 2000
         .ColWidth(11) = 2000
         .ColWidth(12) = 4000
         .ColWidth(0) = 2000
         .ColWidth(2) = 2000
         .ColWidth(5) = 2000
         .ColWidth(6) = 2000
       
         .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
            .Rows = .Rows + 1
            .TextMatrix(.Rows - 1, 0) = mrc!studentNo
            .TextMatrix(.Rows - 1, 1) = mrc!studentName
            .TextMatrix(.Rows - 1, 2) = mrc!cardno
            .TextMatrix(.Rows - 1, 3) = mrc!cash
            .TextMatrix(.Rows - 1, 4) = mrc!sex
            .TextMatrix(.Rows - 1, 5) = mrc!grade
            .TextMatrix(.Rows - 1, 6) = mrc!Class
            .TextMatrix(.Rows - 1, 7) = mrc!department
            .TextMatrix(.Rows - 1, 8) = mrc!Status
            .TextMatrix(.Rows - 1, 9) = mrc!Date
            .TextMatrix(.Rows - 1, 10) = mrc!Time
            .TextMatrix(.Rows - 1, 11) = mrc!Type
            .TextMatrix(.Rows - 1, 12) = mrc!explain

            mrc.MoveNext
         Loop
           mrc.Close
       End With
       If flexgrid1.Rows = 1 Then
           MsgBox "没有此记录,请重新输入查询信息!", vbOKOnly + vbExclamation, "警告"
           Exit Sub
       End If
    
      
End Sub

5、自定义函数,使查询条件进行转换,将查询条件的语言变为数据库中的语言:

Public Function field(strfilename As String) As String
       Select Case strfilename
           Case "学号"
              field = "studentNo"
           Case "姓名"
                field = "studentName"
           Case "卡号"
                 field = "cardno"
           Case "系别"
                field = "department"
           Case "年级"
                field = "grade"
           Case "班级"
                field = "class"
           Case "性别"
                field = "sex"
           Case "与"
                field = "and"
           Case "或"
                field = "or"
           Case "状态"
                field = "status"
           Case "备注"
                field = "explain"
           Case "日期"
                field = "date"
           Case "时间"
                field = "time"
           Case "金额"
                field = "cash"
           Case "类型"
               field = "type"
     End Select
End Function

做完组合查询就知道了,其实都是一样的,只不过我们之前做的简单的查询是根据一个条件查询,这时候就是将多个条件组合在一起进行查询,本质是没有变得。

从组合查询这个事上,我学到了:每件事都有多种解决办法,只是有的人找到的是简单的,有的人找到的是复杂的,我们不能就想着凭借着自己的力量,闷头的做这件事,应该站在巨人的肩膀上,要知道:当你还没有找到简单的解决方法的时候就先按照别人的简单的方法做!!!

抱歉!评论已关闭.