Auto fill date/time

sljensen

Registered User.
Local time
Yesterday, 22:18
Joined
Jun 5, 2012
Messages
23
Hi, New user here.
When a new record in created via a form I need a field to auto enter the current Date/time.Can someone point me int he right direction?
 
When the form is being complete you could use the Event for After Update and in the code screen you can simply place in a command to add the date/time which is called Now()

Open your form in design view and select a field which gets completed, then select to show the properties and then use the Event Tab in the After Update click the elipse button on the right and select Code View then type in

me.YourDateFieldName=Now()

Save and close the VBA screen and when you complete that field and move away it will add the date and time.
 
Trevor,
I's struggling.
When the VBA screen opens there are several lines of text. I assume I insert the code between Private and End as shown. I changed YourDateFieldName to my field name of Date_Last_Modified. It looks like this:

Private Sub Date_Last_Modified_AfterUpdate()
Me.Date_Last_Modified = Now()
End Sub

It doesn't autopopulate when I move away from the record. Do I have to select the date? A pop-up calendar does show adjacent to the box.

Where did I go wrong?
 
That should work I have tested this several ways.

Do you have any restrictions on the form? is it possible to attach a copy of the database stripping out any sensitive data?
 
You can also use the GotFocus() method. As you want it to autofill.. AfterUpdate will be invoked after you move away from the record after making some changes.. Use the same code but inside GotFocus().. something like..

Private Sub Text21_GotFocus()
Me.Text21 = Date
End Sub
 
I'm not aware that the form has any restrictions.

Lets see if this works:
I stripped down the database to just a few tables most of the linked tables were removed. Don't know if that will cause issues.

Thanks for helping.
 

Attachments

I must be doing something wrong. I tried GotFocus() and didn't get the date cell to autopopulate either.
 
With a little more effort, this can be made much more complicated. :D

You can in the properties of the field in table design say Now(), so each new record is born with a time stamp.

On your form, you can put the same in the default property of the control bound to that field, and that will do the same.
 
Spikepl,
Can you expand on this:

You can in the properties of the field in table design say Now(), so each new record is born with a time stamp.


See how green I am?

Thanks
 
Sorry, but this is very basic stuff. Someone here might help you, but whil you wait I would suggest you google for an access tutorial on tables. It will take you 10 minutes and you are done. And don't forget to look in the help file (press F1) there might be some "How to" too. Finally you have the Access application itself. There is no death penalty on scouring around and trying things out.

My main point is that you need to do some basic work yourself, and learn how to do it. Both Help and Google are sufficient for that. Come back with questions for things that are not immediately available in either Help nor Google.
 
Spikepl,
Can you expand on this:

You can in the properties of the field in table design say Now(), so each new record is born with a time stamp.


See how green I am?

Thanks

Though I agree with the gentleperson above, I will say look towards the option of default values for a field :P
 
When the form is being complete you could use the Event for After Update
NO NO NO!!! The Form's AfterUpdate event runs AFTER the record is saved. Updating the record at this point is like the old saying about closing the barn door after the horse has escaped. But this is worse since it puts Access into an infinite loop. Luckily, newer versions of Access will recover from this infinite recursion - record saved, you dirty it by changing a value, record saved, you dirty it, etc.

The correct event to use is the FORM's BeforeUpdate event. This is the last event that fires BEFORE the record is saved. It is the event you would use for final validation so you can cancel the save if there are any errors. It is also appropriate to use this event to timestamp the changes.

Although a default of Now() works for newly added records and so no code is required, there is no default if you want to stamp updates so you will need a line of code.

Me.LastUpdateDate = Now()

If I show this field on the form (which depends on the user's preference), I set it's locked property to Yes and TabStop to No. Since you are managing the timestamp in code, you don't want someone to modify the date and time manually.

In one of the earlier posts in the thread, you put the Me.LastUpdateDate = Now() into the AfterUpdate event of the LastUpdate control. Think about the logic of this (I know it all seems pretty random in the beginning but there really is logic behind how things work). If this event fires AFTER a field is updated, you would need to actually update the date in the control to fire the event and trigger the code. And since you want this field to be populated automatically, it wouldn't make sense to put the "automatic" code in this event.
 
I would just like to point out, I agree with Pat...

Also I have noted that post#3 from sljensen had this

Code:
Private Sub Date_Last_Modified_AfterUpdate()
Me.Date_Last_Modified = Now()
End Sub

Code will only run when you update the field you want to update.
This afterupdate should be on another field, or on a button
 
I have; with the great advice from Pat, have learned the disadvantages and the horror of updating a Form After Update. It is more swift when you DO NOT update the Form after updating the record.

As mentioned by Kev, the code will not be updating; as you have to make some changes to the record already existing for the control source after update to work. sljensen I would suggest you to follow Pat's advice.
 
hello friends i have a form on which i want to add a field "Time" and i want that it will automatically fill with current time when i got focus on "Time" field. somehow i am unable to do it i need your help friends...


Thanks in advance.........
 

Users who are viewing this thread

Back
Top Bottom