Time Stamp on a form

  • Thread starter Thread starter Tony Mc
  • Start date Start date
T

Tony Mc

I have a form that when I open it, I wish to timestamp
that event and also timestamp the close event and then
calculate the time interval and display all 3 values in
another form - is this possible?
 
Yes, this is possible using the form's onload and onclose events. Your two
forms need to include something like the following:

On form 1:

'''declarations section'''
Dim timeOpen As Date

Private Sub btnCancel_Click()

DoCmd.Close acForm, Me.name

End Sub

Private Sub btnOK_Click()

'on click statements...

End Sub

Private Sub Form_Close()

DoCmd.OpenForm FormName:="timerForm2", openargs:=CStr(timeOpen) & "|" &
CStr(Now)

End Sub

Private Sub Form_Load()

timeOpen = Now

End Sub

....and on form 2:

Private Sub btnCancel_Click()

DoCmd.Close acForm, Me.name

End Sub

Private Sub btnOK_Click()

'on click statements...

End Sub

Private Sub Form_Load()

Dim args As Variant

args = Split(openargs, "|")

labelResults.Caption = "Last form was opened at " & args(0) & " and was
closed at " & args(1) & vbCrLf & "Time elapsed was " & DateDiff("s", args(0),
args(1)) & " seconds."

End Sub

All that happens in form 1 is that when it's opened, it calls the Now
function to store the date and time in the globally scoped variable timeOpen.
When the form closes, it calls Now again and uses the openargs parameter of
the docmd.openform method to pass the two valus to form 2 as strings.

In form 2, the onload event gets the openargs, splits them into a simple
array, and calls datediff to work out the elapsed time (datediff works on
anything that looks like a date, even if it's not of the Date type). I've
done it in seconds, but you can obviously tailor this to your requirements.

Another approach would be to time how long form 1 is open by calling the
timer function when it's opened, calling it again when it closes, and
comparing the values.

Hope that's what you're after.

G
 
Thanks for the work, but I am an amatuer at this and
whilst I managed to make it basically work, I have managed
to reset my requirements - can you help????

I have two forms:

Form A - has a button to Open Form B
Form B - has a button to Close Form B and return to form A

What I am wanting to do is to:

1. Push the button on Form A and open Form B which then
allows me to enter data against a particular record and
then close Form B;
2. Timestamp when Form B is opened and hold that time so
that whenever that form is opened it will always disply
the original time opened;
3. Timestamp when the Form B button (Close Form) is pushed
and hold that data as per Item 2;
4. Calculate the time difference;

This is meant as a production timing system - so that when
an operator selects a particular shop order he clicks on
the Form A button which presents him with a screen (Form
B) which allows him to enter how many of the product he
has made and then close form B which gives me the time
that the manufcaturing operation took.

One other area of interest is, if he opens "Record 1" for
20 items and only makes 10 items, how do I get the system
to open a new window for the next time the record is
clicked on Form A??

Anyway - thanks for your excellent help.

Tony Mc
 
Okay, I'll take this point by point:

'1. Push the button on Form A and open Form B which then
'allows me to enter data against a particular record and
'then close Form B;

Opening one form from another is simple. Use the OpenForm method of the
DoCmd object in a button's onclick procedure as follows:

Private Sub BtnOpenFormB_Click()

DoCmd.OpenForm "formName", acNormal

'There are other arguments you can use, such as the OpenArgs argument - see
Access help for details.

'if you want Form A to close at this point, you can also add:

'DoCmd.Close acForm, Me.name

'...which will close the form whose module the line is in.

End Sub

You can enter data into a table from a form in many different ways - I
prefer to write my own functions to update data rather than let Access do it
normally. I won't go into that here, though.

'2. Timestamp when Form B is opened and hold that time so
'that whenever that form is opened it will always disply
'the original time opened;

When Form B opens, you can use the onLoad event to specify things it should
do in the Form_Load procedure inside the module for Form B. Form B has a
text box called txtOpened, and in the declarations section of Form B's module
(ie, right at the top under the Option Compare Database and Option Explicit
statements) declare a variably of global scope by adding this line:

dim openedTime as Date

The Form_Load() procedure should look like this:

Private Sub Form_Load()

openedTime = Time 'we put the time of opening into openedTime
txtOpened.Value = openedTime 'and display it on the form

End Sub

Time is an inbuilt function which gets the system time. That's your form
timestamped, and the time displayed on it.

'3. Timestamp when the Form B button (Close Form) is pushed
'and hold that data as per Item 2;

Well, since you're closing the form at this point, you can't hold that data
for very long. However, you can use the onClose event of Form B to...

'4. Calculate the time difference;

Form_Close()

Dim closedTime As Date 'declare a local variable for the time right now

closedTime = Time 'set that variable with the time right now

Dim elapsedTime As Date 'declare a local variable to hold the elapsed time

elapsedTime = openedTime - closedTime 'with date variables we can do a
simple calculation

MsgBox "Time elapsed: " & elapsedTime 'display the time elapsed with msgbox,
but you could easily pass this value to a form by calling a form with it in
openArgs, or writing it to a table

End Sub

Not quite sure what you mean about 20 items/10 items thing.

Hope that helps,

G

FULL CODE:

Option Compare Database
Option Explicit
Dim openedTime As Date

Private Sub Form_Close()

Dim closedTime As Date

closedTime = Time

Dim elapsedTime As Date

elapsedTime = openedTime - closedTime

MsgBox "Time elapsed: " & elapsedTime

End Sub

Private Sub Form_Load()

Me.InsideHeight = 5190
Me.InsideWidth = 8415

openedTime = Time
txtOpened.Value = openedTime

End Sub
 
Tony Mc said:
Thanks for the work, but I am an amatuer at this and
whilst I managed to make it basically work, I have managed
to reset my requirements - can you help????

I have two forms:

Form A - has a button to Open Form B
Form B - has a button to Close Form B and return to form A

What I am wanting to do is to:

1. Push the button on Form A and open Form B which then
allows me to enter data against a particular record and
then close Form B;
2. Timestamp when Form B is opened and hold that time so
that whenever that form is opened it will always disply
the original time opened;
3. Timestamp when the Form B button (Close Form) is pushed
and hold that data as per Item 2;
4. Calculate the time difference;

This is meant as a production timing system - so that when
an operator selects a particular shop order he clicks on
the Form A button which presents him with a screen (Form
B) which allows him to enter how many of the product he
has made and then close form B which gives me the time
that the manufcaturing operation took.

One other area of interest is, if he opens "Record 1" for
20 items and only makes 10 items, how do I get the system
to open a new window for the next time the record is
clicked on Form A??

Anyway - thanks for your excellent help.

Tony Mc
 
Back
Top