机房收费系统—组合查询

       刚开始做组合查询这部分时,特别头疼,不知道怎样去做。后来,慢慢了解了组合查询需要实现什么样的功能,就有了一个大的框架。我就按着自己的框架一步步的去做,一点点的完善它。现在我的组合查询已经竣工了,来讲一下我做组合查询具体是怎样实现的吧!

一、总体构思

 

 

二、具体实施

        下面我以查询操作员工记录为例,说一下我的组合查询。先来把我的界面展示一下,再把各个控件的小名说一下!

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

字段名下的三个组合框为:combo1(0)、combo1(1)、combo1(2)

操作符下的三个组合框为:combo2(0)、combo2(1)、combo2(2)

要查询的内容下三个文本框为:text1(0)、text1(1)、text1(2)

组合关系下的两个组合框为:combo3(0)、combo3(1)

查询:cmdinquire        退出:cmdexit


(1)form load 加载项


Private Sub Form_Load()
  '加载第一层中的字段名
    combo1(0).AddItem "教师"
    combo1(0).AddItem "注册日期"
    combo1(0).AddItem "注册时间"
    combo1(0).AddItem "注销日期"
    combo1(0).AddItem "注销时间"
    combo1(0).AddItem "机器名"


    '加载第一层中的操作符
    Combo2(0).AddItem "="
    Combo2(0).AddItem "<"
    Combo2(0).AddItem ">"
    Combo2(0).AddItem "<>"

    '加载第一层的组合关系
    
    Combo3(0).AddItem ""
    Combo3(0).AddItem "与"
    Combo3(0).AddItem "或"

    '加载第二层中的字段名
    combo1(1).AddItem "教师"
    combo1(1).AddItem "注册日期"
    combo1(1).AddItem "注册时间"
    combo1(1).AddItem "注销日期"
    combo1(1).AddItem "注销时间"
    combo1(1).AddItem "机器名"

    '加载第二层中的操作符
    Combo2(1).AddItem "="
    Combo2(1).AddItem "<"
    Combo2(1).AddItem ">"
    Combo2(1).AddItem "<>"

    '加载第二层的组合关系
    
    Combo3(1).AddItem ""
    Combo3(1).AddItem "与"
    Combo3(1).AddItem "或"

    '加载第三层中的字段名
    combo1(2).AddItem "教师"
    combo1(2).AddItem "注册日期"
    combo1(2).AddItem "注册时间"
    combo1(2).AddItem "注销日期"
    combo1(2).AddItem "注销时间"
    combo1(2).AddItem "机器名"



    '加载第一层中的操作符
    Combo2(2).AddItem "="
    Combo2(2).AddItem "<"
    Combo2(2).AddItem ">"
    Combo2(2).AddItem "<>"
End Sub

(2)组合关系设计

机房收费系统—组合查询_第2张图片
 


'判断两个组合关系,只有选择了组合关系,相应的选项才可用

Private Sub combo3_Click(Index As Integer)

    '第一个组合关系不为空时
    If Trim(Combo3(0).Text) = "与" Or Trim(Combo3(0).Text) = "或" Then  '当第一个选择关系不为空时,第二行选项内容变为可用
        combo1(1).Enabled = True
        Combo2(1).Enabled = True
        Text1(1).Enabled = True
    End If


    '第一个组合关系不为空,同时第二个组合关系不为空
    If Not Trim(Combo3(0).Text) = "" Then
       If Trim(Combo3(1).Text) = "与" Or Trim(Combo3(1).Text) = "或" Then  '当两个组合关系都不为空时,第三行选项内容才可用
        combo1(2).Enabled = True
        Combo2(2).Enabled = True
        Text1(2).Enabled = True
       End If
    End If
    
    
    
    '第一个组合关系为空时,第二层选项均不可用,并且清空
   
  If Trim(Combo3(0).Text) = "" Then
        combo1(1).Text = ""
        Combo2(1).Text = ""
        Text1(1).Text = ""
        combo1(1).Enabled = False
        Combo2(1).Enabled = False
        Text1(1).Enabled = False
    End If
    
    
    '第二个组合关系为空时,第三层选项均不可用,并且清空
 
 If Trim(Combo3(1).Text) = "" Then
        combo1(2).Text = ""
        Combo2(2).Text = ""
        Text1(2).Text = ""
        combo1(2).Enabled = False
        Combo2(2).Enabled = False
        Text1(2).Enabled = False
    End If
