机房收费系统—组合查询

来源:互联网 发布:手机搜图软件 编辑:程序博客网 时间:2024/04/29 06:43

            关于组合查询,真的是一个令人头疼的东西,但是当自己突然间的做出来时,却莫名的有种貌似又不是很难得感觉。昨天弄了整整一下午,今天的下午终于在调试了两个小时做出来了。

       首先是查到了一些关于组合查询的博客,有一个共同的特点就是都要获得用户所选的字段名并转化成数据库表中的字段名,这样实现了人机共同语言的转换。但是代码实在是太复杂了,在小星星的引导下,我知道了可以定义模块的办法。最后我是采用了这样一种方法终于成功了。

        定义在模块中的代码如下:

 '将选择的字段汉字转化成数据库中的字段,——计算机语言
  '因为以后还会有好多个组合查询的窗体,会用到下面的字段,一起都写出来,很方便调用

Public Function FieldName(strFieldName As String) As String
   Select Case strFieldName
       Case "卡号"
          FieldName = "cardno"
       Case "学号"
          FieldName = "studentNo"
       Case "姓名"
          FieldName = "studentName"
       Case "性别"
          FieldName = "sex"
       Case "系别"
          FieldName = "department"
       Case "年级"
          FieldName = "grade"
       Case "班号"
          FieldName = "class"
       Case "上机日期"
          FieldName = "ondate"
       Case "上机时间"
          FieldName = "ontime"
       Case "下机日期"
          FieldName = "offdate"
       Case "下机时间"
          FieldName = "offtime"
       Case "消费金额"
          FieldName = "consume"
       Case "余额"
          FieldName = "cash"
       Case "备注"
          FieldName = "status"
       Case "教师"
          FieldName = "Level"
       Case "注册日期"
          FieldName = "LoginDate"
       Case "注册时间"
          FieldName = "LoginTime"
       Case "注销日期"
          FieldName = "LogoutDate"
       Case "注销时间"
          FieldName = "LogoutTime"
       Case "机器名"
          FieldName = "computer"
   End Select
End Function

'将操作符转化成数据库表中的字段名
Public Function Operate(strOperate As String)
   Select Case strOperate
      Case "="
         Operate = "="
      Case "<"
         Operate = "<"
      Case ">"
         Operate = ">"
      Case "<>"
         Operate = "<>"
   End Select
End Function

'将连接符转化成数据库表中的字段名
Public Function Connect(strConnect As String) As String
    Select Case strConnect
       Case "或"
          Connect = "or"
       Case "与"
          Connect = "and"
       Case ""
          Connect = ""
    End Select
End Function


        学生基本信息维护窗体

Private Sub cmdInquire_Click()
       Dim objrs As ADODB.Recordset
       Dim txtSQL As String
       Dim msgText As String
       Dim strA As String   '定义三个保存SQL语句的字符串
       Dim strB As String
       Dim strC As String
   
   '先写上一个不完整的SQL语句,等用到时再补充
       txtSQL = "select * from student_Info where "
   
   '每个字符串代表分别是三行三种查询条件时的三种情况
       strA = FieldName(cmbField(0).Text) & Operate(cmbOperate(0).Text) & Trim(txtContent(0).Text)
       strB = strA & " " & Connect(cmbConnect(0).Text) & " " & FieldName(cmbField(1).Text) & Operate(cmbOperate(1).Text) & Trim(txtContent(1).Text)
       strC = strB & " " & Connect(cmbConnect(1).Text) & " " & FieldName(cmbField(2).Text) & Operate(cmbOperate(2).Text) & Trim(txtContent(2).Text)

   '判断查询条件,当只有一行条件时,默认选择第一行的查询条件,即第一个组合关系没有选择;
   '当有两条查询条件时,默认是选择前两行,即第一个组合关系不为空
   '当有三行查询条件时,即每个控件都不为空,即第二个组合关系不为空
      If Trim(cmbConnect(0).Text) = "" Then
           If Trim(cmbField(0).Text) = "" Or Trim(cmbOperate(0).Text) = "" Or (txtContent(0).Text) = "" Then
                MsgBox "请在第一行输入查询条件", 48, "提示"
                Exit Sub
           Else
                txtSQL = txtSQL & strA
           End If
     Else
            If Trim(cmbConnect(1).Text) = "" Then
                 If Trim(cmbField(1).Text) = "" Or Trim(cmbOperate(1).Text) = "" Or (txtContent(1).Text) = "" Then
                      MsgBox "请在第二行输入查询条件", 48, "提示"
                      Exit Sub
                 Else
                      txtSQL = txtSQL & strB
                 End If
           Else
                If Trim(cmbField(2).Text) = "" Or Trim(cmbOperate(2).Text) = "" Or (txtContent(2).Text) = "" Then
                       MsgBox "请在第三行输入查询条件", 48, "提示"
                       Exit Sub
                Else
                       txtSQL = txtSQL & strC
                End If
           End If
    End If
   
    Set objrs = ExecuteSQL(txtSQL, msgText)
   
   If objrs.EOF Then
        MsgBox "没有查到记录", 48, "提示"
        Exit Sub
   End If
  
   '将查询到的数据写到myFlexGrid表格中,在写入数据之前要注意清空数据表,防止数据重复
   With myFlexGrid
        .Clear
        .Cols = 7
        .CellAlignment = 4
        .TextMatrix(0, 0) = "卡号"
        .TextMatrix(0, 1) = "学号"
        .TextMatrix(0, 2) = "姓名"
        .TextMatrix(0, 3) = "性别"
        .TextMatrix(0, 4) = "系别"
        .TextMatrix(0, 5) = "年级"
        .TextMatrix(0, 6) = "班级"
        
        .Rows = 1
        Do While Not objrs.EOF
           .Rows = .Rows + 1
           .CellAlignment = 4
           .TextMatrix(.Rows - 1, 0) = objrs!cardno
           .TextMatrix(.Rows - 1, 1) = objrs!studentNo
           .TextMatrix(.Rows - 1, 2) = objrs!studentName
           .TextMatrix(.Rows - 1, 3) = objrs!sex
           .TextMatrix(.Rows - 1, 4) = objrs!department
           .TextMatrix(.Rows - 1, 5) = objrs!grade
           .TextMatrix(.Rows - 1, 6) = objrs!Class
           objrs.MoveNext
        Loop
   End With
   objrs.Close 
End Sub