机房收费系统---组合查询

     在机房收费系统中,在对大型的数据中进行检索的时候。通过一个字段进行检索的时候,往往得到很多没有价值的结果。这样的查询没有多大意义。所以人们往往通过添加字段的方式。逐步进行筛选。这样得到的数据,又少又有用。

     通过添加字段的形式来进行组合筛选。



 

     默认是单个查询方式。

   当使用的多的话,就可以通过添加字段的形式来进行组合查询。添加字段形式如下:



 

因为,在text中的文本与数据库中的字段名不一致,所以 这样的SQL语句达不到要求,所以需要把 text文本中的字段转换为 数据库中所使用的字段。

 

所以使用了tag属性:

Private Sub cboField1_Click()           '把选择的字段转换成  sql语句中的字段
    If cboField1.Text = "教师" Then cboField1.Tag = "UserId"
    If cboField1.Text = "上机日期" Then cboField1.Tag = "loginDate"
    If cboField1.Text = "上机时间" Then cboField1.Tag = "loginTime"
    If cboField1.Text = "注销日期" Then cboField1.Tag = "logoutDate"
    If cboField1.Text = "注销时间" Then cboField1.Tag = "logoutTime"
    If cboField1.Text = "机器号" Then cboField1.Tag = "computer"
End Sub

Private Sub cboMode1_Click()            '把选择的 字段 组合关系 转换成SQL中的 组合关系关键字
    If cboMode1.Text = "与" Then cboMode1.Tag = "and"
    If cboMode1.Text = "或" Then cboMode1.Tag = "or"

    cboField2.Visible = True            ’添加字段,打开一个新字段
    cboSign2.Visible = True
    txtInquire2.Visible = True
    cboMode2.Visible = True
End Sub

 

在下述的查询中,依次向txtSQL中添加 sql语句,形成的SQL语句形式为:

select * from worklog_Info where (UserId='**' and loginDate='**' and ---) order by serial


 

然后依次添加检索字段如下:

Private Sub cmdInquire_Click()
    Dim txtSQL As String
    Dim MsgText As String
    Dim mrc As ADODB.Recordset
    
    '首先写一个不完全的SQL语句,一会儿补充
    txtSQL = "select * from student_Info where (" & cboField1.Tag & cboSign1.Text & "'" & txtInquire1.Text & "'"
    
    If cboMode1.Text <> "" Then      '检验第二个组合字段的正确性
        If cboField2.Text = "" Then
            MsgBox "请选择字段名!", vbOKOnly + vbExclamation, "警告"
            cboField2.SetFocus
            Exit Sub
        End If
        If cboSign2.Text = "" Then
            MsgBox "请输入操作符!", vbOKOnly + vbExclamation, "警告"
            cboSign2.SetFocus
            Exit Sub
        End If
        If txtInquire2.Text = "" Then
            MsgBox "请输入要查询的内容", vbOKOnly + vbExclamation, "警告"
            txtInquire2.SetFocus
            Exit Sub
        End If
        
        '第二个组合字段正确,开始添加信息
        txtSQL = txtSQL & " " & cboMode1.Tag & " " & cboField2.Tag & cboSign2.Text & "'" & txtInquire2.Text & "'"
    End If
    
    If cboMode2.Text <> "" Then     '检验第三组 组合字段的正确性
        If cboField3.Text = "" Then
            MsgBox "请选择字段名!", vbOKOnly + vbExclamation, "警告"
            cboField3.SetFocus
            Exit Sub
        End If
        If cboSign3.Text = "" Then
            MsgBox "请输入操作符!", vbOKOnly + vbExclamation, "警告"
            cboSign3.SetFocus
            Exit Sub
        End If
        If txtInquire3.Text = "" Then
            MsgBox "请输入要查询的内容", vbOKOnly + vbExclamation, "警告"
            txtInquire3.SetFocus
            Exit Sub
        End If
        
        '第三个组合字段正确,开始添加信息
        txtSQL = txtSQL & " " & cboMode2.Tag & " " & cboField3.Tag & cboSign3.Text & "'" & txtInquire3.Text & "'"
    End If
    
    txtSQL = txtSQL & ") "   '把SQL语句补充完整
    'Debug.Print txtSQL   用于调试程序使用
    Set mrc = ExecuteSQL(txtSQL, MsgText)
    
    If mrc.RecordCount = 0 Then         '如果要查询的结果为空,则提醒用户
        myFlexGrid.Clear
        MsgBox "结果集为空!", vbOKOnly + vbExclamation, "警告"
        Exit Sub
    End If
    
    With myFlexGrid         '把标题写上
        .Row = 0
        .TextMatrix(.Row, 0) = "学号"
        .TextMatrix(.Row, 1) = "姓名"
        .TextMatrix(.Row, 2) = "卡号"
        .TextMatrix(.Row, 3) = "余额"
        .TextMatrix(.Row, 4) = "学院"
        .TextMatrix(.Row, 5) = "专业"
        .TextMatrix(.Row, 6) = "年级"
        .TextMatrix(.Row, 7) = "班级"
        .TextMatrix(.Row, 8) = "性别"
        .TextMatrix(.Row, 9) = "状态"
        .TextMatrix(.Row, 10) = "备注"
    End With
    
    myFlexGrid.Rows = mrc.RecordCount + 1           '设置大小
    
    With myFlexGrid         '对查询到的结果进行遍历,显示出来
        .Row = 0
        While mrc.EOF = False
            .Row = .Row + 1
            .TextMatrix(.Row, 0) = mrc.Fields(1)
            .TextMatrix(.Row, 1) = mrc.Fields(2)
            .TextMatrix(.Row, 2) = mrc.Fields(0)
            .TextMatrix(.Row, 3) = mrc.Fields(8)
            .TextMatrix(.Row, 4) = mrc.Fields(4)
            .TextMatrix(.Row, 5) = mrc.Fields(5)
            .TextMatrix(.Row, 6) = mrc.Fields(6)
            .TextMatrix(.Row, 7) = mrc.Fields(7)
            .TextMatrix(.Row, 8) = mrc.Fields(3)
            .TextMatrix(.Row, 9) = mrc.Fields(11)
            .TextMatrix(.Row, 10) =mrc.Fields(9)
            mrc.MoveNext
        Wend
    End With
End Sub


在给控件,添加tag属性的时候,由于我使用的不是控件数组。所以,写的代码有点繁琐。

如果是控件数据,可以通过for循环来减少代码量。

参考博客:http://blog.csdn.net/augus3344/article/details/11018455



posted on 2013-09-11 20:56  you Richer  阅读(212)  评论(0编辑  收藏  举报