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

    操作员窗体的代码实现可分为三部分:一部分是注册,这在学生信息系统中有所接触,所以实现起来相对较容易;第二部分是充值,退卡,也挺容易的,主要是在第三部分组合查询,下面我就为大家说说:组合查询那些事儿。

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

    该组合查询分四种情况:

    一、符合第一层条件,查询。

    二、选择第一个条件“与”“或”后,符合前两层条件,查询。

    三、选择第二个条件“与”“或”后,符合三层条件,查询。

    四、判断一下没有可查询的信息并提示。

    其中的细节问题就是要定义一个函数,将文字转化成数据库中的字段名称,以便识别,代码如下:

   

Public Function fieldname(strfieldname As String) As String
    '是汉字转化成数据库中的字段
    Select Case strfieldname
        Case "卡号"
            fieldname = "cardno"
        Case "姓名"
            fieldname = "studentname"
        Case "上机时间"
            fieldname = "ontime"
        Case "上机日期"
            fieldname = "ondate"
        Case "下机时间"
            fieldname = "offtime"
        Case "下机日期"
            fieldname = "offdate"
        Case "消费金额"
            fieldname = "consume"
        Case "余额"
            fieldname = "cash"
        Case "备注"
            fieldname = "status"
        Case "或"
            fieldname = "or"
        Case "与"
            fieldname = "and"
    End Select
End Function


查询的代码如下:

Private Sub cmdInquire_Click()
    Dim txtSQL As String
    Dim MsgText As String
    Dim lmrc As ADODB.Recordset
    

    '判断第一行是否为空
    If Trim(Comboa1.Text) = "" Or Trim(Combob1.Text) = "" Or Trim(txtc1.Text) = "" Then
        MsgBox "请输入要查询的信息!", 48, "提示"
        Exit Sub
    End If
    
    '第一层查询
    txtSQL = "select * from line_info where "
    txtSQL = txtSQL & fieldname(Comboa1.Text) & Trim(Combob1.Text) & Trim(txtc1.Text)
    
    '第一个组合查询
    If Trim(Combod1.Text <> "") Then
        If Trim(Comboa2.Text) = "" Or Trim(Combob2.Text) = "" Or Trim(txtc2.Text) = "" Then
            MsgBox "请输入第二行查询信息,再点击查询!", vbOKOnly, "提示"
            Exit Sub
        Else
            txtSQL = txtSQL & " " & fieldname(Combod1.Text) & " " & fieldname(Comboa2.Text) & Trim(Combob2.Text) & " '" & Trim(txtc2.Text) & "'"
        End If
    End If
    '第二个组合查询
    If Trim(Combod2.Text <> "") Then
        If Trim(Comboa3.Text) = "" Or Trim(Combob3.Text) = "" Or Trim(txtc3.Text) = "" Then
            MsgBox "请输入第三行查询信息,再点击查询!", vbOKOnly, "提示"
            Exit Sub
        Else
            txtSQL = txtSQL & " " & fieldname(Combod2.Text) & " " & fieldname(Comboa3.Text) & Trim(Combob3.Text) & " '" & Trim(txtc3.Text) & "'"
        End If
    End If
    '执行查询语句
    Set lmrc = ExecuteSQL(txtSQL, MsgText)
    
    '将信息加载到mshflexgrid控件中
    With MSHFlexGrid1
        .Rows = 1
        .CellAlignment = 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) = "备注"
        Do While Not lmrc.EOF
        .Rows = .Rows + 1
        .CellAlignment = 4
        .TextMatrix(.Rows - 1, 0) = Trim(lmrc.Fields(1))
        .TextMatrix(.Rows - 1, 1) = Trim(lmrc.Fields(3))
        .TextMatrix(.Rows - 1, 2) = Format(lmrc.Fields(6), "yyyy-mm-dd")
        .TextMatrix(.Rows - 1, 3) = Format(lmrc.Fields(7), "hh:mm:ss")
        .TextMatrix(.Rows - 1, 4) = Format(lmrc.Fields(8), "yyyy-mm-dd")
        .TextMatrix(.Rows - 1, 5) = Format(lmrc.Fields(9), "hh:mm:ss")
        .TextMatrix(.Rows - 1, 6) = Trim(lmrc.Fields(11))
        .TextMatrix(.Rows - 1, 7) = Trim(lmrc.Fields(12))
        .TextMatrix(.Rows - 1, 8) = Trim(lmrc.Fields(13))
        lmrc.MoveNext
        Loop
    End With
    lmrc.close
    
    If MSHFlexGrid1.Rows = 1 Then
        MsgBox "没有您要查询的记录!", vbOKOnly + vbExclamation, "警告"
        Comboa1.SetFocus
    End If
End Sub

    组合查询在学生信息系统中有接触,其实理解了并不困难,只要先理清思路,然后手,这样就比较简单了,其难点在于查询语句,其中多一个标点符号,或缺个空格使其报错,查找起来也会比较困难,所以大家在敲SQL查询语句代码的时候要特别注意,搞清楚符号的意义,搞定组合查询就没问题了。


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