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

【前言】

  1. 组合查询时机房收费系统的难点之一,其中对数组的理解和整体的逻辑流程有着很大的考验
  2. 刚准备入手做的时候有了大概的思路,但是做的时候出现了问题
  3. 查询的条件太多,很多代码的冗余

【思考】

  1. .当第一个条件是可以单独查询的,第二个条件查询是在第一个条件的基础上同时查询,第三个条件符合后是三个一起同时查询。
  2. 为列表框添加内容,不需要一个框一个框的添加,利用数组的独特性质,让其后面的自动添加。
  3. 在对数据库进行查询的时候和前面的内容查询方式不一样,利用函数去组合查询。
  4. 利用函数的去减少查询条件的判断代码,这样做有一点不好之处是出现错误的时候无法很精准的判断是那个位置的错误,常规判断可以很好的解决这个问题,两种方法各有各的好处。

【代码】

Private Sub Command2_Click()

  Dim mrcStudent As ADODB.Recordset
  Dim StusdentSQL As String
  Dim MsgText As String

   StudentSQL = "select * from student_Info where "

   '判断查询条件是否为空
   Kong = Testtxt(Combo1(0).Text) Or Testtxt(Combo2(0).Text) Or Testtxt(Text1(0).Text)

   If Kong = False Then
     MsgBox "请输入查询条件", 0 + 48, "系统提示"
   Exit Sub
   Else

   '一个条件的查询
   StudentSQL = StudentSQL & "" & FieldName(Combo1(0).Text) & "" & Combo2(0).Text & "'" & Trim(Text1(0).Text) & "'"
   End If

   If Combo3(0).Text <> "" Then
       Kong1 = Kong And Testtxt(Combo1(1).Text) And Testtxt(Combo2(1).Text) And Testtxt(Text1(1).Text)
       If Kong1 = False Then
          MsgBox "请输入查询条件", 0 + 48, "系统提示"
    Exit Sub
    Else

    '添加第二个条件的查询
     StudentSQL = StudentSQL & " " & FieldName1(Combo3(0).Text) & " " & FieldName(Combo1(1).Text) & Combo2(1).Text & "'" & Trim(Text1(1).Text) & "'"
     End If
   End If

   If Combo3(1).Text <> "" Then
      Kong2 = Kong And Kong1 And Testtxt(Combo1(2).Text) And Testtxt(Combo2(2).Text) And Testtxt(Text1(2).Text)
   If Kong2 = False Then
      MsgBox "请输入查询条件", 0 + 48, "系统提示"
   Exit Sub
   Else

   '添加第三个条件的查询
   StudentSQL = StudentSQL & "" & FieldName1(Combo3(1).Text) & " " & FieldName(Combo1(2).Text) & Combo2(2).Text & "'" & Trim(Text1(2).Text) & "'"
   End If
   End If

   Set mrcStudent = ExecuteSQL(StudentSQL, MsgText)

   If mrcStudent.EOF = True Then
      MsgBox "没有查询到结果!“,0+48,“系统提示”
   Else

         MSHFlexGrid1.AllowUserResizing = 3    '调整行和列的大小
   With MSHFlexGrid1
        .Rows = 1
        .CellAlignment = 4     '单元内容居中对齐
        .ColAlignment = 4       '列内容居中对齐
        .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 mrcStudent.EOF = False
            .Rows = .Rows + 1
            .CellAlignment = 4     '单元内容居中对齐
            .ColAlignment = 4       '列内容居中对齐
            .TextMatrix(.Rows - 1, 0) = Trim(mrcStudent.Fields(1))
            .TextMatrix(.Rows - 1, 1) = Trim(mrcStudent.Fields(2))
            .TextMatrix(.Rows - 1, 2) = Trim(mrcStudent.Fields(0))
            .TextMatrix(.Rows - 1, 3) = Trim(mrcStudent.Fields(7))
            .TextMatrix(.Rows - 1, 4) = Trim(mrcStudent.Fields(4))
            .TextMatrix(.Rows - 1, 5) = Trim(mrcStudent.Fields(5))
            .TextMatrix(.Rows - 1, 6) = Trim(mrcStudent.Fields(6))
            .TextMatrix(.Rows - 1, 7) = Trim(mrcStudent.Fields(3))
            .TextMatrix(.Rows - 1, 8) = Trim(mrcStudent.Fields(10))
            .TextMatrix(.Rows - 1, 9) = Trim(mrcStudent.Fields(8))
            .TextMatrix(.Rows - 1, 10) = Trim(mrcStudent.Fields(14))
            .TextMatrix(.Rows - 1, 11) = Trim(mrcStudent.Fields(12))
            .TextMatrix(.Rows - 1, 12) = Trim(mrcStudent.Fields(13))
            mrcStudent.MoveNext
        Loop
    End With
 End If
End Sub

Private Function FieldName(zhang As String) As String
    Select Case zhang
       Case "学号"
          FieldName = "StudentNo"
       Case "姓名"
          FieldName = "StudentName"
       Case "卡号"
          FieldName = "CardNo"
       Case "系别"
          FieldName = "DepartMent"
       Case "年级"
          FieldName = "Grade"
       Case "性别"
          FieldName = "Sex"
       Case "班级"
          FieldName = "Class"
    End Select
End Function

Private Function FieldName1(dada As String) As String
    Select Case dada
       Case "与"
          FieldName1 = "and"
       Case "或"
          FieldName1 = "or"
    End Select
End Function

Public Function Testtxt(strtxt As String) As Boolean
    If strtxt = "" Then
        Testtxt = False
    Else
        Testtxt = True
    End If
End Function

【总结】

  1. 同一功能又很多种代码可以实现,简化代码可以帮我们节省很多时间和控件,需要我们更加深入的去探索。
  2. 遇到问题,解决无果,可以暂时放放回头再看会有不一样的“风采”,如果还解决不了就需要我们站在巨人的肩膀上啦。

猜你喜欢

转载自blog.csdn.net/damishidai15/article/details/80684931