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

前言

  还没到组合查询的时候就听到了说组合查询特别难,一开始的时候自己也就想的特别难,一直拖着没去做,真到不得不去做的时候,才发现理清楚思路一切问题就迎刃而解了,所以不应该去惧怕困难,遇见困难的时候正是自己在提高的时候,只有多遇见困难,才会有更多的成长。

思路

  组合查询来源于简单查询,依旧是老套路,先判断控件框是否为空,然后查询赋值,有区别的是分了三层,第一层是第一行要查询的内容,第二三层要通过组合关系判断是否到第二层和第三层,第二层判断一行和二行。第三层判断了所有查询内容。还有些小细节是需要考虑的,比如姓名,性别,只能选择<>,=。如果不去限制也是没有问题的。
这里写图片描述

流程图

这里写图片描述

重要代码

开始不明白字段名里是汉字,怎么把数据库表示出来,后来看了看别人的博客才明白了。定义了一个函数代码如下。

`
Public Function filed(a As String) As String  
    Select Case a  
        Case "卡号"
            filed = "cardno"
        Case "学号"
            filed = "studentno"
        Case "姓名"
            filed = "studentname"
        Case "性别"
            filed = "sex"
        Case "系别"
            filed = "department"
        Case "年级"
            filed = "grade"
        Case "班级"
            filed = "class"
        Case "与"  
            filed = "and"  
        Case "或"  
            filed= "or"  
    End Select  
End Function  `
Private Sub cmdIquiry_Click()
    Dim txtsql As String
    Dim msgtext As String
    Dim mrc As ADODB.Recordset
    txtsql = "select * from Student_Info where"

    '判断第一层查询是否为空
    If Combo1(0).Text = "" Or Combo1(1).Text = "" Or txtIquiry1.Text = "" Then
        MsgBox "条件没有填写完!", vbExclamation, "警告"
        Exit Sub
    Else
    txtsql = txtsql & " " & filed(Combo1(0).Text) & " " & Combo1(1).Text & "'" & Trim(txtIquiry1.Text) & "'"
    Set mrc = ExecuteSQL(txtsql, msgtext)
    '组合关系第一个不为空进行二层查询
    If Combo4(0).Text <> "" Then
        If Combo1(0).Text = "" Or Combo1(1).Text = "" Or Trim(txtIquiry1.Text) = "" _
        Or Combo2(0).Text = "" Or Combo2(1).Text = "" Or Trim(txtInquiry2.Text) = "" Then
           MsgBox "第二行条件没有填写完!", vbExclamation, "警告"
        Exit Sub
    Else
                txtsql = txtsql & filed(Trim(Combo4(0).Text)) & " " & filed(Trim(Combo2(0).Text)) & " " & (Trim(Combo2(1).Text)) & "'" & (txtInquiry2.Text) & "'"

                Set mrc = ExecuteSQL(txtsql, msgtext)
    End If
    '第二个组合关系不为空进行第三层查询
    If Combo4(1).Text <> "" Then
        If filed(Combo1(0).Text) = "" Or Combo1(1).Text = "" Or Trim(txtIquiry1.Text) = "" Or _
        Combo2(0).Text = "" Or Combo2(1).Text = "" Or Trim(txtInquiry2.Text) = "" Or _
        filed(Combo3(0).Text) = "" Or Combo3(1).Text = "" Or Trim(txtInquiry3.Text) = "" Then
            MsgBox "第三行条件没有填写完!", vbExclamation, "警告"
            Exit Sub
        Else '三层关系4中
            txtsql = txtsql & filed(Trim(Combo4(1).Text)) & " " & filed(Trim(Combo3(0).Text)) & " " & (Trim(Combo3(1).Text)) & " '" & txtInquiry3.Text & "'"
            Set mrc = ExecuteSQL(txtsql, msgtext)
            If mrc.EOF Then
                MsgBox "没有数据请重新输入", vbExclamation, "警告"
                Exit Sub
            End If
      End If
   End If
 End If
End If

心得:很多地方一直想想不透,可以休息会在想,或者问问小伙伴们,很多感觉困难的地方只是自己理解错误了,当自己思考理解不了的时候一定要去问问别人。遇见困难,就是在成长的时候。

组合查询优化后(以操作员工作记录为例):
学生基本信息里没有以日期格式和时间格式为查询的,那么学生上机统计信息,和操作员工作记录都有这种格式的,那么我们要怎么做才能使查询的内容日期控件和文本控件一起交互用呢?

Private Sub comboField1_Click()
这是我以前的方法:
 '      If comboField3.Text = "注册日期" Or comboField3.Text = "注销日期" Then
'        MsgBox "日期查询格式为 “yyyy-mm-dd”"
'    End If
'    If comboField3.Text = "注册时间" Or comboField3.Text = "注销时间" Then
'        MsgBox "时间查询格式为  “hh:mm:ss”"
'    End If
更改后的:
      If comboField1.Text = "注册日期" Or comboField1.Text = "注销日期" Then
    '与数据库中的日期格式相符合
        DTPicker1.Format = dtpCustom
        DTPicker1.CustomFormat = Format("yyyy-mm-dd")
        DTPicker1.Visible = True
        txtContent1.Visible = False
    Else
        If comboField1.Text = "注册时间" Or comboField1.Text = "注销时间" Then
            DTPicker1.Format = dtpTime
            DTPicker1.Value = False
            txtContent1.Visible = True
        Else
            DTPicker1.Visible = False
            txtContent1.Visible = True
        End If
    End If
End Sub
End Sub

猜你喜欢

转载自blog.csdn.net/a15076159739/article/details/79250184