VBA Timing Question...

dsfcom

Registered User.
Local time
Today, 18:47
Joined
Mar 13, 2007
Messages
72
I'm sure to get some flak about this, but I have a question concerning timing (or code processing). Say there's a form with an "OnTimer" event running the following code:

Code:
Private Sub Form_Timer()
Me.Form.TimerInterval = 0
Call GetPrevAccessOptions
Call SetAccessOptions
Call YetAnotherFunction
DoCmd.Close acForm, "frmAccessOptions_Setter_2", acSaveNo
End Sub

Does the db at run-time then set the form's timer interval to zero, then run the code in 'GetPrevAccessOptions', then run the code in 'SetAccessOptions', then run the code in 'YetAnotherFunction', then close the form in sequence waiting for each line to process or does it run through each line trying to run the three "Called" functions simultaneously?
 
No.

When the timer fires the first time (based on property settings), those things might occur. It doesn't make sense to set the timer to 0 and then close the form shortly thereafter.

This is very dangerous syntax since the timer might start multiple times.

I don't use timers anymore, but try this syntax (from the top of my head, based on your code...you test and troubleshoot):

Private Sub Form_Timer()
static i as integer
if i = 1 then
exit sub
else
i = 1
'Me.Form.TimerInterval = 0 'Makes no sense, you're about to close the form containing the timer.
Call GetPrevAccessOptions
doevents
Call SetAccessOptions
doevents
Call YetAnotherFunction
doevents
DoCmd.Close acForm, "frmAccessOptions_Setter_2", acSaveNo 'Not sure if this will work. Try it and see.
'exit sub 'Maybe...might re-open the form. Try it and see.
end if
i = 0 'Maybe...might re-open the form. Try it and see.
End Sub
 
I'm using the timer because it's the whole purpose of this form. On open the interval is set to 5000ms (5 seconds or so). I suppose theres no need to set back to zero since the form is going to close anyway but is the key thing here the addition of "DoEvents"? I've never used it before. I'll read up on that one. Thanks for your inputs!
 
PMFJI but the syntax for referencing the TimerInterval is: Me.TimerInterval = 0 I believe your syntax should throw a syntax error since it is the equivalent to Form.Form.TimerInterval or Me.Me.TimerInterval. BTW that should disable any further Timer Interrupts. Access is single threaded and unless you are doing something strange in the called subs, they will complete in sequence.
 
Last edited:
So should I still set the timer back to zero or just let the form close and not worry about it? The Me.Form.TimerInterval works fine but I do see now that the extra word may simply be a waste of time. Not sure how it effects the execution.
 
I would turn off the interrupts. Is there a reason you are using the Timer event rather than just using the OnLoad event?
 
Yes there is a reason for the timer. There are a couple of forms that open on db startup and one of them I need to run some actions and then close iteself after completion but not until a few seconds after the db runs some startup procedures. If I used the OnLoad event it would run the actions immediately and I wouldn't get the benefit of the time delay. Am I doing wrong?
 
I suppose theres no need to set back to zero since the form is going to close anyway but is the key thing here the addition of "DoEvents

No, the key thing is the addition of a static variable to eliminate re-entry.
 
How do you get this form loaded? The method you are using seems a little prone to some timing issues.
 
The AutoExec macro runs code in a module that opens the form.
 
Yes there is a reason for the timer. There are a couple of forms that open on db startup and one of them I need to run some actions and then close iteself after completion but not until a few seconds after the db runs some startup procedures. If I used the OnLoad event it would run the actions immediately and I wouldn't get the benefit of the time delay. Am I doing wrong?
Is everything working as you want? If so then I guess it ain't broke so we will stop trying to fix it.
 

Users who are viewing this thread

Back
Top Bottom