VB中模糊查询

前言

学生系统已经做好有一段时间了,最近有小伙伴在这个阶段然后来寻求帮助,聊天的过程中有了新的需求和想法通过讨论实验终于将需求变成了实际的代码功能。

需求的主要内容:学生成绩输入的过程中,输入学生学号,下拉菜单中会出现包含输入的相关数字的选项。可以提高输入速度,方便成绩录入人员。

正文

首通过截图来看看效果。 首先是学号下拉框中的数据:

输入数字3后的显示: 主要代码入下:
Dim mrc As ADODB.Recordset Dim txtSQL As String Dim MsgText As String Dim tempstr As String '定义list控件的基本属性 List1.Clear List1.Left = comboSID.Left List1.Top = comboSID.Top + comboSID.Height List1.Width = comboSID.Width List1.Visible = True 'list控件中显示相关数据 tempstr = comboSID.Text For i = 0 To comboSID.ListCount - 1 If InStr(1, Left(comboSID.List(i), Len(tempstr)), tempstr) <> 0 Then List1.AddItem comboSID.List(i) Next i '自动显示姓名 txtSQL = "select*from student_info where student_id='" & comboSID.Text & "'" Set mrc = ExecuteSQL(txtSQL, MsgText) If mrc.RecordCount <> 0 Then txtName.Text = Trim(mrc.Fields(1)) End If

上边的代码主要是通过combox和list控件来实现的。

完整代码如下:
Dim flagselect As Boolean
Dim grade As String
Private Sub cmdcancel_Click()Unload Me
End Sub
Private Sub cmdOK_Click()Dim mrc As ADODB.RecordsetDim txtSQL As StringDim MsgText As StringIf Not testtxt(comboExamtype.Text) Then    '输入框中不能为空MsgBox "请输入考试编号!", vbOKOnly + vbExclamation, "警告"Exit SubEnd IfIf Not testtxt(comboClassno.Text) ThenMsgBox "请选择班号!", vbOKOnly + vbExclamation, "警告"Exit SubEnd IfIf Not testtxt(comboSID.Text) ThenMsgBox "请选择学号!", vbOKOnly + vbExclamation, "警告"Exit SubEnd IfIf Not testtxt(comboCourse.Text) ThenMsgBox "请选择课程!", vbOKOnly + vbExclamation, "警告"Exit SubEnd IfIf Not testtxt(txtResult.Text) ThenMsgBox "请输入分数!", vbOKOnly + vbExclamation, "警告"Exit SubEnd IfIf Not IsNumeric(txtResult.Text) ThenMsgBox "分数请输入数字!", vbOKOnly + vbExclamation, "警告"Exit SubEnd If'添加的信息不能重复txtSQL = "select*from result_info where exam_no='" & comboExamtype.Text & "'and student_id='" _& comboSID.Text & "'and course_name='" & comboCourse.Text & "'"Set mrc = ExecuteSQL(txtSQL, MsgText)If mrc.EOF = False ThenMsgBox "有相同记录,请重新输入信息!", vbOKOnly + vbExclamation, "警告"mrc.Close'添加Elsemrc.AddNewmrc.Fields(0) = comboExamtype.Textmrc.Fields(1) = comboSID.Textmrc.Fields(2) = txtName.Textmrc.Fields(3) = comboClassno.Textmrc.Fields(4) = comboCourse.Textmrc.Fields(5) = txtResult.Textmrc.Update'mrc.CloseMsgBox "添加成绩成功!", vbOKOnly + vbExclamation, "警告"Unload MeEnd IfEnd SubPrivate Sub comboSID_Change()Dim mrc As ADODB.RecordsetDim txtSQL As StringDim MsgText As StringDim tempstr As String'定义list控件的基本属性List1.ClearList1.Left = comboSID.LeftList1.Top = comboSID.Top + comboSID.HeightList1.Width = comboSID.WidthList1.Visible = True
'list控件中显示相关数据tempstr = comboSID.TextFor i = 0 To comboSID.ListCount - 1If InStr(1, Left(comboSID.List(i), Len(tempstr)), tempstr) <> 0 Then List1.AddItem comboSID.List(i)Next i'自动显示姓名txtSQL = "select*from student_info where student_id='" & comboSID.Text & "'"Set mrc = ExecuteSQL(txtSQL, MsgText)If mrc.RecordCount <> 0 ThentxtName.Text = Trim(mrc.Fields(1))End IfEnd SubPrivate Sub comboSID_Click()Dim mrc As ADODB.RecordsetDim txtSQL As StringDim MsgText As StringList1.Visible = False
'自动显示姓名txtSQL = "select*from student_info where student_id='" & comboSID.Text & "'"Set mrc = ExecuteSQL(txtSQL, MsgText)If mrc.RecordCount <> 0 ThentxtName.Text = Trim(mrc.Fields(1))End If
End SubPrivate Sub Form_Load()Dim mrc As ADODB.RecordsetDim mrcc As ADODB.RecordsetDim txtSQL As StringDim MsgText As StringList1.Visible = Falseflagselect = FalsetxtSQL = "select * from student_info  "Set mrc = ExecuteSQL(txtSQL, MsgText)While (mrc.EOF = False)comboClassno.AddItem mrc!class_nocomboSID.AddItem mrc!student_idmrc.MoveNextWend'mrc.ClosetxtSQL = "select*from course_info"Set mrcc = ExecuteSQL(txtSQL, MsgText)While (mrcc.EOF = False)comboCourse.AddItem mrcc!course_namemrcc.MoveNextWend'mrcc.ClosecomboExamtype.AddItem "2000期中"comboExamtype.AddItem "2000期末"comboExamtype.AddItem "2001期中"comboExamtype.AddItem "2001期末"End SubPrivate Sub List1_Click()
comboSID.Text = List1.Text
List1.Visible = False
End Sub        

结束语
每一次回顾都会有新的收获,每次的讨论都会碰撞出新的想法。通过不断的完善程序代码,让自己不断的成长。


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部