timestamp when a button is clicked (1 Viewer)

umair434

Registered User.
Local time
Today, 15:26
Joined
Jul 8, 2011
Messages
186
Hi,

So I have a form set up which has a button.. the onclick event of this button runs some code and produces a report.. This usually takes 2-3 mins.

I am actually looking for a way that if it has been less than 20 mins since the user clicked that button, a macro is skipped (no need since data wont be updated)..i.e a line of code is skipped.


something alone the line:

If datediff("mi", timestamp, now()) < 20 then..


^ something's wrong here .. any suggestions please?:confused:
 

rctjoe24

Registered User.
Local time
Today, 18:26
Joined
Jul 9, 2012
Messages
28
I think you're looking for,

If datediff("n", timestamp, now()) < 20 then
From the Access Help File, these are the Time Intervals;
yyyy Year
q Quarter
m Month
y Day of year
d Day
w Weekday
ww Week
h Hour
n Minute
s Second
 

umair434

Registered User.
Local time
Today, 15:26
Joined
Jul 8, 2011
Messages
186
but how do I find the time when the button is clicked?
 

rctjoe24

Registered User.
Local time
Today, 18:26
Joined
Jul 9, 2012
Messages
28
Easiest solution would be to declare a form level variable at the top of the form just after the Option Compare Database with something like;

Public mdtmTimeStamp as date

When you click the button that launches the report, prior to actually launching the report add the following code;

mdtmTimeStamp = now()

When the report finishes running, you can do a time difference like this

If DateDiff("n",mdtmTimeStamp,now()) > 20 then
.....
End IF

I chose to make the variable public in the form module because it is only set by this form and is only referenced by the one report. In some other cases it may be wiser to move the variable to a module which can be accessed by all Access object.
 

umair434

Registered User.
Local time
Today, 15:26
Joined
Jul 8, 2011
Messages
186
Easiest solution would be to declare a form level variable at the top of the form just after the Option Compare Database with something like;

Public mdtmTimeStamp as date

When you click the button that launches the report, prior to actually launching the report add the following code;

mdtmTimeStamp = now()

When the report finishes running, you can do a time difference like this

If DateDiff("n",mdtmTimeStamp,now()) > 20 then
.....
End IF

I chose to make the variable public in the form module because it is only set by this form and is only referenced by the one report. In some other cases it may be wiser to move the variable to a module which can be accessed by all Access object.


that's what I tried before but timestamp always equals now().. the difference always turns out to be 0. Somehow I need to find the time when the button is clicked..Now() just shows the true time.
 

rctjoe24

Registered User.
Local time
Today, 18:26
Joined
Jul 9, 2012
Messages
28
that's what I tried before but timestamp always equals now().. the difference always turns out to be 0. Somehow I need to find the time when the button is clicked..Now() just shows the true time.

Maybe I'm not explaining this well enough and for that I'm sorry.

I'm starting to think that the form which contains the button to open the report is closed after you click it. For that reason the variable would be set to nothing. In addition if you don't explicitly dimension your variables with "Option Explicit" at the top of each module, it would assume the variable as a date and default it to current date? :confused:

Anyway, the best course of action is to add the variable I was speaking of, mdtmTimeStamp, to a module. When the report is finished running, DateDiff function will be looking at a date different than the current date/time.

 

rctjoe24

Registered User.
Local time
Today, 18:26
Joined
Jul 9, 2012
Messages
28
that's what I tried before but timestamp always equals now().. the difference always turns out to be 0. Somehow I need to find the time when the button is clicked..Now() just shows the true time.


Also make sure you put the mdtmTimeStamp = now()
BEFORE you run docmd.openreport

It must be in this order or else you would get a 0 when you run the datediff.
 

umair434

Registered User.
Local time
Today, 15:26
Joined
Jul 8, 2011
Messages
186
Tried everything.. it still gives 0. I'm missing something here :(
 

rctjoe24

Registered User.
Local time
Today, 18:26
Joined
Jul 9, 2012
Messages
28
could you paste your code in this window so I could have a look?
 

Users who are viewing this thread

Top Bottom