vba - 微软 Access : Action "onchange" event after a delay?

标签 vba ms-access

早上好!

我在 Microsoft Access 中有一个“奇特”的搜索功能,当您在搜索字段中键入内容时,可能的选项列表会缩小。不幸的是,计算机和服务器无法跟上这些快速的数据请求。

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

我目前的想法是……我知道一定有更好的东西。

“更改时,将搜索框值设置为 X 并等待 1 秒。1 秒后,如果 X = 搜索框值,则运行重新查询。问题是它将快速重写 X 值并有一个“等待” ' 命令针对每个字母 float 。

希望有一种方法可以编写“当字段 X 已更改但在过去一秒内未更改时”的事件触发器。

谢谢!

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

'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 上运行,由嗜睡症沙鼠提供动力。

最佳答案

您可以简单地使用当前表单的计时器。不需要单独的表格或任何东西。

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(),具体来说:

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

关于vba - 微软 Access : Action "onchange" event after a delay?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51599001/

相关文章:

ms-access - office 2003 和 2007 之间的 VBA 版本有区别吗?

sql-server - Sql Server ODBC 日期字段 - 未实现可选功能

excel - 为什么在 Outlook VBA 中调用 excel 函数需要更多时间?

python - 从 VBA 到 Flask 的 HTTP 请求的编码问题

excel - 将列转换为大写的动态范围

vba - 查找 "Cells.Find"函数的单元格引用

ms-access - MS Access VBA 临时高亮字段

ms-access - 我无法在 VBA Access 中使用 UBound() 函数。好像没有被识别

validation - 如何通过特定字段组合将表限制为 5 条记录

vba - Excel VBA 创建日期+时间值