Constantly refreshing textbox from a module

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

The following code is supposed to run to log users out of the database every
night (always x amount of seconds after they logged in in the morning). I
used to run it on a form but everytime the form was used the countdown would
stop so i'm running it through a module instead, however the form doesn't
load properly with this code and when it does the txtExitCountDown is not
constantly being refreshed.

Public Function Exiter()

Dim OriginalTime As Long
Dim ExitTime As Long
OriginalTime = Timer
ExitTime = OriginalTime + 40000

Do Until Timer > ExitTime
[Forms]![frm_Main]![txtExitCountdown] = "(" & Round((ExitTime - Timer),
0) & ")"
DoEvents
Loop

DoCmd.Quit
End Function
 
I would strongly recommend using an invisible form that is open
all of the time with some Access timer code running.
Here are a number of suggestions:
http://home.bendbroadband.com/conradsystems/accessjunkie/kickoff.html

The following code is supposed to run to log users out of the database every
night (always x amount of seconds after they logged in in the morning). I
used to run it on a form but everytime the form was used the countdown would
stop so i'm running it through a module instead, however the form doesn't
load properly with this code and when it does the txtExitCountDown is not
constantly being refreshed.

Public Function Exiter()

Dim OriginalTime As Long
Dim ExitTime As Long
OriginalTime = Timer
ExitTime = OriginalTime + 40000

Do Until Timer > ExitTime
[Forms]![frm_Main]![txtExitCountdown] = "(" & Round((ExitTime - Timer),
0) & ")"
DoEvents
Loop

DoCmd.Quit
End Function

_______________________________________________
hth - RuralGuy (RG for short)
Please post to the NewsGroup so all may benefit.
 
I don't have access to most internet sites including that one. Using an
invisible form is a good idea, but how do I get the code to run so that it
doesn't use up all the CPU?
--
Kind Regards,
Adam Thwaites
Access Database Designer
Manchester, UK


RuralGuy said:
I would strongly recommend using an invisible form that is open
all of the time with some Access timer code running.
Here are a number of suggestions:
http://home.bendbroadband.com/conradsystems/accessjunkie/kickoff.html

The following code is supposed to run to log users out of the database every
night (always x amount of seconds after they logged in in the morning). I
used to run it on a form but everytime the form was used the countdown would
stop so i'm running it through a module instead, however the form doesn't
load properly with this code and when it does the txtExitCountDown is not
constantly being refreshed.

Public Function Exiter()

Dim OriginalTime As Long
Dim ExitTime As Long
OriginalTime = Timer
ExitTime = OriginalTime + 40000

Do Until Timer > ExitTime
[Forms]![frm_Main]![txtExitCountdown] = "(" & Round((ExitTime - Timer),
0) & ")"
DoEvents
Loop

DoCmd.Quit
End Function

_______________________________________________
hth - RuralGuy (RG for short)
Please post to the NewsGroup so all may benefit.
 
You don't need very tight resolution so set the Timer Interval to
60000 (every minute) or greater 600000 (every 10 minutes).

I don't have access to most internet sites including that one. Using an
invisible form is a good idea, but how do I get the code to run so that it
doesn't use up all the CPU?

_______________________________________________
hth - RuralGuy (RG for short)
Please post to the NewsGroup so all may benefit.
 
This is still using 100% of the CPU, even if I set the interval to a minute
it is still 100%

Public Function Exiter()

Dim OriginalTime As Long
Dim ExitTime As Long
OriginalTime = Timer
ExitTime = OriginalTime + 40000
TimerInterval = 6000

Do Until Timer > ExitTime
TimerInterval = 6000
lblExitCountdown.Caption = "(" & Round((ExitTime - Timer), 0) & ")"
DoEvents
Loop

DoCmd.Quit
End Function
 
Hi Adam,
Code that used Timer and loops on DoEvents *will* use
100% of the CPU. The Access timer does *not* work
this way. What URL's or domains can you view on
the Internet?


This is still using 100% of the CPU, even if I set the interval to a minute
it is still 100%

Public Function Exiter()

Dim OriginalTime As Long
Dim ExitTime As Long
OriginalTime = Timer
ExitTime = OriginalTime + 40000
TimerInterval = 6000

Do Until Timer > ExitTime
TimerInterval = 6000
lblExitCountdown.Caption = "(" & Round((ExitTime - Timer), 0) & ")"
DoEvents
Loop

DoCmd.Quit
End Function

_______________________________________________
hth - RuralGuy (RG for short)
Please post to the NewsGroup so all may benefit.
 
You don't need the Do ... Loop.

