Link to home
Start Free TrialLog in
Avatar of keithedwardb
keithedwardbFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Form refresh fails when timer fires a function

Hi,
I am having a problem getting a form label to change when the forms timer function is active.
I have a form called 'frmAutoPoll'.  When the form opens, its label is green.  When the timer interval fires, the code should change the label colour to red and put a new caption in there, BEFORE going off to the called procedure AutoPoll (see attached code).  After the function has finished, the timer code (successfully) changes the form label back to green and modifies the caption.
The problem is that while the function AutoPoll fires and runs, the code to change the label to red is being ignored.  If I put a break point in at the procedure call to AutoPoll, the label changes to red correctly; likewise, if I put a message box requiring human interaction.  The idea is that this form will be active 24/7 unless it is stopped by the user.  The idea of the label colour change is just to alert someone where the system is in the cycle should they come and look.
Can anyone advise how I might achieve the label change before the procedure call fires?  I have tried putting the initial label change to red inside the called procedure without success, and I have also tried refresh and repaint.
Thank you.
Keith Blakesley
AutoPoll.txt
Avatar of Andrew_Webster
Andrew_Webster
Flag of United States of America image

I'd suggest a couple of things.

First, put DoEvents in before and after AutoPoll.

If that doesn't make a difference, then maybe create a procedure that pauses the code for you for a moment, and place that before AutoPoll.

I've had problems like this before - I've never quite understood what's causing them, but by giving things time to "catch up" I've fixed them.  Stopping the code execution with a break point or a messagebox is the clue, I've definitely seen problems like that - you pause it, it works, you don't it doesn'

That's not to say that one of the gurus here won't put their finger on it right away and tell you something different.
Avatar of keithedwardb

ASKER

Hi Andrew,
Thanks. is 'DoEvents' just that, or is it part of a longer syntax? I haven't come across that before.
Also, I used to program in spectrum basic (!) and could pause the code in that, but haven't a clue of the syntax in vba / vb.NET? Any clues there, please?
Thanks again.
Ok.

DoEvents is simply that.  It tells Access to take a breath so the operating system can process other events.  See http://office.microsoft.com/en-us/access/HA012288271033.aspx.

I've attached my own Pause routine.  There's one catch to it, it uses the "Timer" function.  This counts the number of milliseconds since midnight.  In that applications where I've been using it, it's never going to be run so that it crosses midnight.  If it did it could potentially run indefinitely!  

If you think there's any chance that your users could do something that would have it run over midnight, then let me know, and I'll spend a few moments thinking up a way to avoid that being a problem.
Option Compare Database
Option Explicit

Public Sub Pause(ByVal MillisecondsToWait As Long)
'---------------------------------------------------------------------------------------
' Procedure   : Pause
' DateTime    : 19/08/2008 14:29
' Author      : Andrew
' Purpose     : Pauses for a given number of milliseconds.  Don't expect it to be accurate.
'---------------------------------------------------------------------------------------
'

Dim sngNow As Single
Dim sngLater As Single

'Debug.Print Timer

sngNow = Timer
sngLater = sngNow + MillisecondsToWait / 1000
Do While Timer < sngLater
    DoEvents
Loop

'Debug.Print Timer

End Sub

Open in new window

Hi Andrew,
Thank you.  My access program will be polling 24/7, every 15 minutes in a 24-hour working environment.  Do you think that will be an issue.  If not I'll give your code a try.
Thanks.
Keith
Ok.  If I have a minute during the day I'll see if I can tweak the code.  Check back here later.
ASKER CERTIFIED SOLUTION
Avatar of Andrew_Webster
Andrew_Webster
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Andrew,
This looks good.  The issue is I am now on vacation and won't be able to try this for a couple of weeks.  Having said that the code looks fine to me so I will accept this as the answer.  If I have another question following the inclusion of the code I'll post back.  Otherwise thanks for your help.

Best Regards,
Keith Blakesley.