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

机房收费系统已经结束了,正在整理和完善自己的代码,优化代码,在整个过程中遇到了几个比较棘手的部分,也是整个机房收费的核心学习点。

(一):机房收费系统——组合查询

一、界面设计:

二、运行结果:

三、功能解析:窗体加载时显示控件信息以及MSHFlexGrid1中显示的表头。

                 查询时:按照不同的条件查询,单组查询和多组查询结合,关键是编写多组查询的代码,查询条件的设定。

'检查输入的内容是否满足要求,是否已经输入内容等。下面以文本框中的输入内容为例

If txtContent1.Text = "" Then

        MsgBox "请输入查询内容!", vbOKOnly + vbExclamation, "提示"

        txtContent1.SetFocus

        Exit Sub

End If

 

 '如果只有一个查询条件

 

    If cboRelation(0).Text = "" AndcboRelation(1).Text = "" Then

        strSQL = "select * fromstudent_Info where " & strFields(0) & cboOperator(0).Text &"'" &Trim(txtContent1.Text) & "'"

        Set objrst = ExecuteSQL(strSQL,msgtext)

    End If

 

'如果为一二组合查询

    If cboRelation(0).Text <>"" And cboRelation(1).Text = "" Then

        If txtContent2.Text = "" Then

            MsgBox "请输入查询内容!", vbOKOnly + vbExclamation, "提示"

            txtContent2.SetFocus

            Exit Sub

        End If

        strSQL = "select * fromstudent_Info where " & strFields(0) & cboOperator(0).Text &"'" &Trim(txtContent1.Text) & "'" _

                                         &" " & strRelation(0) & " " & strFields(1) &cboOperator(1).Text & "'" &Trim(txtContent2.Text) &"'"

        Set objrst = ExecuteSQL(strSQL, msgtext)

    End If

 

'

'    '如果为一二三组合查询

    If cboRelation(0).Text <>"" And cboRelation(1).Text <> "" Then

        If txtContent2.Text = "" Then

            MsgBox "请输入要查询的内容!", vbOKOnly + vbExclamation, "提示"

            txtContent2.SetFocus

            Exit Sub

        End If

        If txtContent3.Text = "" Then

            MsgBox "请输入要查询的内容!", vbOKOnly + vbExclamation, "提示"

            txtContent3.SetFocus

            Exit Sub

        End If

 

        strSQL = "select * fromstudent_Info where " & strFields(0) & cboOperator(0).Text &"'" &Trim(txtContent1.Text) & "'" _

                        & " "& strRelation(0) & " " & strFields(1) &cboOperator(1).Text & "'" & Trim(txtContent2.Text)&"'" _

                        & " "& strRelation(1) & " " & strFields(2) &cboOperator(2).Text & "'" & Trim(txtContent3.Text)&"'"

        Set objrst = ExecuteSQL(strSQL,msgtext)

    End If

 

   '如果用户选择了组合关系2没选组合关系1,提示用户

 

    If cboRelation(0).Text = "" AndcboRelation(1).Text <> "" Then

        MsgBox "请选择合适的组合关系!", vbOKOnly + vbExclamation, "提示"

        cboRelation(0).SetFocus

        Exit Sub

End If


在查询时我们首先考虑如何让查询的条件和数据库中的表字段结合起来,采取的方法就是,用select case 语句分别设置字段和索引相符合。'通过索引选择条件,将条件与表中的字段对应


    For intindex = 0 To 2

        Select Case cbofields(intindex).Text

            Case "卡号"

                strFields(intindex) ="cardno"

            Case "学号"

                strFields(intindex) ="studentno"

            Case "姓名"

                strFields(intindex) ="studentname"

            Case "性别"

                strFields(intindex) ="sex"

            Case "系别"

                strFields(intindex) ="department"

            Case "年级"

                strFields(intindex) ="grade"

            Case "班号"

                strFields(intindex) ="class"

        End Select

    Next intindex

   

    For intindex = 0 To 1

        Select Case cboRelation(intindex).Text

            Case "与"

                strRelation(intindex) ="and"

            Case "或"

                strRelation(intindex) ="or"

        End Select

Next intindex


对于组合查询,只要明白程序如何运行,如何进行的对条件的查询,编写代码很容易,易错点就是在strSQL=”select * from 表 where 条件”中的条件如何编写,细节的符号等。

      

 

评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值