Text Input with time delay
-
21-12-2019 - |
Question
My scenario: I have built up a form with multiple input fields. One of the text input field has an underlying event listener, which catches up the "change" event.
Private Sub orderID_Change()
If Not IsNull(Me.orderID.Text) Then
Set Me.orders.Recordset = ordermanager.getOrder(Me.orderID.Text)
End If
End Sub
When the user types in an specific order number, the callback runs for each letter/sign the user has already typed into the text field.
My question: Is there a possible solution in msaccess, to create a time delay for a specific input mask?
For example: A user is typing an order number with some digits. When the first change event is fired, an internal timer function starts to decrement a counter. As long as now additional interaction happens with the user, the timer runs out of time and calls the underlying function. If in the meantime, additional input has made by the user, the counter should be reset.
For example: user types in "123"
SYSTEM : waiting for input
USER INPUT : 1
SYSTEM : timer 100ms
SYSTEM : timer 80ms
SYSTEM : timer 60ms
SYSTEM : timer 40ms
USER INPUT : 2
SYSTEM : timer 100ms
SYSTEM : timer 80ms
USER INPUT : 3
SYSTEM : timer 100ms
SYSTEM : timer 80ms
SYSTEM : timer 60ms
SYSTEM : timer 40ms
SYSTEM : timer 20ms
SYSTEM : timer 0ms
SYSTEM : Run Routine
Solution
Use the form Timer
event. Set TimerInterval
to zero at form load. Then, in the text box change event, reset TimerInterval
to your count-down timer value.
Note Me.orderID.Text
is a text property and will never be Null. Therefore check whether its length is greater than zero in orderID_Change
.
Private Sub Form_Load()
Me.TimerInterval = 0
End Sub
Private Sub Form_Timer()
MsgBox "Run routine now"
Me.TimerInterval = 0
End Sub
Private Sub orderID_Change()
'If Not IsNull(Me.orderID.Text) Then
If Len(Me.orderID.Text) > 0 Then
Me.TimerInterval = 1000 ' milliseconds
Else
Me.TimerInterval = 0
End If
End Sub
I tested that approach with a sample form in Access 2007 and I think it does what you requested. If you wanted to do something like this for more than one text box on the same form, this suggestion wouldn't cut it.
OTHER TIPS
I would suggest that you use the _AfterUpdate event instead - and just have the user press the enter key, or tab key when finished entering characters. That way it will only fire once. Trying to build a timer may not work either if the user pauses to look for something.
Or if the Order # is always the same length (i.e. 6), you could do:
Private Sub orderID_Change()
If Len(Me.orderID.Text) < 6 Then
'Debug.Print "len=" & Len(Me.orderID.Text)
Exit Sub
Else
'Debug.Print "Ready to set RS!!"
Set Me.orders.Recordset = ordermanager.getOrder(Me.orderID.Text)
End If
End Sub