Access: Timestamp new records

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
2,338
Office Version
  1. 365
Platform
  1. Windows
I have a table where I put a field in named "_TIME_STAMP"
Whenever a new record is entered into the table I want to capture the date and time in this field

Note the user is entering data into the table via a Form (Splitform).

What is the best way to do this?


Accesstbl_VendorQuoteData - Table
frm_VendorQuoteData - Form
 
Last edited:

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
I would try using a default value in the table. It is also possible to do it with a before save event on the FORM. But the former doesn't require any code and always works - you would just leave the field out of the form (or leave it hidden in the form if you so desire). The value then gets populated automatically when the record is inserted.
 
Upvote 0
I would say the same as xenou re table default. Use Now() for the default.
However I found when I wanted to do that, I also wanted to know who created it and when it was last amended and who amended it.
Something to consider perhaps?
 
Upvote 0
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
    Dim sUserName As String

    UserNameID = (Environ$("Username"))

  If Me.NewRecord = True Then
             Me![_TIME_STAMP] = Date
             Me![_USERIDSTAMP] = UserNameID
         Else
             Me![_TIME_STAMP] = Date
             Me![_USERIDSTAMP] = UserNameID
         End If
         
End Sub
 
Upvote 0
Hi,
Note that since we are doing the same update for both new records (inserts) and non-new records (updates) the above is the same as:
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
    Dim sUserName As String

    UserNameID = (Environ$("Username"))

	 Me![_TIME_STAMP] = Date
	 Me![_USERIDSTAMP] = UserNameID
         
End Sub

If you want to have some difference between inserts and updates, you can have two fields, one for CreatedDate and another for LastModifiedDate. Both can be handled with form code or table defaults. The former should, of course, never change, while the latter would, in such a setup, always show the timestamp of the last change (but not really a history of changes over time, so of limited but not necessarily inconsequential use).
 
Upvote 0
Were you perhaps trying to do something like this?
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.NewRecord Then
    Me.CreatedDate = Now()
    Me.CreatedBy = Environ("username")
Else
    Me.AmendedDate = Now()
    Me.AmendedBy = Environ("username")
End If
End Sub

For that I have separate fields on the table.
 
Upvote 0

Forum statistics

Threads
1,214,593
Messages
6,120,434
Members
448,961
Latest member
nzskater

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top