VB中模糊查询

前端之家收集整理的这篇文章主要介绍了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.Recordset
    Dim txtsql As String
    Dim MsgText As String
    If Not testtxt(comboExamtype.Text) Then    '输入框中不能为空
         MsgBox "请输入考试编号!",vbOKOnly + vbExclamation,"警告"
         Exit Sub
    End If
    If Not testtxt(comboClassno.Text) Then
           MsgBox "请选择班号!","警告"
           Exit Sub
    End If
    If Not testtxt(comboSID.Text) Then
         MsgBox "请选择学号!","警告"
         Exit Sub
    End If
    If Not testtxt(comboCourse.Text) Then
         MsgBox "请选择课程!","警告"
         Exit Sub
    End If
    If Not testtxt(txtResult.Text) Then
        MsgBox "请输入分数!","警告"
        Exit Sub
    End If
    
    If Not IsNumeric(txtResult.Text) Then
          MsgBox "分数请输入数字!","警告"
          Exit Sub
    End 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 Then
        MsgBox "有相同记录,请重新输入信息!","警告"
        mrc.Close
        '添加
    Else
        mrc.AddNew
        mrc.Fields(0) = comboExamtype.Text
        mrc.Fields(1) = comboSID.Text
        mrc.Fields(2) = txtName.Text
        mrc.Fields(3) = comboClassno.Text
        mrc.Fields(4) = comboCourse.Text
        mrc.Fields(5) = txtResult.Text
        mrc.Update
        'mrc.Close
        MsgBox "添加成绩成功!","警告"
        Unload Me
    End If
    
End Sub

Private Sub comboSID_Change()
        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,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
        
End Sub

Private Sub comboSID_Click()
        Dim mrc As ADODB.Recordset
        Dim txtsql As String
        Dim MsgText As String

        List1.Visible = False
'自动显示姓名
        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
End Sub

Private Sub Form_Load()
       Dim mrc As ADODB.Recordset
       Dim mrcc As ADODB.Recordset
       Dim txtsql As String
       Dim MsgText As String
       List1.Visible = False
       flagselect = False
       txtsql = "select * from student_info  "
       Set mrc = Executesql(txtsql,MsgText)
       
       While (mrc.EOF = False)
           comboClassno.AddItem mrc!class_no
           comboSID.AddItem mrc!student_id
           mrc.MoveNext
       Wend
       'mrc.Close
       
       txtsql = "select*from course_info"
       Set mrcc = Executesql(txtsql,MsgText)
    
       While (mrcc.EOF = False)
           comboCourse.AddItem mrcc!course_name
           mrcc.MoveNext
       Wend
       'mrcc.Close
       
       comboExamtype.AddItem "2000期中"
       comboExamtype.AddItem "2000期末"
       comboExamtype.AddItem "2001期中"
       comboExamtype.AddItem "2001期末"
       
End Sub

Private Sub List1_Click()
comboSID.Text = List1.Text
List1.Visible = False
End Sub
结束语

每一次回顾都会有新的收获,每次的讨论都会碰撞出新的想法。通过不断的完善程序代码,让自己不断的成长。
原文链接:https://www.f2er.com/vb/256075.html

猜你在找的VB相关文章