超好用的智能文本框通用函数-trynew
Access软件网QQ交流学习群(群号码198465573),欢迎您的加入!
首页 >技术文章> Access数据库-模块/函数/VBA


超好用的智能文本框通用函数

发表时间:2008/8/21 21:47:12 评论(3) 浏览(9556)  评论 | 加入收藏 | 复制
   
摘 要:为了庆祝奥运开幕,特推出最新特价的“超好用的智能文本框通用函数”例程。
正 文:

功能:
1、用法简单,任何文本框、组合框的“键释放”事件中使用此函数,即可带有智能辅助和选择录入此字段已有的记录中的文本。
2、极大加块了录入速度,比组合框快X倍。


代码:
Public Function GetFieldValue(Optional KeyCode As Integer = 1000)
'MsgBox KeyCode
On Error Resume Next
If KeyCode = 8 or KeyCode = 46 Then Exit Function
Dim i As Integer, txtSet As TextBox, intMax As Integer
Set txtSet = Screen.ActiveControl
i = txtSet.SelStart
If KeyCode = 40 Then
    With Screen.ActiveForm.RecordsetClone
    Do While Not .EOF()
        .MoveNext
        If Left(.Fields(txtSet.ControlSource), i) = Left(txtSet.Text, i) Then
            txtSet.Text = .Fields(txtSet.ControlSource)
            txtSet.Tag = txtSet.Text
            txtSet.SelStart = i
            txtSet.SelLength = Len(txtSet.Text)
            Exit Do
        End If
        intMax = intMax + 1
        If intMax > 10000 Then Exit Do
    Loop
    End With
    Exit Function
End If
If KeyCode = 38 Then
    With Screen.ActiveForm.RecordsetClone
    Do While Not .BOF()
        .MovePrevious
        If Left(.Fields(txtSet.ControlSource), i) = Left(txtSet.Text, i) Then
            txtSet.Text = .Fields(txtSet.ControlSource)
            txtSet.Tag = txtSet.Text
            txtSet.SelStart = i
            txtSet.SelLength = Len(txtSet.Text)
            Exit Do
        End If
        intMax = intMax + 1
        If intMax > 10000 Then Exit Do
    Loop
    End With
    Exit Function
End If
If txtSet.SelStart = Len(txtSet.Text) Then
    If Left(Nz(txtSet.Tag), i) = txtSet.Text Then
        txtSet.Text = txtSet.Tag
        txtSet.SelStart = i
        txtSet.SelLength = Len(txtSet.Text)
    Else
        With Screen.ActiveForm.RecordsetClone
        .MoveFirst
        Do While Not .EOF()
            If Left(.Fields(txtSet.ControlSource), i) = txtSet.Text Then
                txtSet.Text = .Fields(txtSet.ControlSource)
                txtSet.Tag = txtSet.Text
                txtSet.SelStart = i
                txtSet.SelLength = Len(txtSet.Text)
                Exit Do
End If
        intMax = intMax + 1
        If intMax > 10000 Then Exit Do
    Loop
    End With
    Exit Function
End If
If KeyCode = 38 Then
    With Screen.ActiveForm.RecordsetClone
    Do While Not .BOF()
        .MovePrevious
        If Left(.Fields(txtSet.ControlSource), i) = Left(txtSet.Text, i) Then
            txtSet.Text = .Fields(txtSet.ControlSource)
            txtSet.Tag = txtSet.Text
            txtSet.SelStart = i
            txtSet.SelLength = Len(txtSet.Text)
            Exit Do
        End If
        intMax = intMax + 1
        If intMax > 10000 Then Exit Do
    Loop
    End With
    Exit Function
End If
If txtSet.SelStart = Len(txtSet.Text) Then
    If Left(Nz(txtSet.Tag), i) = txtSet.Text Then
        txtSet.Text = txtSet.Tag
        txtSet.SelStart = i
        txtSet.SelLength = Len(txtSet.Text)
    Else
        With Screen.ActiveForm.RecordsetClone
        .MoveFirst
        Do While Not .EOF()
            If Left(.Fields(txtSet.ControlSource), i) = txtSet.Text Then
                txtSet.Text = .Fields(txtSet.ControlSource)
                txtSet.Tag = txtSet.Text
                txtSet.SelStart = i
                txtSet.SelLength = Len(txtSet.Text)
                Exit Do
            End If
           intMax = intMax + 1
            If intMax > 10000 Then Exit Do
            .MoveNext
        Loop
        End With
    End If
End If
End Function

用法:
在“键释放”事件中使用
Private Sub 姓名_KeyUp(KeyCode As Integer, Shift As Integer)
GetFieldValue KeyCode
End Sub

智能文本框:你可以在新记录的姓名字段中,输入“李”字,则字段中会自动出现反黑选定的“应”字,你可以用上下键更换此表已有姓名中的其它候选名字,也可以继续输入,或者回车(TAB)键转到其他字段。此函数有一个可选参数,放在“键释放”事件中,不带参数则屏蔽上下键选择功能。


Access软件网交流QQ群(群号:198465573)
 
 相关文章
查找access内指定对象是否存的通用函数  【叶海峰  2012/5/21】
【access源码示例】上传图片或附件到指定文件夹或共享文件夹的通...  【金宇  2012/6/1】
自定义年度、月份、年初、年末、月初、月末通用函数  【网行者  2012/6/6】
【转】下载网络文件的通用函数,适合客户端更新  【杏林求真  2012/12/26】
【Access小品】一语定乾坤--通用函数编制示例  【煮江品茶  2013/5/17】
常见问答
技术分类
相关资源
文章搜索
关于作者

trynew

文章分类

文章存档

友情链接