Date/Time Now () not working the way I want it to

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Within my Form I have a subform in datasheet view that only has two fields
Date/Time and Comments/Status. I'd like to auto insert the current date &
time in the sub-form when someones adds an entry to the comments/status
field.

I've set the default value to now() in the sub-form (design view), however,
it automatically inserts the date and time for the next row instead of
keeping with the row where I type in new data.

Please help. Thanks.
 
cengel0327 said:
Within my Form I have a subform in datasheet view that only has two
fields Date/Time and Comments/Status. I'd like to auto insert the
current date & time in the sub-form when someones adds an entry to
the comments/status field.

I've set the default value to now() in the sub-form (design view),
however, it automatically inserts the date and time for the next row
instead of keeping with the row where I type in new data.

Please help. Thanks.

So the user is editing an existing record, and you want to save the
date/time of that edit in the [Date/Time] field of that record? For
that, you'll have to use the form's BeforeUpdate event. An event
procedure might look like this:

'----- start of example code -----
Private Sub Form_BeforeUpdate(Cancel As Integer)

Me![Date/Time] = Now()

End Sub
'----- end of example code -----

You should be aware that field names containing the "/" character are
not a very good idea, as they force you to enclose the field name in
brackets ([]) every time you use it.
 
Instead of using the DefaultValue property put code along these lines in the
subform's BeforeInsert event procedure:

Me.[Date/Time] = Now()

The current date/time value will be inserted into the field as soon as the
user begins to add data to a new record. If you set the date/time control's
locked property to True (Yes) and its Enabled property to False (No) the
field will look as normal but the user won't be able to change the value
inserted. In fact they won't even be able to move focus to the field.

Ken Sheridan
Stafford, England
 
That was exactly what I needed - thank you! Thanks for the tip on removing
the / - I will do that.



Dirk Goldgar said:
cengel0327 said:
Within my Form I have a subform in datasheet view that only has two
fields Date/Time and Comments/Status. I'd like to auto insert the
current date & time in the sub-form when someones adds an entry to
the comments/status field.

I've set the default value to now() in the sub-form (design view),
however, it automatically inserts the date and time for the next row
instead of keeping with the row where I type in new data.

Please help. Thanks.

So the user is editing an existing record, and you want to save the
date/time of that edit in the [Date/Time] field of that record? For
that, you'll have to use the form's BeforeUpdate event. An event
procedure might look like this:

'----- start of example code -----
Private Sub Form_BeforeUpdate(Cancel As Integer)

Me![Date/Time] = Now()

End Sub
'----- end of example code -----

You should be aware that field names containing the "/" character are
not a very good idea, as they force you to enclose the field name in
brackets ([]) every time you use it.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
Hi Ken,

I've been searching all the related posts for Date/Time functions in a form
and your example below was just what I was looking for. It works GREAT!!

Thanks!!
Paul

Ken Sheridan said:
Instead of using the DefaultValue property put code along these lines in the
subform's BeforeInsert event procedure:

Me.[Date/Time] = Now()

The current date/time value will be inserted into the field as soon as the
user begins to add data to a new record. If you set the date/time control's
locked property to True (Yes) and its Enabled property to False (No) the
field will look as normal but the user won't be able to change the value
inserted. In fact they won't even be able to move focus to the field.

Ken Sheridan
Stafford, England

cengel0327 said:
Within my Form I have a subform in datasheet view that only has two fields
Date/Time and Comments/Status. I'd like to auto insert the current date &
time in the sub-form when someones adds an entry to the comments/status
field.

I've set the default value to now() in the sub-form (design view), however,
it automatically inserts the date and time for the next row instead of
keeping with the row where I type in new data.

Please help. Thanks.
 
Back
Top