
机房收费系统在磕磕绊绊中过来了,这期间遇到问题,解决问题,最后收获的特别多,在敲得过程中,不断的学习新知识,应该说组合查询是收费系统的一个小难点了吧,起初我是真的不知道该从哪里下手,总是有种剪不断理还乱的感觉,分析分析就绕进去了,我总是把问题想的很复杂,其实只要一句SQL语句,一切都解决了,根本用不到好多好多的IF,FOr 什么的,下面是我自己在实现此过程所需要的代码:以收费系统中基本信息维护为例子,其实其他的也都大同小异
只要一句正确的SQL,其他全OK!
刚着手时看到这个确实有点迷糊,该从何下手呢?理清楚了才明白,其实很简单,在这里我是用组合关系为点,进行判断,如果组合关系中什么都没有选择,那么就认为查询条件为第一行,而且必须选择完全,如果选择了组合关系,那么相对应的2或3行条件就需要填充完整,接着就是按照条件在数据库中查询内容了。而我的字段名选择是汉字,要想在数据库中查询字段,根本就不认识,这就需要我将汉字转换为相应的字符串,代码中的FieldName 就是我要利用到的一个函数
Private Sub cmdInquire_Click() : Dim mrc As ADODB.Recordset : Dim txtSQL As String ; Dim MsgText As String ! ! txtSQL = settle upon * both to and from student_Info during which time '判断字段的选择是否为空 : If Not Testtxt(comboFiledName1.Text) Then : MsgBox "请选择字段!", vbOKOnly + vbExclamation,! !"警告"! ! ! comboFiledName1.SetFocus : Exit Sub ! ! ! End If ! ! ! '判断操作符的选择是否为空 : If Not Testtxt(comboOperate1.Text) Then ! ! ! ! ! MsgBox "请选择操作符!", vbOKOnly + vbExclamation,:"警告": comboOperate1.SetFocus ! ! ! Exit Sub ! ! ! End If ! ! ! '判断要查询的内容是否为空 ! ! If Not Testtxt(txtContent1.Text) Then : d MsgBox "请在输入要查询的内容", vbOKOnly + vbExclamation,! ! !"警告"! ! ! txtContent1.SetFocus ! ! ! ! ! Exit Sub ! ! ! ! ! End If ! ! ! ! ! txtSQL equals txtSQL & FiledName(comboFiledName1.Text) & the reason is & comboOperate1.Text & wi & txtContent1.Text & ' '利用模版函数查看是否是组合查询第一行为空时,查询无效 ! ! ! '开始组合查询 ! ! ! If Trim(comboRelation1.Text <> the reason being Then ; If Trim(comboFiledName2.Text) = the reason is Or Trim(comboOperate2.Text) = the excuse is Or Trim(txtContent2.Text) = associated with Then ! ! ! MsgBox "您选择了组合关系,请输入数据之后再查询", vbOKOnly,! ! !"提示信息"! ! ! ! ! Exit Sub ! ! ! ! ,
コンバース 靴紐! Else : txtSQL = txtSQL & FiledName(comboRelation1.Text) & " & FiledName(comboFiledName2.Text) & comboOperate2.Text & wi & Trim(txtContent2.Text) & wi End If : d End If : If Trim(comboRelation2.Text) <> the excuse is Then ; If Trim(comboFiledName3,
ダウン レディース.Text) equals the reason is Or Trim(comboOperate3.Text) = this is because Or Trim(txtContent3.Text) equals graphs Then : MsgBox "您选择了第二个组合,请输入数据之后在查询", vbOKOnly,! ! ! ! !"提示": Exit Sub : d Else ! ! ! ! ! txtSQL equals txtSQL & FiledName(comboRelation2,
モンスタービーツ ピンク.Text) & " & FiledName(comboFiledName3.Text) & comboOperate3,
クリスチャンルブタン パンプス.Text & wi & Trim(txtContent3.Text) & wi End If ; End If : '开始进行查找 ! ! ! Set mrc equals ExecuteSQL(txtSQL, MsgText) : d If mrc.RecordCount = 0 Then : MsgBox "没有您要查找的学生上机记录!", vbOKOnly + vbCritical,! ! !"查询提示"; comboFiledName1.SetFocus ! ! MSHFlexGrid1.Rows equals an '存在上机记录查询成功,填充到表格 : Else ! ! MSHFlexGrid1.Rows equals one Do While Not mrc.EOF ! ! ! With MSHFlexGrid1 :.CellAlignment = four.Rows =.Rows + one.TextMatrix(.Rows as tall as one 0) equals mrc!studentNo ! ! !.TextMatrix(.Rows up to one one = mrc!studentName ! ! !.TextMatrix(.Rows often an two equals mrc!cardno ! ! !.TextMatrix(.Rows - an 3 equals mrc!a lot of cash.TextMatrix(.Rows as high as 1,four equals mrc!office.TextMatrix(.Rows - an,five = mrc!fine quality.TextMatrix(.Rows up to one six equals mrc!Class :.TextMatrix(.Rows all the way to 1 7 equals mrc!Sex :.TextMatrix(.Rows - 1,eight equals mrc!Status ! ! !.TextMatrix(.Rows - one,nine equals mrc!make it known End With : mrc.MoveNext ; Loop : End If ! ! Exit SubEnd Sub
'将汉字转换为相应的字段名字Public Function FiledName(StrFiledName As String) As String ! ! ,
ノースフェイス ジャケット! Select Case StrFiledName ! ! Case "卡号"! ! ! FiledName equals"cardno"; Case "学号": d FiledName ="studentNo"! ! ! Case "姓名"! ! ! FiledName ="studentName"! ! Case "性别": FiledName ="Sex": Case "学院": d FiledName = division Case "年级"! ! ! FiledName equals quality Case "班级"! ! ! ! ! FiledName = thinking about Case "与"! ! ! ! ! FiledName = not only that but Case "或"! ! FiledName equals or at least End SelectEnd Function
这样一个组合查询完成了,重其中我也收获了财富,遇到错误的时候就是进步的时候,实现这个功能最大的感慨就是SQL语句真的很强大,而我还处在最低级,以后还要努力加油了!
Related articles: