MS Access:操作“改变".延误后的事件? [英] MS Access: Action "onchange" event after a delay?

查看:59
本文介绍了MS Access:操作“改变".延误后的事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好!

我在Microsoft Access中具有精美"搜索功能,其中在您在搜索字段中键入内容时,可能的选项列表会缩小.不幸的是,计算机和服务器无法跟上这些快速的数据查询.

I have a "fancy" search function in Microsoft Access where the list of possible options shrinks as you type in the search field. Unfortunately the computer and server can't keep up with these rapid requeries of the data.

当前,该命令用于使用搜索框的"onchange"功能中的字段重新查询.我想添加一个延迟,以便仅在搜索框未更改一秒钟时才运行重新查询.因此,如果有人输入8个字母的单词,则不会执行8个查询.

Currently the command to requery with the field in the 'onchange' function of the search box. I'd like to add a delay so it only runs the requery when the search box has not changed for a second. Thus if someone types in a 8 letter word, it isn't running 8 requeries.

我目前的想法是,我知道必须有更好的东西.

The current idea I have for it, which I know there must be something better, is..

在更改时,将搜索框值设置为X并等待1秒钟.1秒钟后,如果X =搜索框值,请运行重新查询.问题是它将迅速重写X值并有一个'wait '为每个字母浮动的命令.

"On change, set search box value to X and wait 1 second. After 1 second, if X = search box value, run the requery. An issue is that it would be rapidly rewriting the X value and have a 'wait' command floating for each letter.

希望有一种方法可以编写一个事件触发器字段X已更改,但在过去一秒内未更改."

Hopefully there's a way to write an event trigger of "When field X has changed, but not changed for the past second."

谢谢!

根据要求,这是我当前的代码

As requested, here is my current code

'Create a string (text) variable
    Dim vSearchString As String
'Populate the string variable with the text entered in the Text Box SearchFor
    vSearchString = SearchFor.Text
'Pass the value contained in the string variable to the hidden text box SrchText,
'that is used as the sear4ch criteria for the Query QRY_SearchAll
    SrchText = vSearchString
'Requery the List Box to show the latest results for the text entered in Text Box SearchFor
    Me.SearchResults.Requery
    Me.SearchResults2.Requery
'Tests for a trailing space and exits the sub routine at this point
'so as to preserve the trailing space, which would be lost if focus was shifted from Text Box SearchFor
    If Len(Me.SrchText) <> 0 And InStr(Len(SrchText), SrchText, " ", vbTextCompare) Then
        'Set the focus on the first item in the list box
            Me.SearchResults = Me.SearchResults.ItemData(1)
            Me.SearchResults.SetFocus
        'Requery the form to refresh the content of any unbound text box that might be feeding off the record source of  the List Box
            DoCmd.Requery
        'Returns the cursor to the the end of the text in Text Box SearchFor,
        'and restores trailing space lost when focus is shifted to the list box
            Me.SearchFor = vSearchString
            Me.SearchFor.SetFocus
            Me.SearchFor.SelStart = Me.SearchFor.SelLength
            Exit Sub
    End If
'Set the focus on the first item in the list box
'    Me.SearchResults = Me.SearchResults.ItemData(1)
    Me.SearchResults.SetFocus    
'Requery the form to refresh the content of any unbound text box that might be feeding off the record source of  the List Box
    DoCmd.Requery
'Returns the cursor to the the end of the text in Text Box SearchFor
    Me.SearchFor.SetFocus
    If Not IsNull(Len(Me.SearchFor)) Then
        Me.SearchFor.SelStart = Len(Me.SearchFor)
    End If

很明显,这不是我的代码,它来自互联网上的某个地方.对于本地存储的数据库,它的工作效果非常好,但是一切都转移到了我们的Sharepoint服务器上,该服务器运行在由麻醉性沙鼠驱动的发霉的地下室中的386上.

Obviously this is not MY code, it's from somewhere on the interweb. It works fantastic for databases stored locally, but everything is moving to our Sharepoint server which is running on a 386 in a moldy basement powered by a narcoleptic gerbil.

推荐答案

您可以简单地使用当前表单的计时器.无需单独的表格或其他任何内容.

You can simply use the Timer of the current form. No need for a separate form or anything.

Private Sub DoSearch()

    ' Your current code
    ' but you should look into removing as many "Requery" from there as possible!

End Sub

Private Sub SearchFor_Change()

    ' Wait for x Milliseconds until the search is started.
    ' Each new change restarts the timer interval.
    ' Use 1000 (1 s) for slow typists or a really slow server
    ' 200 ms feels right for a normal typist
    Me.TimerInterval = 200

End Sub

Private Sub Form_Timer()

    ' Disable timer (will be enabled by the next SearchFor_Change)
    Me.TimerInterval = 0
    ' Now run the search
    DoSearch

End Sub

注意:您可能需要将某些光标处理代码从DoSearch()移到SearchFor_Change(),具体是:

Note: you may need to move some of the cursor-handling code from DoSearch() to SearchFor_Change(), specifically:

Me.SearchFor.SelStart = Len(Me.SearchFor)

这篇关于MS Access:操作“改变".延误后的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