If you set the form's TimerInterval, its Timer event will fire whenever
that interval has past (assuming the form is open). So all you have to
do is put code in the Timer event to check whatever you want, e.g.

If (Now() - TimeUserLoggedIn) * 24 >= 10 Then
'it's 10 hours since they logged in
'force close
...
End If

One way of testing for inactivity is to have the Timer event
(a) log the name of the active form, name of the active control, and
value or text property of the active control.
(b) next time round, compare these with the currently active form,
control and value. If there's been no change, assume the user's not
using!
 
RuralGuy > Access to a white list sites of which microsoft.com is the only
programming one.

John> Cheers, I modified the code to this:

Sub Form_Timer()
Dim TimeUserLoggedIn As Date
Dim HoursUntilLogout As Integer
Dim MinutesUntilLogout As Integer

TimeUserLoggedIn = lblTimeLoggedIn.Caption
HoursUntilLogout = 10
MinutesUntilLogout = (HoursUntilLogout * 60)

If (Now() - TimeUserLoggedIn) * 1440 >= MinutesUntilLogout Then
DoCmd.Quit
Else
lblTimeTilLogout.Caption = ((MinutesUntilLogout - Round(((Now() -
TimeUserLoggedIn) * 1440), 0)) \ 60 Mod 60) _
& ":" & ((MinutesUntilLogout - Round(((Now() - TimeUserLoggedIn) *
1440), 0)) Mod 60)
End If

If lblTimeTilLogout.Caption <= "0:10" Then
lblTimeTilLogout.ForeColor = vbRed
Else
lblTimeTilLogout.ForeColor = vbBlack
End If
End Sub

Probably a bit long winded and messy but it works, unless I put a number
less than 1 for the hour, in which case it just rounds it up, or if its less
than 0.5 it exits the database straight away. Why is this?
 
Hi Adam,

It looks like John has you on the right track now. Just a couple of questions.
What value did you use for the TimerInterval? This is *not* your
actual Timer code is it? Your variables should be defined as Public
at the top of the code sheet and outside of the sub routine.

I'm drawing a blank. What is the 1440?

RuralGuy > Access to a white list sites of which microsoft.com is the only
programming one.

John> Cheers, I modified the code to this:

Sub Form_Timer()
Dim TimeUserLoggedIn As Date
Dim HoursUntilLogout As Integer
Dim MinutesUntilLogout As Integer

TimeUserLoggedIn = lblTimeLoggedIn.Caption
HoursUntilLogout = 10
MinutesUntilLogout = (HoursUntilLogout * 60)

If (Now() - TimeUserLoggedIn) * 1440 >= MinutesUntilLogout Then
DoCmd.Quit
Else
lblTimeTilLogout.Caption = ((MinutesUntilLogout - Round(((Now() -
TimeUserLoggedIn) * 1440), 0)) \ 60 Mod 60) _
& ":" & ((MinutesUntilLogout - Round(((Now() - TimeUserLoggedIn) *
1440), 0)) Mod 60)
End If

If lblTimeTilLogout.Caption <= "0:10" Then
lblTimeTilLogout.ForeColor = vbRed
Else
lblTimeTilLogout.ForeColor = vbBlack
End If
End Sub

Probably a bit long winded and messy but it works, unless I put a number
less than 1 for the hour, in which case it just rounds it up, or if its less
than 0.5 it exits the database straight away. Why is this?

_______________________________________________
hth - RuralGuy (RG for short)
Please post to the NewsGroup so all may benefit.
 
1440 is 24 * 60 which changes the time from days to hours to minutes, in
Johns example all his code ran in hours but as I wanted the time until the
system logs your out showing in the format "HH:MM" I had to change the code
to minutes.

My timer interval is 60000 (60 seconds) so you can see the minutes ticking
down.
I did think about having public variables instead of using the lable to hold
the login time but i'm used to storing values like that on my forms so I can
see them when i'm getting the code to work. All the code I pasted is running
straight from the timer code, yes.
 
Thanks for the update. Glad you got things rolling!

1440 is 24 * 60 which changes the time from days to hours to minutes, in
Johns example all his code ran in hours but as I wanted the time until the
system logs your out showing in the format "HH:MM" I had to change the code
to minutes.

My timer interval is 60000 (60 seconds) so you can see the minutes ticking
down.
I did think about having public variables instead of using the lable to hold
the login time but i'm used to storing values like that on my forms so I can
see them when i'm getting the code to work. All the code I pasted is running
straight from the timer code, yes.

_______________________________________________
hth - RuralGuy (RG for short)
Please post to the NewsGroup so all may benefit.
 
Back
Top