Adding time on a form

A

Armele

Hi,

I would like a form to be closed if there is no activity after 5 minutes.
How do I code it?
 
D

Dale_Fye via AccessMonster.com

It depends on how you define no activity. Personally, I consider no activity
to mean that no information has been entered in the form, that the cursor has
not moved out of its original location, and that the mouse has not moved.

To do something like this you need to:

1. Set the forms KeyPreview property to Yes
2. Declare a DateTime variable at the form level (in the forms declaration
section)

Dim dtLastActivity as date

3. Put some code in the forms KeyDown and MouseMove events to reset
dtLastActivity

Private Sub Form_KeyDown

dtLastActivity = Now()

End sub

5. Set the Forms TimerInterval to 10000 (checks every 10 seconds) and then
add code to the forms Timer event to determine whether more than 5 minutes
has elapsed since dtLastActivity.

Private Sub Form_Timer

if datediff("s", dtLastActivity, Now()) > 300 then
me.undo
docmd.close acform, me.name
endif
End Sub

You could also through some code in the Timer event to write any changes that
have occurred to your data before closing the form. The problem with this is
that if you have made changes, and those changes do not pass your validation
checks, then you would also have to add some code whereever you are doing
those checks, to bypass those checks, or if the checks were failed, then undo
them.

Additionally, I mentioned that I would use the forms MouseMove event to reset
the dtLastActivity. If the controls on your form are too close together,
then the forms MouseMove event may not fire, in which case you might have to
use the MouseMove events that are associated with some of the controls on
your form as well.

HTH
 
A

Armele

Dale_Fye,

Thanks for the message. However, after following the steps, when I tried to
open the form, I get the following error:
The expression on timer you entered in the event property setting produced
the following error: User defined typed not declared.

Do I need to check off any thing in the reference library, if so, what?

Thanks
 
D

Dale_Fye via AccessMonster.com

My guess is that you did not put the declaration of dtLastActivity in the
forms declarations section.

When you open that forms code, go to the very top. You should see something
like:

Option Compare Database
Option Explicit

Does not have to include both of these but I always include the Option
Explicit line.

Right below that, declare the dtLastActivity variable as DateTime, like:

Private dtLastActivity as Date

Below that, you should see the code for your various form and control events.

Dale
Dale_Fye,

Thanks for the message. However, after following the steps, when I tried to
open the form, I get the following error:
The expression on timer you entered in the event property setting produced
the following error: User defined typed not declared.

Do I need to check off any thing in the reference library, if so, what?

Thanks
It depends on how you define no activity. Personally, I consider no activity
to mean that no information has been entered in the form, that the cursor has
[quoted text clipped - 48 lines]
 
A

Armele

I am still getting the same error.

Dale_Fye via AccessMonster.com said:
My guess is that you did not put the declaration of dtLastActivity in the
forms declarations section.

When you open that forms code, go to the very top. You should see something
like:

Option Compare Database
Option Explicit

Does not have to include both of these but I always include the Option
Explicit line.

Right below that, declare the dtLastActivity variable as DateTime, like:

Private dtLastActivity as Date

Below that, you should see the code for your various form and control events.

Dale
Dale_Fye,

Thanks for the message. However, after following the steps, when I tried to
open the form, I get the following error:
The expression on timer you entered in the event property setting produced
the following error: User defined typed not declared.

Do I need to check off any thing in the reference library, if so, what?

Thanks
It depends on how you define no activity. Personally, I consider no activity
to mean that no information has been entered in the form, that the cursor has
[quoted text clipped - 48 lines]
I would like a form to be closed if there is no activity after 5 minutes.
How do I code it?
 
D

Dale_Fye via AccessMonster.com

Another way to do this would be to initially set the TimerInterval to 0.
Then, in either the keydown or mousemove event, check to see if it is zero
and set it to 10000 after setting the variable dtLastActivity.

Do you have a lot of code behind this form? If not, just copy all of the
code that relates to the form and post it here. If you do, then copy the
code from the Timer event and the forms_keydown and mousemove events.

Dale
I am still getting the same error.
My guess is that you did not put the declaration of dtLastActivity in the
forms declarations section.
[quoted text clipped - 32 lines]
 
Top