在机房收费系统中,组合查询用的还是挺多的,像上机状态查询窗体、学生上机统计信息窗体、操作员工记录窗体、基本信息维护窗体。这其中,学生基本信息维护窗体中的东西比较多,就以它为例子,说说组合查询吧!
学生基本信息维护窗体如图所示:
先把代码放在下面,然后再解说:
Option Explicit Private Sub cboField1_Click() '把选择的字段转换成 sql语句中的字段 If cboField1.Text = "卡号" Then cboField1.Tag = "cardNo" If cboField1.Text = "学号" Then cboField1.Tag = "studentNo" If cboField1.Text = "姓名" Then cboField1.Tag = "studentName" If cboField1.Text = "性别" Then cboField1.Tag = "sex" If cboField1.Text = "学院" Then cboField1.Tag = "college" If cboField1.Text = "专业" Then cboField1.Tag = "major" If cboField1.Text = "年级" Then cboField1.Tag = "grade" If cboField1.Text = "班级" Then cboField1.Tag = "class" End Sub Private Sub cboField2_Click() '把选择的字段转换成 sql语句中的字段 If cboField2.Text = "卡号" Then cboField2.Tag = "cardNo" If cboField2.Text = "学号" Then cboField2.Tag = "studentNo" If cboField2.Text = "姓名" Then cboField2.Tag = "studentName" If cboField2.Text = "性别" Then cboField2.Tag = "sex" If cboField2.Text = "学院" Then cboField2.Tag = "college" If cboField2.Text = "专业" Then cboField2.Tag = "major" If cboField2.Text = "年级" Then cboField2.Tag = "grade" If cboField2.Text = "班级" Then cboField2.Tag = "class" End Sub Private Sub cboField3_Click() '把选择的字段转换成 sql语句中的字段 If cboField3.Text = "卡号" Then cboField3.Tag = "cardNo" If cboField3.Text = "学号" Then cboField3.Tag = "studentNo" If cboField3.Text = "姓名" Then cboField3.Tag = "studentName" If cboField3.Text = "性别" Then cboField3.Tag = "sex" If cboField3.Text = "学院" Then cboField3.Tag = "college" If cboField3.Text = "专业" Then cboField3.Tag = "major" If cboField3.Text = "年级" Then cboField3.Tag = "grade" If cboField3.Text = "班级" Then cboField3.Tag = "class" End Sub Private Sub cboMode1_Click() '把选择的字段的组合关系转换成sql中的组合关系关键字,后面可以直接写入查询语句 If cboMode1.Text = "与" Then cboMode1.Tag = "and" If cboMode1.Text = "或" Then cboMode1.Tag = "or" cboField2.Visible = True cboSign2.Visible = True txtInquire2.Visible = True cboMode2.Visible = True End Sub Private Sub cboMode2_Click() '把选择的字段组合关系转换成sql中的组合关系关键字,后面可以直接写入查询语句 If cboMode2.Text = "与" Then cboMode2.Tag = "and" If cboMode2.Text = "或" Then cboMode2.Tag = "or" cboField3.Visible = True cboSign3.Visible = True txtInquire3.Visible = True End Sub Private Sub cmdClear_Click() '清空所有的属性 cboField1.Text = "" cboField2.Text = "" cboField3.Text = "" cboSign1.Text = "" cboSign2.Text = "" cboSign3.Text = "" txtInquire1.Text = "" txtInquire2.Text = "" cboMode1.Text = "" cboMode2.Text = "" cboField2.Visible = False '把多查询条件字段先隐藏'隐藏起多字段,这样刚刚开始,只显示第一条查询,如果需要,选择"与"或者"或",显示下一条字段,同理显示第三条字段。无形中告诉使用者,不要空掉第二条字段,只用一三条,这样会给编码带来麻烦的。 cboSign2.Visible = False txtInquire2.Visible = False cboMode2.Visible = False cboField3.Visible = False cboSign3.Visible = False txtInquire3.Visible = False myFlexGrid.Clear End Sub
Private Sub cmdcharge_Click() Dim txtsql As String Dim MsgText As String Dim mrcSD As ADODB.Recordset txtsql = "select * from student_Info where studentNo='" & Trim(myFlexGrid.TextMatrix(myFlexGrid.RowSel,0)) & "'" Set mrcSD = Executesql(txtsql,MsgText) If mrcSD.RecordCount = 0 Then MsgBox "请选中一条记录!",vbOKCancel + vbExclamation,"警告" Exit Sub End If frmModifyStudentLog.Show'显示查询窗体 End Sub Private Sub cmdExit_Click() Unload Me End Sub Private Sub cmdselect_Click() Dim txtsql As String Dim MsgText As String Dim mrc As ADODB.Recordset If cboMode1.Text = "" Then MsgBox "请输入需要查询的字段",vbOKOnly + vbExclamation,"警告" Unload Me End If '首先写一个不完全的sql语句,一会儿补充 txtsql = "select * from student_Info where (" & cboField1.Tag & cboSign1.Text & "'" & txtInquire1.Text & "'" If cboMode1.Text <> "" Then '检验第二个组合字段的正确性 If cboField2.Text = "" Then MsgBox "请选择字段名!","警告" cboField2.SetFocus Exit Sub End If If cboSign2.Text = "" Then MsgBox "请输入操作符!","警告" cboSign2.SetFocus Exit Sub End If If txtInquire2.Text = "" Then MsgBox "请输入要查询的内容","警告" txtInquire2.SetFocus Exit Sub End If '第二个组合字段正确,开始添加信息 txtsql = txtsql & " " & cboMode1.Tag & " " & cboField2.Tag & cboSign2.Text & "'" & txtInquire2.Text & "'" End If If cboMode2.Text <> "" Then '检验第三组 组合字段的正确性 If cboField3.Text = "" Then MsgBox "请选择字段名!","警告" cboField3.SetFocus Exit Sub End If If cboSign3.Text = "" Then MsgBox "请输入操作符!","警告" cboSign3.SetFocus Exit Sub End If If txtInquire3.Text = "" Then MsgBox "请输入要查询的内容","警告" txtInquire3.SetFocus Exit Sub End If '第三个组合字段正确,开始添加信息 txtsql = txtsql & " " & cboMode2.Tag & " " & cboField3.Tag & cboSign3.Text & "'" & txtInquire3.Text & "'" End If txtsql = txtsql & ") " '把sql语句补充完整 'Debug.Print txtsql 用于调试程序使用 Set mrc = Executesql(txtsql,MsgText) If mrc.RecordCount = 0 Then '如果要查询的结果为空,则提醒用户 myFlexGrid.Clear MsgBox "结果集为空!","警告" Exit Sub End If With myFlexGrid '把标题写上 .Row = 0 .TextMatrix(.Row,0) = "学号" .TextMatrix(.Row,1) = "姓名" .TextMatrix(.Row,2) = "卡号" .TextMatrix(.Row,3) = "余额" .TextMatrix(.Row,4) = "学院" .TextMatrix(.Row,5) = "年级" .TextMatrix(.Row,6) = "班级" .TextMatrix(.Row,7) = "性别" .TextMatrix(.Row,8) = "状态" .TextMatrix(.Row,9) = "备注" End With myFlexGrid.Rows = mrc.RecordCount + 1 '设置大小 With myFlexGrid '对查询到的结果进行遍历,显示出来 .Row = 0 While mrc.EOF = False .Row = .Row + 1 .TextMatrix(.Row,0) = " " & mrc.Fields(1) .TextMatrix(.Row,1) = " " & mrc.Fields(2) .TextMatrix(.Row,2) = " " & mrc.Fields(0) .TextMatrix(.Row,3) = " " & mrc.Fields(7) .TextMatrix(.Row,4) = " " & mrc.Fields(4) .TextMatrix(.Row,5) = " " & mrc.Fields(5) .TextMatrix(.Row,6) = " " & mrc.Fields(6) .TextMatrix(.Row,7) = " " & mrc.Fields(3) .TextMatrix(.Row,8) = " " & mrc.Fields(11) .TextMatrix(.Row,9) = " " & mrc.Fields(8) mrc.MoveNext Wend End With End Sub Private Sub Form_Load() cboField1.AddItem "卡号" cboField1.AddItem "学号" cboField1.AddItem "姓名" cboField1.AddItem "性别" cboField1.AddItem "学院" cboField1.AddItem "专业" cboField1.AddItem "年级" cboField1.AddItem "班级" cboField2.AddItem "卡号" cboField2.AddItem "学号" cboField2.AddItem "姓名" cboField2.AddItem "性别" cboField2.AddItem "学院" cboField2.AddItem "专业" cboField2.AddItem "年级" cboField2.AddItem "班级" cboField3.AddItem "卡号" cboField3.AddItem "学号" cboField3.AddItem "姓名" cboField3.AddItem "性别" cboField3.AddItem "学院" cboField3.AddItem "专业" cboField3.AddItem "年级" cboField3.AddItem "班级" cboSign1.AddItem "=" cboSign1.AddItem "<" cboSign1.AddItem ">" cboSign1.AddItem "<>" cboSign2.AddItem "=" cboSign2.AddItem "<" cboSign2.AddItem ">" cboSign2.AddItem "<>" cboSign3.AddItem "=" cboSign3.AddItem "<" cboSign3.AddItem ">" cboSign3.AddItem "<>" cboMode1.AddItem "与" cboMode1.AddItem "或" cboMode2.AddItem "与" cboMode2.AddItem "或" End Sub
在编写这个窗体的时候,刚开始总是无法查询,各种报错,后来,看了同学的博客,利用tag将文字语言转换为sql语言,直接写入查询语句,这就方便了很多。之后,在查询的时候,刚开始想直接在窗体中显示所有查询语句,但是,在执行的时候,往往因为第二条空着,而第三条能写入了查询条件而出错。各种限制方法非常麻烦,我就直接限制了窗体显示的语句条数,算是个“懒办法”吧!
这个窗体还真是用了不少时间,刚刚开始是因为不知道如何写查询语句,使用了tag之后,又出现了查询时遍历错误,总之,麻烦还是挺多的。不过,解决了一个窗体,其他的组合查询都差不多了。
我的总结,希望大家帮忙找出缺点,改正后一起进步。下一篇VB写入数据库。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。