Timer looping in vba (Access)

2019-06-02 23:15发布

问题:

I'm trying to configure a timer to run every 3 minutes in VBA, to loop through my Access database table and validate data. I'm stuck at the point of starting the timer. I wrote this mini script to test the timer:

Function JobNameValidate()
MsgBox ("Hello")
'callAgain.OnTimer
End Function

Function callAgain()
callAgain.TimerInterval = 300000
Forms("HiddenForm1").OnTimer
JobNameValidate
End Function

It loops fine, however it loops instantly, regardless of the TimerInterval put in. I couldn't find any helpful documentation about this online.

回答1:

You can set your form's OnTimer property to a string which starts with = followed by your function name and a pair of parentheses.

The units for TimerInterval are milliseconds. So 3 minutes is 180000 (3 * 60 * 1000).

Function callAgain()
    Forms("HiddenForm1").OnTimer = "=JobNameValidate()"
    Forms("HiddenForm1").TimerInterval = 180000
End Function

I'm not sure why you want to do this with VBA. You could set both those properties in the form's property sheet. But you can do it with VBA if needed.