End Sub

(3)三层逻辑计算设计

机房收费系统—组合查询_第3张图片



Private Sub cmdinquire_Click()

    '只有第一层时
    strTxtSQL = "select * from worklog_info where "       '从表中取出所需内容

    If Trim$(Combo3(0).Text) = "" Then                    '当combo3(0)为空时
        '如果所选内容为空,则提示“请把您的输入项填写完整”
        '两个组合关系都不选的情况下
        If Trim$(combo1(0).Text) = "" Or Trim$(Combo2(0).Text) = "" Or Trim$(Text1(0).Text) = "" Then
            MsgBox "请把您的输入项填写完整", vbOKOnly + vbExclamation, "警告!"
            Exit Sub
        Else
            '执行sql语句
            strTxtSQL = strTxtSQL & filename(combo1(0).Text) & Combo2(0).Text & "'" & Text1(0).Text & "'"
            Set mrc = executeSQL(strTxtSQL, strMsgtext)

            If mrc.EOF Then                     '数据库中无记录时
                MsgBox "无此记录", vbOKOnly + vbExclamation, "警告!"
                combo1(0).SetFocus
                Text1(0).Text = ""
                mygrid1.Clear
                Exit Sub
            Else                                '数据库中有记录时
                Call tianjia                    '调用添加函数
            End If
        End If
        Set mrc = Nothing                       '释放记录集
    End If



    '选择了第一个组合关系,涉及前两层的计算

     strTxtSQL = "select * from worklog_info where "       '从表中取出所需内容

    If Not Trim$(Combo3(0).Text) = "" Then
        '确保输入选项完整
        If Trim$(combo1(0).Text) = "" Or Trim$(Combo2(0).Text) = "" Or Trim$(Text1(0).Text) = "" Or _
           Trim$(combo1(1).Text) = "" Or Trim$(Combo2(1).Text) = "" Or Trim$(Text1(1).Text) = "" Then
            MsgBox "请把您的输入项填写完整", vbOKOnly + vbExclamation, "警告!"
            Exit Sub
        Else

            If Combo3(0).Text = "与" Then            '当combo3(0)为"与"时
                strTxtSQL = strTxtSQL & filename(combo1(0).Text) & " " & Trim$(Combo2(0).Text) & "'" & _
                    Trim$(Text1(0).Text) & "'" & " " & "And" & " " & filename(combo1(1).Text) & " " & _
                    Trim$(Combo2(1).Text) & "'" & Text1(1).Text & "'"
                Set mrc = executeSQL(strTxtSQL, strMsgtext)

                If mrc.EOF Then                      '数据库中无记录时
                    MsgBox "无此记录", vbOKOnly + vbExclamation, "警告!"
                    combo1(1).SetFocus
                    Text1(1).Text = ""
                    mygrid1.Clear
                    Exit Sub
                Else
                    Call tianjia
                End If
            Else
                '当combo3(0)为"或"时
                strTxtSQL = strTxtSQL & filename(combo1(0).Text) & " " & Trim$(Combo2(0).Text) & "'" & _
                  Trim$(Text1(0).Text) & "'" & " " & "or" & " " & filename(combo1(1).Text) & " " & _
                  Trim$(Combo2(1).Text) & "'" & Text1(1).Text & "'"
                Set mrc = executeSQL(strTxtSQL, strMsgtext)

                If mrc.EOF Then                     '数据库中无记录时
                    MsgBox "无此记录", vbOKOnly + vbExclamation, "警告!"
                    combo1(1).SetFocus
                    Text1(1).Text = ""
                    mygrid1.Clear
                    Exit Sub
                Else
                    Call tianjia                    '调用添加函数,把数据载入到flexgrid控件中
                End If
            End If
        End If
         Set mrc = Nothing                          '释放记录集
    End If




   '两个组合关系全选时,涉及三层关系

    strTxtSQL = "select * from worklog_info where "       '从表中取出所需内容

   If Not Trim$(Combo3(1).Text) = "" Then
        '确保输入选项完整
        If Trim$(combo1(0).Text) = "" Or Trim$(Combo2(0).Text) = "" Or Trim$(Text1(0).Text) = "" Or _
            Trim$(combo1(1).Text) = "" Or Trim$(Combo2(1).Text) = "" Or Trim$(Text1(1).Text) = "" _
            Or Trim(combo1(2).Text) = "" Or Trim(Combo2(2).Text) = "" Or Trim(Text1(2).Text) = "" Then
            MsgBox "请把您的输入项填写完整", vbOKOnly + vbExclamation, "警告!"
            Exit Sub
        Else

           '两个组合关系均为“与”时
           If Combo3(0).Text = "与" And Combo3(1).Text = "与" Then
                strTxtSQL = strTxtSQL & filename(combo1(0).Text) & " " & Trim$(Combo2(0).Text) & _
                          "'" & Trim$(Text1(0).Text) & "'" & " " & "And" & " " & filename(combo1(1).Text) _
                          & " " & Trim$(Combo2(1).Text) & "'" & Text1(1).Text & "'" & " " & "And" _
                          & " " & filename(combo1(2).Text) & " " & Trim(Combo2(2).Text) & "'" & Trim(Text1(2).Text) & "'"
                Set mrc = executeSQL(strTxtSQL, strMsgtext)

                If mrc.EOF Then                      '数据库中无记录时
                    MsgBox "无此记录", vbOKOnly + vbExclamation, "警告!"
                    combo1(2).SetFocus
                    Text1(2).Text = ""
                    mygrid1.Clear
                    Exit Sub
                Else
                    Call tianjia
                End If
            End If


           '第一个组合关系为“与”,第二个组合关系为“或”

           If Combo3(0).Text = "与" And Combo3(1).Text = "或" Then
                strTxtSQL = strTxtSQL & filename(combo1(0).Text) & " " & Trim$(Combo2(0).Text) & "'" & _
                    Trim$(Text1(0).Text) & "'" & " " & "And" & " " & filename(combo1(1).Text) & _
                    " " & Trim$(Combo2(1).Text) & "'" & Text1(1).Text & "'" & " " & "or" & " " & _
                    filename(combo1(2).Text) & " " & Trim(Combo2(2).Text) & "'" & Trim(Text1(2).Text) & "'"
                Set mrc = executeSQL(strTxtSQL, strMsgtext)

                If mrc.EOF Then                      '数据库中无记录时
                    MsgBox "无此记录", vbOKOnly + vbExclamation, "警告!"
                    combo1(2).SetFocus
                    Text1(2).Text = ""
                    mygrid1.Clear
                    Exit Sub
                Else
                    Call tianjia
                End If
            End If


            '第一个组合关系为“或”,第二个组合关系为“与”


            If Combo3(0).Text = "或" And Combo3(1).Text = "与" Then
                strTxtSQL = strTxtSQL & filename(combo1(0).Text) & " " & Trim$(Combo2(0).Text) & "'" _
                        & Trim$(Text1(0).Text) & "'" & " " & "or" & " " & filename(combo1(1).Text) _
                        & " " & Trim$(Combo2(1).Text) & "'" & Text1(1).Text & "'" & " " & "and" & " " _
                        & filename(combo1(2).Text) & " " & Trim(Combo2(2).Text) & "'" & Trim(Text1(2).Text) & "'"
                Set mrc = executeSQL(strTxtSQL, strMsgtext)

                If mrc.EOF Then                      '数据库中无记录时
                    MsgBox "无此记录", vbOKOnly + vbExclamation, "警告!"
                    combo1(2).SetFocus
                    Text1(2).Text = ""
                    mygrid1.Clear
                    Exit Sub
                Else
                    Call tianjia
                End If
            End If


            '第一个组合关系为“或”,第二个组合关系为“或”


            If Combo3(0).Text = "或" And Combo3(1).Text = "与" Then
                strTxtSQL = strTxtSQL & filename(combo1(0).Text) & " " & Trim$(Combo2(0).Text) & "'" _
                        & Trim$(Text1(0).Text) & "'" & " " & "or" & " " & filename(combo1(1).Text) _
                        & " " & Trim$(Combo2(1).Text) & "'" & Text1(1).Text & "'" & " " & "or" & " " _
                        & filename(combo1(2).Text) & " " & Trim(Combo2(2).Text) & "'" & Trim(Text1(2).Text) & "'"
                Set mrc = executeSQL(strTxtSQL, strMsgtext)

                If mrc.EOF Then                      '数据库中无记录时
                    MsgBox "无此记录", vbOKOnly + vbExclamation, "警告!"
                    combo1(2).SetFocus
                    Text1(2).Text = ""
                    mygrid1.Clear
                    Exit Sub
                Else
                    Call tianjia
                End If
            End If

          Set mrc = Nothing                          '释放记录集
      End If
    End If


