所有的伟大只源于一个勇敢地开始。
组合查询之前以为很难,但是真正去开始敲了,你就会发现并不难。敲完了第一个,基本上就算是敲完了。
这是机房收费系统里面组合查询的标准样式,之所以叫组合查询,是因为我们要将若干个条件选择出来之后组合在一起来查询。首先,组合查询分为三组查询条件和两个组合关系,组合关系两两连接组合条件。所以当给你选择了第一个组合关系之后,必须要有前两行的查询条件,选择了第二个组合关系之后三行条件必须全部选好。反之如果不选组合关系,那么只能查询第一行的查询内容。
以学生基本信息维护为例来做一个讲解。
首先,思路要清晰,所以流程图先画好。
之后,根据流程图来写代码。
这是组合查询部分代码。
Dim txtsql As String
Dim msgText As String
Dim mrc As ADODB.Recordset
Dim ctl As Control
Dim ctl1 As Control
txtsql = "select * from student_info where"
'检查输入条件
If Trim(comboField1.Text) = "" Or Trim(comboOpSign1.Text) = "" Or Trim(Text1.Text) = "" Then
MsgBox "请输入完整的查询条件", , "苏轼提醒您"
Exit Sub
End If
'将第一行查询内容组合
txtsql = txtsql & " " & Trim(Field(comboField1.Text)) & " " & Trim((comboOpSign1.Text)) & " " & "'" & Trim(Text1.Text) & "'"
If Trim(comboCombineRe1.Text <> "") Then '第一个组合关系存在
If Trim(comboField2.Text) = "" Or Trim(comboOpSign2.Text = "") Or Trim(Text2.Text = "") Then
MsgBox "你已经选择了第一个组合关系,请输入第二行查询条件", , "苏轼提醒您"
Exit Sub
Else
txtsql = txtsql & " " & Field(Trim(comboCombineRe1.Text)) & " " & Field(comboField2.Text) & " " & comboOpSign2.Text & " " & "'" & Trim(Text2.Text) & "'"
End If
End If
If Trim(comboCombineRe2.Text <> "") Then '第二个组合关系存在
If Trim(comboField3.Text) = "" Or Trim(comboOpSign3.Text = "") Or Trim(Text3.Text = "") Then
MsgBox "你已经选择了第二个组合关系,请输入第三行查询条件", , "苏轼提醒您"
Exit Sub
Else
txtsql = txtsql & " " & Field(Trim(comboCombineRe2.Text)) & " " & Field(comboField3.Text) & " " & comboOpSign3.Text & " " & "'" & Trim(Text2.Text) & "'"
End If
End If
Set mrc = ExecuteSQL(txtsql, msgText)
If mrc.EOF = True Then '检查信息是否存在,如果不存在给出提示并清空所有文本框
MsgBox "没有查询到结果,可能你输入的信息不存在,或者信息矛盾。"
For Each ctl In Controls
'清除所有文本
……
Exit Sub
End If
'匹配
With myFlexGrid
.rows = 1 '第一行赋名
.CellAlignment = 4 '单元格的内容居中,居中对齐
.TextMatrix(0, 0) = "卡号"
……
Do While Not mrc.EOF
.rows = .rows + 1
.CellAlignment = 4
.TextMatrix(.rows - 1, 0) = Trim(mrc.Fields(0))
……
mrc.MoveNext
Loop
End With
基本上把上面这些代码弄明白,组合查询就差不多了。
另外,添加几个小技巧。
一键清除所有文本。本来想把代码弄到模块了,到时候直接调用,但是没有成功,有大神会的可以来教教我。
Dim ctl As Control
Dim ctl1 As Control
For Each ctl In Controls
'清除所有文本
If TypeOf ctl Is TextBox Then
ctl.Text = ""
End If
Next ctl
'清除combobox文本
For Each ctl1 In Controls
If TypeOf ctl1 Is ComboBox Then
ctl1.Text = ""
End If
Next ctl1
myFlexGrid.clear
定义一个函数让text里的文本和数据库里的字段对应。
Public Function Field(i As String) As String
'定义一个函数让text里的文本和数据库里的字段对应
Select Case i
Case "卡号"
Field = "cardno"
Case "学号"
Field = "studentno"
……
Case "与"
Field = "and"
……
End Select
End Function
当点击上机日期,上机时间这样的条件的时候,记得提醒输入日期查询各式。可以设置一个温馨弹窗。另外像姓名、性别这类的条件操作符只有“=”和“<>”,所以我们可以做一个优化。
Private Sub comboField1_click()
If comboField1.Text = "上机日期" Or comboField1.Text = "上机时间" Or comboField1.Text = "下机日期" Or comboField1.Text = "下机时间" Then
MsgBox "日期查询格式为yyyy/mm/dd,时间查询格式为hh:mm:ss!", vbOKOnly + vbExclamation, "苏轼提醒您"
End If
Select Case comboField1.Text
Case "姓名", "备注"
comboOpSign1.clear
comboOpSign1.AddItem "="
comboOpSign1.AddItem "<>"
Case "卡号", "上机日期", "上机时间", "下机日期", "下机时间", "消费金额", "余额"
comboOpSign1.clear
comboOpSign1.AddItem "="
comboOpSign1.AddItem "<>"
comboOpSign1.AddItem "<"
comboOpSign1.AddItem ">"
End Select
End Sub
敲过之后感觉不难,所以我们不能等有感觉了才开始行动。
加油。
版权声明:本文为wzh66888原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。