机房收费系统——组合查询
机房收费系统敲到这,算是遇到真正的难题了。用了我整整一下午的时间,晚饭都没顾上吃才攻克了。
这个窗体难点就在组合查询,很考验人的思维能力,要想实现这个窗体必须要有清晰地思路。
首先判断各种情况:
1、 只使用第一行查询条件
2、 只不使用第一行查询条件即选择第二、三行查询条件
3、 只使用第二种查询条件(这里没有只不选择第二行查询条件,因为第一行跟第三行中间没有关系符号)
4、 只使用第三行查询条件
5、 只不使用第三行查询条件即选择第一、二行查询条件
6、 三行查询条件都使用
我个人觉得这样分析条理性比较好,只是代码量写的会比较大,但是各种情况之间代码变化不大,所以下面只写出第一二种情况的代码,其他情况可以都可以照着写出来。
If (Ok1 = False And Ok2 = False And Ok3 = False) Then
MsgBox "请输入查询条件", vbOKOnly + vbInformation, "提示"
combofield1(0).SetFocus
End If
If (Ok1 = True And Ok2 = False And Ok3 = False) Then '只选择第一行查询条件
Txtsql = "select * from online_info where " & FieldName & " " & Trim(Combosign1(0).Text) & "'" & Trim(Txtcontent1.Text) & "'"
Set mrc = ExecuteSQL(Txtsql, Msgtext)
If mrc.EOF Then
MsgBox "搜索到的记录为空", vbOKOnly + vbInformation, "提示"
Exit Sub
Else
With MYFlexGrid
.Cols = 5
.Rows = 1 '防止空白行
.TextMatrix(0, 0) = "卡号"
.TextMatrix(0, 1) = "姓名"
.TextMatrix(0, 2) = "上机日期"
.TextMatrix(0, 3) = "上机时间"
.TextMatrix(0, 4) = "机房号"
For i = 1 To mrc.RecordCount
.Rows = .Rows + 1
.TextMatrix(.Rows - 1, 0) = mrc.Fields(0)
.TextMatrix(.Rows - 1, 1) = mrc.Fields(3)
.TextMatrix(.Rows - 1, 2) = mrc.Fields(6)
.TextMatrix(.Rows - 1, 3) = mrc.Fields(7)
.TextMatrix(.Rows - 1, 4) = mrc.Fields(8)
mrc.MoveNext
Next i
mrc.Close
End With
End If
ElseIf (Ok1 = False And Ok2 = True And Ok3 = True) Then ''只有第一行查询条件不被选择,即选择第二、三行查询条件
If Trim(Comborelation1(1)) = "" Then
MsgBox "请选择第二个组合关系", vbOKOnly + vbInformation, "提示"
Comborelation1(1).SetFocus
Exit Sub
Else
Txtsql = "select * from online_info where " & FieldName & " " & Trim(Combosign1(1).Text) & "'" & Trim(Txtcontent2.Text) & "'"
Txtsql = Txtsql & Strrelation & " " & FieldName & " " & Trim(Combosign1(2).Text) & "'" & Trim(Txtcontent3.Text) & "'"
Set mrc = ExecuteSQL(Txtsql, Msgtext)
If mrc.EOF Then
MsgBox "搜索到的记录为空", vbOKOnly + vbInformation, "提示"
Exit Sub
Else
With MYFlexGrid
.Cols = 5
.Rows = 1 '防止空白行
.TextMatrix(0, 0) = "卡号"
.TextMatrix(0, 1) = "姓名"
.TextMatrix(0, 2) = "上机日期"
.TextMatrix(0, 3) = "上机时间"
.TextMatrix(0, 4) = "机房号"
For i = 1 To mrc.RecordCount
.Rows = .Rows + 1
.TextMatrix(.Rows - 1, 0) = mrc.Fields(0)
.TextMatrix(.Rows - 1, 1) = mrc.Fields(3)
.TextMatrix(.Rows - 1, 2) = mrc.Fields(6)
.TextMatrix(.Rows - 1, 3) = mrc.Fields(7)
.TextMatrix(.Rows - 1, 4) = mrc.Fields(8)
mrc.MoveNext
Next i
mrc.Close
End With
End If
End If
End If