Time in Access 2007

  • Thread starter Thread starter JOE POLLOCK
  • Start date Start date
J

JOE POLLOCK

I have a form that records call records for our department. I would like to
have a field on the form that when the record loads, the current time
displays. I would also like to display a field on the form that would, at
some action of my own, display the time that the call ended. I would also
like to do this automatically, rather than making it user dependent. I have
tried several options, but nothing has worked. Any suggestions will be
appreciated.
 
Assuming the form includes two bound controls, CallStarts and CallEnds
bound to columns of date/time data type, if you are referring to when
creating a new record then the following in the BeforeInsert event
procedure of the form would insert the current date/time when the user
begins to enter data:

Me.CallStarts = Now()

To insert the current date/time when the user navigates to a new
record, but before they enter any data put the following in the form's
Current event procedure:

If Me.NewRecord Then
Me.CallStarts.DefaultValue = """" & Now() & """"
End If

Note that the DefaultValue property is a string expression regardless
of the underlying data type, which is why the value is wrapped in
literal quotes.

Or simply set the CallStarts field's DefaultValue property to Now() in
table design.

Putting the following in the forms AfterInsert event procedure will
store the date/time value when the new record is saved:

Me.CallEnds = Now()

A user might keep the form open at the new record after the call has
ended of course, in which case the value would not be entered until
the record is saved by closing the form, moving to another record or
otherwise explicitly saving the record. You could include a button to
be clicked when a call ends which saves the current record with
either:

RunCommand acCmdSaveRecord

or:

Me.Dirty = False

The end of call value would then be entered either when the button is
clicked, or if they omit to do so, when the user otherwise saves the
record.

If you want to display only the times in the controls, without the
date element, then format the controls accordingly. There is no such
thing in Access as a time value per se, only date/time values.

Ken Sheridan
Stafford, England
 
Assuming that you are talking about NEW records, then set the default for the
StartTime to Time or Now if you want the date and time.

You can use code to set the time or datetime.
Me.EndTime = Time

Or
Me.EndTime = Now() 'if you want date and time

Where you use the expression depends on what you want to do. You can use the
expression in the Click event of button or the after update event of a control
or the before update event of the form or ...

John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County
 
Back
Top