【VB】机房收费系统(组合查询)

本文介绍了一个使用Visual Basic开发的学生信息查询系统。该系统通过定义字段名称映射和关系运算符来实现对数据库中学生信息的有效检索。文章详细展示了如何通过组合框选择查询条件,并生成SQL查询语句。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

涉及组合窗体:

学生基本信息维护窗体,学生上机统计信息窗体,操作员工作记录窗体

定义函数:

我们需要定义一个Field函数,用来使VB程序中的字段名和数据库中的列名对应:

 

Public Function FiledName(StrFiledName As String) As String
    Select Case StrFiledName
    Case "卡号"
        FiledName = "cardno"
    Case "学号"
        FiledName = "studentno"
    Case "姓名"
        FiledName = "studentname"
    Case "性别"
        FiledName = "sex"
    Case "系别"
        FiledName = "department"
    Case "年级"
        FiledName = "grade"
    Case "班级"
        FiledName = "class"
    End Select
End Function

Public Function Relationname(strRelationname As String) As String
    Select Case strRelationname
        Case "与"
            Relationname = "and"
        Case "或"
            Relationname = "or"
    End Select
End Function

然后理清思路,可以自己画个简单的图,判断3个组合关系:

 

 

Private Sub Command1_Click()
    Dim txtSQL As String
    Dim MsgText As String
    Dim mrc As ADODB.Recordset
    
    If ComboZD1.Text = "" Then
        MsgBox "请选择字段名!", vbOKOnly + vbExclamation, "提示"
        ComboZD1.SetFocus
        Exit Sub
    End If
    
    If ComboCZ1.Text = "" Then
        MsgBox "请选择操作符!", vbOKOnly + vbExclamation, "提示"
        ComboCZ1.SetFocus
        Exit Sub
    End If
    
    If txtCX1.Text = "" Then
        MsgBox "请输入要查询的内容"
        txtCX1.SetFocus
        Exit Sub
    End If
    
    txtSQL = "select * from student_Info where " & FiledName(ComboZD1.Text) & ComboCZ1.Text & " '" & txtCX1.Text & "'"
    
    If Trim(ComboZH1(0).Text) <> "" Then
        If Trim(ComboCZ1.Text) = "" Or Trim(txtCX2.Text) = "" Or Trim(ComboZH1(0).Text) = "" Then
            MsgBox "您选择了第一种组合关系,请输入完整信息再查询", vbExclamation + vbOKOnly, "提示"
            Exit Sub
        Else
        
            txtSQL = "select * from student_Info where " & FiledName(ComboZD1.Text) & ComboCZ1.Text & "'" _
            & Trim(txtCX1.Text) & "'" & " " & Relationname(ComboZH1(0).Text) & " " & FiledName(ComboZD2.Text) _
            & ComboCZ2.Text & "'" & Trim(txtCX2.Text) & "'"
        End If
    
    End If
    
    If Trim(ComboZH1(1).Text) <> "" Then
        If Trim(ComboCZ3.Text) = "" Or Trim(txtCX3.Text) = "" Or Trim(ComboZH1(1).Text) = "" Then
            MsgBox "您选择了第二种组合关系,请输入完整信息再查询", vbExclamation + vbOKOnly, "提示"
            Exit Sub
        Else
        
            txtSQL = "select * from student_Info where " & FiledName(ComboZD1.Text) & ComboCZ1.Text & "'" _
            & Trim(txtCX1.Text) & "'" & " " & Relationname(ComboZH1(0).Text) & " " & FiledName(ComboZD2.Text) _
            & ComboCZ2.Text & "'" & Trim(txtCX2.Text) & "'" & " " & Relationname(ComboZH1(1).Text) & " " & _
            FiledName(ComboZD3.Text) & ComboCZ3.Text & "'" & Trim(txtCX3.Text) & "'"
        End If
    End If

    Set mrc = ExecuteSQL(txtSQL, MsgText)
    If mrc.EOF Then
        MsgBox "没有您要查找的学生,请重新查询!", vbOKOnly + vbExclamation, "提示"
    Else
        Do While Not mrc.EOF
            With MSHFlexGrid1
                .Rows = .Rows + 1
                .CellAlignment = 4
                .TextMatrix(.Rows - 1, 0) = Trim(mrc.Fields(0))
                .TextMatrix(.Rows - 1, 1) = Trim(mrc.Fields(1))
                .TextMatrix(.Rows - 1, 2) = Trim(mrc.Fields(2))
                .TextMatrix(.Rows - 1, 3) = Trim(mrc.Fields(3))
                .TextMatrix(.Rows - 1, 4) = Trim(mrc.Fields(4))
                .TextMatrix(.Rows - 1, 5) = Trim(mrc.Fields(5))
                .TextMatrix(.Rows - 1, 6) = Trim(mrc.Fields(6))
                .TextMatrix(.Rows - 1, 7) = Trim(mrc.Fields(7))
                .TextMatrix(.Rows - 1, 8) = Trim(mrc.Fields(8))
                .TextMatrix(.Rows - 1, 9) = Trim(mrc.Fields(10))
                .TextMatrix(.Rows - 1, 10) = Trim(mrc.Fields(12))
                .TextMatrix(.Rows - 1, 11) = Trim(mrc.Fields(13))
                .TextMatrix(.Rows - 1, 12) = Trim(mrc.Fields(14))
            End With
            mrc.MoveNext
        Loop
    End If
            mrc.Close
End Sub


总结:

 

我的这个有点儿太繁杂了,可以让代码更简便一点儿。
 

评论 31
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值