分享

VB中模糊查询

 hdzgx 2019-12-08

前言

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

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

正文

首通过截图来看看效果。
首先是学号下拉框中的数据:
输入数字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控件来实现的。
完整代码如下:
  1. Dim flagselect As Boolean
  2. Dim grade As String
  3. Private Sub cmdcancel_Click()
  4. Unload Me
  5. End Sub
  6. Private Sub cmdOK_Click()
  7. Dim mrc As ADODB.Recordset
  8. Dim txtSQL As String
  9. Dim MsgText As String
  10. If Not testtxt(comboExamtype.Text) Then '输入框中不能为空
  11. MsgBox "请输入考试编号!", vbOKOnly + vbExclamation, "警告"
  12. Exit Sub
  13. End If
  14. If Not testtxt(comboClassno.Text) Then
  15. MsgBox "请选择班号!", vbOKOnly + vbExclamation, "警告"
  16. Exit Sub
  17. End If
  18. If Not testtxt(comboSID.Text) Then
  19. MsgBox "请选择学号!", vbOKOnly + vbExclamation, "警告"
  20. Exit Sub
  21. End If
  22. If Not testtxt(comboCourse.Text) Then
  23. MsgBox "请选择课程!", vbOKOnly + vbExclamation, "警告"
  24. Exit Sub
  25. End If
  26. If Not testtxt(txtResult.Text) Then
  27. MsgBox "请输入分数!", vbOKOnly + vbExclamation, "警告"
  28. Exit Sub
  29. End If
  30. If Not IsNumeric(txtResult.Text) Then
  31. MsgBox "分数请输入数字!", vbOKOnly + vbExclamation, "警告"
  32. Exit Sub
  33. End If
  34. '添加的信息不能重复
  35. txtSQL = "select*from result_info where exam_no='" & comboExamtype.Text & "'and student_id='" _
  36. & comboSID.Text & "'and course_name='" & comboCourse.Text & "'"
  37. Set mrc = ExecuteSQL(txtSQL, MsgText)
  38. If mrc.EOF = False Then
  39. MsgBox "有相同记录,请重新输入信息!", vbOKOnly + vbExclamation, "警告"
  40. mrc.Close
  41. '添加
  42. Else
  43. mrc.AddNew
  44. mrc.Fields(0) = comboExamtype.Text
  45. mrc.Fields(1) = comboSID.Text
  46. mrc.Fields(2) = txtName.Text
  47. mrc.Fields(3) = comboClassno.Text
  48. mrc.Fields(4) = comboCourse.Text
  49. mrc.Fields(5) = txtResult.Text
  50. mrc.Update
  51. 'mrc.Close
  52. MsgBox "添加成绩成功!", vbOKOnly + vbExclamation, "警告"
  53. Unload Me
  54. End If
  55. End Sub
  56. Private Sub comboSID_Change()
  57. Dim mrc As ADODB.Recordset
  58. Dim txtSQL As String
  59. Dim MsgText As String
  60. Dim tempstr As String
  61. '定义list控件的基本属性
  62. List1.Clear
  63. List1.Left = comboSID.Left
  64. List1.Top = comboSID.Top + comboSID.Height
  65. List1.Width = comboSID.Width
  66. List1.Visible = True
  67. 'list控件中显示相关数据
  68. tempstr = comboSID.Text
  69. For i = 0 To comboSID.ListCount - 1
  70. If InStr(1, Left(comboSID.List(i), Len(tempstr)), tempstr) <> 0 Then List1.AddItem comboSID.List(i)
  71. Next i
  72. '自动显示姓名
  73. txtSQL = "select*from student_info where student_id='" & comboSID.Text & "'"
  74. Set mrc = ExecuteSQL(txtSQL, MsgText)
  75. If mrc.RecordCount <> 0 Then
  76. txtName.Text = Trim(mrc.Fields(1))
  77. End If
  78. End Sub
  79. Private Sub comboSID_Click()
  80. Dim mrc As ADODB.Recordset
  81. Dim txtSQL As String
  82. Dim MsgText As String
  83. List1.Visible = False
  84. '自动显示姓名
  85. txtSQL = "select*from student_info where student_id='" & comboSID.Text & "'"
  86. Set mrc = ExecuteSQL(txtSQL, MsgText)
  87. If mrc.RecordCount <> 0 Then
  88. txtName.Text = Trim(mrc.Fields(1))
  89. End If
  90. End Sub
  91. Private Sub Form_Load()
  92. Dim mrc As ADODB.Recordset
  93. Dim mrcc As ADODB.Recordset
  94. Dim txtSQL As String
  95. Dim MsgText As String
  96. List1.Visible = False
  97. flagselect = False
  98. txtSQL = "select * from student_info "
  99. Set mrc = ExecuteSQL(txtSQL, MsgText)
  100. While (mrc.EOF = False)
  101. comboClassno.AddItem mrc!class_no
  102. comboSID.AddItem mrc!student_id
  103. mrc.MoveNext
  104. Wend
  105. 'mrc.Close
  106. txtSQL = "select*from course_info"
  107. Set mrcc = ExecuteSQL(txtSQL, MsgText)
  108. While (mrcc.EOF = False)
  109. comboCourse.AddItem mrcc!course_name
  110. mrcc.MoveNext
  111. Wend
  112. 'mrcc.Close
  113. comboExamtype.AddItem "2000期中"
  114. comboExamtype.AddItem "2000期末"
  115. comboExamtype.AddItem "2001期中"
  116. comboExamtype.AddItem "2001期末"
  117. End Sub
  118. Private Sub List1_Click()
  119. comboSID.Text = List1.Text
  120. List1.Visible = False
  121. End Sub
结束语

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


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多