天天看点

机房收费系统组合查询

机房收费系统做的差不多了,现在回顾一下能够分为以下几点:

一、学生录入信息、上机、下机、充值、退卡、维护个人信息一系列操作

二、学生上机、下机的收费计算

三、学生信息或者机房工作人员信息查询(组合查询)

四、结账、进行报表

下面说一下第三点儿组合查询的问题。

    机房收费系统中组合查询十多次使用。

          比如:学生上机状态查询

         学生基本信息维护

                学生上机统计信息

         操作员工作记录

简图如下:

机房收费系统组合查询

    下面说一下我在组合查询中的思路:

    1、首先遇到的困难就是‘字段名’ 和 ‘组合关系’对应的文本和我们要查询表中的字段不是一致的(字段名=学号,对应数据表中 studentNo 但是 如何当我们输入学号让它查询是对应的studentNo中去?是一个问题)

    解:定义一个函数DateView()

 public Function DateView(a as string)as string

  select case a

      case "学号"

          DateView = "studentNo"

      case "姓名"

   DateView = "studentname"

   .

   .

   .

   .

  end select

    这样就可以使窗体中的字符串和数据表中对应字段一致了

    2、紧接着是查询语句,对于多条件查询也就是and / or 的问题。

        比如查询student表中学号(studentno)在3—9之间的学生可以这样写:

 dim txtsql as string

 txtsql="select * from student where studentno>'"&"3"&"' and student<'"&"9"&"'"

    3、向窗体中显示数据

        使用do while语句一条一条的添加数据

    现在基本代码是实现了,但是对于许多能够出现的错误要避免。下面是我在查询"学生上机统计查询"的代码:

public Function Field(a as string ) as string

 select case a

     case"卡号"

  field="cardno"

     case"姓名"

  field="studentname"

     case"上机日期"

  field="ondate"

     case"上机时间"

  field="ontime"

     case"下机日期"

  field="offdate"

     case"下机时间"

  field="offtime"

     case"消费金额"

  field="consume"

     case"余额"

  field="cash"

     case"备注"

  field="status"

 end select

End Function

Private Sub CmdInquire_Click()

    Dim Mrc As ADODB.Recordset

    Dim txtSQL As String

    Dim MsgText As String

    '如果第一行输入内容有空,提示信息

    If Trim(ComboField1.Text) = "" Or Trim(ComboOperator1.Text) = "" Or Trim(txtTest1.Text) = "" Then

        MsgBox "请输入完整的查询条件", , "提示"

        Exit Sub

    End If

    txtSQL = "select * from line_Info where  "

    txtSQL = txtSQL & Field(ComboField1.Text) & Trim(ComboOperator1.Text) & "'" & Trim(txtTest1.Text) & "'"

    If Trim(ComboRelation1.Text <> "") Then

        If Trim(ComboField2.Text) = "" Or Trim(ComboOperator2.Text) = "" Or Trim(txtTest2.Text) = "" Then

            MsgBox "您选择了第一个组合关系,请输入第二行条件在查询", vbOKOnly, "提示"

            Exit Sub

        Else

            txtSQL = txtSQL & Field(ComboRelation1.Text) & " " & Field(ComboField2.Text) & ComboOperator2.Text & "'" & Trim(txtTest2.Text) & "'"

        End If

    End If

    If Trim(ComboRelation2.Text) <> "" Then

        If Trim(ComboField3.Text) = "" Or Trim(ComboOperator3.Text) = "" Or Trim(txtTest3.Text) = "" Then

            MsgBox "您选择了第二个组合关系,请输入第三行条件在查询", vbOKOnly, "提示"

            Exit Sub

        Else

            txtSQL = txtSQL & Field(ComboRelation2.Text) & " " & Field(ComboField3.Text) & ComboOperator3.Text & "'" & Trim(txtTest3.Text) & "'"

        End If

    End If

    Set Mrc = ExecuteSQL(txtSQL, MsgText)

    With MyflexGrid

        .Rows = 1

        .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) = "备注"

        Do While Not Mrc.EOF

            .Rows = .Rows + 1

            .TextMatrix(.Rows - 1, 0) = Mrc!cardno

            .TextMatrix(.Rows - 1, 1) = Mrc!studentname

            .TextMatrix(.Rows - 1, 2) = Mrc!ondate

            .TextMatrix(.Rows - 1, 3) = Mrc!ontime

            .TextMatrix(.Rows - 1, 4) = Mrc!offDate

            .TextMatrix(.Rows - 1, 5) = Mrc!offTime

            .TextMatrix(.Rows - 1, 6) = Mrc!consume

            .TextMatrix(.Rows - 1, 7) = Mrc!cash

            .TextMatrix(.Rows - 1, 8) = Mrc!Status

            Mrc.MoveNext

        Loop

        Mrc.Close

    End With

End Sub

组合查询中开始的想法和最终的想法其实差不多,但是总是查不出来,总是出问题,后来利用断点调试,渐渐的明白了空格的重要性。就像上面写的一句代码:txtSQL = txtSQL & Field(ComboRelation1.Text) & " " & Field(ComboField2.Text) & ComboOperator2.Text & "'" & Trim(txtTest2.Text) & "'" 如果这段代码写成这样那必然是读不出来:txtSQL = txtSQL & Field(ComboRelation1.Text) & Field(ComboField2.Text) & ComboOperator2.Text & "'" & Trim(txtTest2.Text) & "'"