Run timer event once only

  • Thread starter Thread starter groovershk
  • Start date Start date
G

groovershk

I have some code that I want to run 3 seconds after a form opens. I've
tried using the OnTimer events, but this will run the code every 3
seconds - I want it to run once only.

Can anybody help me out?
 
It is possible to disable the timer by setting the TimerInterval to zero, or
use a form-level boolean variable that you set to True once the code has
run. However, the Timer is best reserved for cases where you are absolutely
stuck, as it has such horrid side effects (especially when debugging.)

Can I suggest an alternative:

Private Sub Form_Load()
Dim dtEnd As Date
Static bExecuting As Boolean

If Not bExecuting Then
bExecuting = True
dtEnd = DateAdd("s", 3, Now())
Do While Now() < dtEnd
DoEvents
Loop
MsgBox "Time's up!"
bExecuting = False
End If
End Sub
 
Back
Top