End Sub

(4)自己完善的

    ①限制文本框长度

Private Sub Text1_Click(Index As Integer)     '设置文本框的输入长度
  Text1(0).MaxLength = 5
  Text1(1).MaxLength = 5
  Text1(2).MaxLength = 5
End Sub

    ②定义一个过程(添加)

Public Function tianjia()               '定义tianjia过程,简化代码
   With mygrid1
                .Rows = 2
                .CellAlignment = 4
                .TextMatrix(1, 0) = "教师"
                .TextMatrix(1, 1) = "注册日期"
                .TextMatrix(1, 2) = "注册时间"
                .TextMatrix(1, 3) = "注销日期"
                .TextMatrix(1, 4) = "注销时间"
                .TextMatrix(1, 5) = "机器名"

                Do While Not mrc.EOF
                    .Rows = .Rows + 1
                    .CellAlignment = 4
                    .TextMatrix(.Rows - 1, 0) = Trim$(mrc.Fields("head"))
                    .TextMatrix(.Rows - 1, 1) = Trim$(mrc.Fields("registration_date"))
                    .TextMatrix(.Rows - 1, 2) = Trim$(mrc.Fields("registration_time"))
                    .TextMatrix(.Rows - 1, 3) = Trim$(mrc.Fields("cancel_date"))
                    .TextMatrix(.Rows - 1, 4) = Trim$(mrc.Fields("cancel_time"))
                    .TextMatrix(.Rows - 1, 5) = Trim$(mrc.Fields("computer"))

                    mrc.MoveNext

                Loop
            End With
            mrc.Close
End Function

    ③定义了一个函数(filename),组合框中的数可与数据库中的字段相对应(借鉴永霞的)

Public Function filename(strfilename As String) As String

 Select Case strfilename
    Case "教师"
       filename = "head"
    Case "注册日期"
       filename = "registration_date"
    Case "注册时间"
       filename = "registration_time"
    Case "注销日期"
       filename = "cancel_date"
    Case "注销时间"
       filename = "cancel_time"
    Case "机器名"
       filename = "computer"
           
  End Select

End Function
          通过做组合查询这部分,发现自己可以想到如何去实现某个功能,但 在细节上出现问题。看来还是需要不断地去积累……


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