Time Calculations in forms

J

JOE POLLOCK

I have two tables, one which records the information on each call that we
receive. I have a second table which stores the Call the Time Began, the Call
the Time Ended and the Contact ID corresponding to the same Contact ID in the
general information table. I have created a form based on a query of these
two tables which serves as the basis for a form that is used for data entry.

I would like to have the Call Time Began autopopulate with the current time
when the form is first opened. When the next record is selected or a new
record is requested I would like the Call Time Ended to autpopulate with the
current time that the record was saved or modified. The purpose in all of
this is to maintain a record of the length of each call.
 
K

Klatuu

Easiest way to do this would be to use the DefaultValue property of the text
box for call start time. For call end time, use this snippet of code to
populate the text box for new records only:

Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.NewRecord Then
Me.txtCallEnd = Now
End If
End Sub
 
J

Jack Leach

Assuming that the record will always be started when a call starts and always
be ended when a call ends may not be the best idea (there's most likely
real-world incidents that will prove this, such as if someone needs to use
the restroom and doesn't close the form).

For this reason I would recommend a command button that allows the user to
record the time... but if you are intent on using the record naviagtion as
your timer, you can do that as well.

The code is fairly simple:

Me.ControlForStartTime = Now()
Me.ControlForEndTime = Now()

If you use record navigation, to get the start time, but it in your
OnCurrent event, and check to see if you're on a new record:

Private Sub Form_Current
If Me.NewRec Then
Me.ControlForStartTime = Now()
End If
End Sub

and for the end time, use the Form's BeforeUpdate event:

Private Sub Form_BeforeUpdate(Cancel As Integer)
Me.ControlForEndTime = Now()
End Sub



As I mentioned though, I would most certainly utilize a command button.
Here's an example that will, on a new record, display a button that says
Start Call. When the button is pressed it will say End Call. If you are not
on a new record, the button will be disabled.


Private Sub Form_Current
If Me.NewRec Then
Me.YourButton.Enabled = True
Me.YourButton.Caption = "Start Call"
Else
Me.YourButton.Enabled = False
End Sub

Private Sub Form_BeforeUpdate(Cancel As Integer)
'make sure the user clicked the button off
If Nz(Me.ControlEndTime, 0) = 0 Then
MsgBox "Please End the Call"
Cancel = True
End If
End Sub

Private Sub YourButton_Click
If Me.YourButton.Caption = "Start Call" Then
Me.ControlStartTime = Now()
Me.YourButton.Caption = "End Call"
Else
Me.ControlEndTime = Now()
Me.YourButton.Caption = "Start Call"
End If
End Sub



anyway... you get the idea (hopefully)

gl!

--
Jack Leach
www.tristatemachine.com

"I haven't failed, I've found ten thousand ways that don't work."
-Thomas Edison (1847-1931)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top