机房收费系统(组合查询)

时间:2022-09-16 10:32:12

涉及组合窗体:

学生基本信息维护窗体,学生上机统计信息窗体,操作员工作记录窗体

定义函数:

我们需要定义一个Field函数,用来使VB程序中的字段名和数据库中的列名对应:

Public Function FiledName(StrFiledName As String) As String
Select Case StrFiledName
Case "卡号"
FiledName = "cardno"
Case "学号"
FiledName = "studentno"
Case "姓名"
FiledName = "studentname"
Case "性别"
FiledName = "sex"
Case "系别"
FiledName = "department"
Case "年级"
FiledName = "grade"
Case "班级"
FiledName = "class"
End Select
End Function

Public Function Relationname(strRelationname As String) As String
Select Case strRelationname
Case "与"
Relationname = "and"
Case "或"
Relationname = "or"
End Select
End Function
然后理清思路,可以自己画个简单的图,判断3个组合关系:

Private Sub Command1_Click()
Dim txtSQL As String
Dim MsgText As String
Dim mrc As ADODB.Recordset

If ComboZD1.Text = "" Then
MsgBox "请选择字段名!", vbOKOnly + vbExclamation, "提示"
ComboZD1.SetFocus
Exit Sub
End If

If ComboCZ1.Text = "" Then
MsgBox "请选择操作符!", vbOKOnly + vbExclamation, "提示"
ComboCZ1.SetFocus
Exit Sub
End If

If txtCX1.Text = "" Then
MsgBox "请输入要查询的内容"
txtCX1.SetFocus
Exit Sub
End If

txtSQL = "select * from student_Info where " & FiledName(ComboZD1.Text) & ComboCZ1.Text & " '" & txtCX1.Text & "'"

If Trim(ComboZH1(0).Text) <> "" Then
If Trim(ComboCZ1.Text) = "" Or Trim(txtCX2.Text) = "" Or Trim(ComboZH1(0).Text) = "" Then
MsgBox "您选择了第一种组合关系,请输入完整信息再查询", vbExclamation + vbOKOnly, "提示"
Exit Sub
Else

txtSQL = "select * from student_Info where " & FiledName(ComboZD1.Text) & ComboCZ1.Text & "'" _
& Trim(txtCX1.Text) & "'" & " " & Relationname(ComboZH1(0).Text) & " " & FiledName(ComboZD2.Text) _
& ComboCZ2.Text & "'" & Trim(txtCX2.Text) & "'"
End If

End If

If Trim(ComboZH1(1).Text) <> "" Then
If Trim(ComboCZ3.Text) = "" Or Trim(txtCX3.Text) = "" Or Trim(ComboZH1(1).Text) = "" Then
MsgBox "您选择了第二种组合关系,请输入完整信息再查询", vbExclamation + vbOKOnly, "提示"
Exit Sub
Else

txtSQL = "select * from student_Info where " & FiledName(ComboZD1.Text) & ComboCZ1.Text & "'" _
& Trim(txtCX1.Text) & "'" & " " & Relationname(ComboZH1(0).Text) & " " & FiledName(ComboZD2.Text) _
& ComboCZ2.Text & "'" & Trim(txtCX2.Text) & "'" & " " & Relationname(ComboZH1(1).Text) & " " & _
FiledName(ComboZD3.Text) & ComboCZ3.Text & "'" & Trim(txtCX3.Text) & "'"
End If
End If

Set mrc = ExecuteSQL(txtSQL, MsgText)
If mrc.EOF Then
MsgBox "没有您要查找的学生,请重新查询!", vbOKOnly + vbExclamation, "提示"
Else
Do While Not mrc.EOF
With MSHFlexGrid1
.Rows = .Rows + 1
.CellAlignment = 4
.TextMatrix(.Rows - 1, 0) = Trim(mrc.Fields(0))
.TextMatrix(.Rows - 1, 1) = Trim(mrc.Fields(1))
.TextMatrix(.Rows - 1, 2) = Trim(mrc.Fields(2))
.TextMatrix(.Rows - 1, 3) = Trim(mrc.Fields(3))
.TextMatrix(.Rows - 1, 4) = Trim(mrc.Fields(4))
.TextMatrix(.Rows - 1, 5) = Trim(mrc.Fields(5))
.TextMatrix(.Rows - 1, 6) = Trim(mrc.Fields(6))
.TextMatrix(.Rows - 1, 7) = Trim(mrc.Fields(7))
.TextMatrix(.Rows - 1, 8) = Trim(mrc.Fields(8))
.TextMatrix(.Rows - 1, 9) = Trim(mrc.Fields(10))
.TextMatrix(.Rows - 1, 10) = Trim(mrc.Fields(12))
.TextMatrix(.Rows - 1, 11) = Trim(mrc.Fields(13))
.TextMatrix(.Rows - 1, 12) = Trim(mrc.Fields(14))
End With
mrc.MoveNext
Loop
End If
mrc.Close
End Sub

总结:

我的这个有点儿太繁杂了,可以让代码更简便一点儿。