机房收费系统中的组合查询也是一大亮点。简单的查询只有一个条件限制,机房收费系统中涉及到的最多的查询条件限制是三个,就一起来揭开他的庐山真面目吧!
<span style="font-size:18px;"><strong>Public Function relation(a As String) As String '转换或和与 Select Case a Case "与" relation = "and" Case "或" relation = "or" End Select End Function</strong></span>
<strong>Public Function filename(strfilename As String) '将选择的查询条件转换为数据库中的字段名 Select Case strfilename Case "教师" filename = "userid" Case "注册日期" filename = "logindate" Case "注册时间" filename = "logintime" Case "注销日期" filename = "logoutdate" Case "注销时间" filename = "logouttime" Case "机器名" filename = "computer" End Select </strong> End Function
<span style="font-size:18px;"><strong><span style="font-size:18px;">Private Sub cmdQuery_Click() Dim txtsql As String Dim msgtext As String Dim mrc As ADODB.Recordset Dim ctrl As Control</span><span style="font-size:24px;"> </span><span style="font-size:18px;"> '第一行可用 '判断第一行信息是否有空值 If Trim(comField1.Text) = "" Or Trim(comOperate1.Text) = "" Or Trim(txtContent1.Text) = "" Then MsgBox "请填充完整信息", vbOKOnly + vbExclamation, "警告" Exit Sub End If '将第一行的信息连接到txtsql中 txtsql = "select * from worklog_info where " txtsql = txtsql & filename(comField1.Text) & Trim(comOperate1.Text) & "'" & Trim(txtContent1.Text) & "'" '第二行可用 If Trim(comRelation(0).Text) <> "" Then '判断第二行信息是否有空值 If Trim(comField2.Text) = "" Or Trim(comOperate2.Text) = "" Or Trim(txtContent2.Text) = "" Then MsgBox "请填充完整信息", vbOKOnly + vbExclamation, "警告" Exit Sub End If txtsql = txtsql & relation(comRelation(0).Text) & " " & filename(comField2.Text) & Trim(comOperate2.Text) & "'" & Trim(txtContent2.Text) & "'" End If '第三行可用 If Trim(comRelation(1).Text) <> "" Then '判断第三行信息是否有空值 If Trim(comField3.Text) = "" Or Trim(comOperate3.Text) = "" Or Trim(txtContent3.Text) = "" Then MsgBox "请填充完整信息", vbOKOnly + vbExclamation, "警告" Exit Sub End If txtsql = txtsql & " " & relation(comRelation(1).Text) & " " & filename(comField3.Text) & comOperate3.Text & "'" _ & txtContent3.Text & "'" End If Set mrc = Executesql(txtsql, msgtext) '当没有记录的时候 If mrc.EOF Then MsgBox "没有此记录", vbOKOnly + vbExclamation, "警告" '清除一类控件 For Each ctrl In Me.Controls If TypeOf ctrl Is TextBox Or TypeOf ctrl Is ComboBox Then ctrl.Text = "" End If Next '清空myflexgrid控件 MyFlexGrid.Clear Call Form_Load Exit Sub End If With MyFlexGrid '设置列宽度 For i = 0 To 8 MyFlexGrid.ColWidth(i) = 1500 Next i '添加第一行信息 .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 mrc.EOF .Rows = .Rows + 1 .CellAlignment = 4 On Error Resume Next .TextMatrix(.Rows - 1, 0) = Trim(mrc.Fields("serial")) .TextMatrix(.Rows - 1, 1) = Trim(mrc.Fields("userid")) .TextMatrix(.Rows - 1, 2) = Trim(mrc.Fields("level")) .TextMatrix(.Rows - 1, 3) = Trim(mrc.Fields("logindate")) .TextMatrix(.Rows - 1, 4) = Trim(mrc.Fields("logintime")) .TextMatrix(.Rows - 1, 5) = Trim(mrc.Fields("logoutdate")) .TextMatrix(.Rows - 1, 6) = Trim(mrc.Fields("logouttime")) .TextMatrix(.Rows - 1, 7) = Trim(mrc.Fields("computer")) .TextMatrix(.Rows - 1, 8) = Trim(mrc.Fields("status")) mrc.MoveNext Loop mrc.Close End With </span><span style="font-size:24px;"> End Sub</span></strong></span>
组合查询并不难,他就是将简单查询的条件由一个变为了两个、三个。将查询语句该连接的连接起来就行!不要把自己先吓到,敲代码之前先想想和简单查询的相似之处和区别之处,遇到像“或”、“与”这类的汉字时无法直接连接,就要去考虑用自定义函数去转换!努力建立自己的知识网ing~