组合查询算是机房收费系统中的一个难点了,经过两天的探索,组合查询终于实现了!回头看,恍然大悟:组合查询也不过是一句SQL语句的问题。跟平时的查询大同小异。哎,大道至简啊!
下面以学生上机统计信息为例。
首先,我们要先定义一个过程,把控件中的运算符与SQL语句联系起来。
<span style="font-family:KaiTi_GB2312;font-size:24px;">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" Case "与" Field = "and" Case "或" Field = "or" End Select End Function </span>然后就可以直接使用控件中的信息,去读取数据库中的信息了。
<span style="font-family:KaiTi_GB2312;font-size:24px;">Private Sub cmdQuery_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 '从数据库表line_Info表中读取数据 txtSQL = "select * from line_Info where " 'txtSQL = 原来读取的+ 第一个字段+操作符+查询内容 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) & Trim(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 </span>
一开始我在写代码时,从数据库中一直读取不出信息。后来发现只是一个小小空格的问题。
例如:正确代码:
<span style="font-family:KaiTi_GB2312;font-size:24px;">txtSQL = txtSQL & Field(ComboRelation2.Text) & " " & Field(ComboField3.Text) & ComboOperator3.Text & "'" & Trim(txtTest3.Text) & "'"</span>写成这样就不对:
<span style="font-family:KaiTi_GB2312;font-size:24px;">txtSQL = txtSQL & Field(ComboRelation2.Text) & "" & Field(ComboField3.Text) & ComboOperator3.Text & "'" & Trim(txtTest3.Text) & "'"</span>我们在平时学习是不能放过任何一个细节的。
本着为人民服务的原则,组合查询还是有些地方需要改进的。比如选择了日期查询,应该有个格式提示,或者可以控件选择;还有,组合关系为空,我们可以把下一层控件设置为不可用。。等等。
还是有很多地方需要改进的。