-----Original Message-----
Access doesn't do this for you, but it's not difficult.
1. Add 4 new fields to your table to hold this information:
EnteredOn Date/Time for create date/time
EnteredBy Text for user who created
UpdatedOn Date/Time for modified date/time
UpdatedBy Text for user who modified.
If your table has an Inactive field (to mark the record inactive without
deleting), you also want:
InactiveOn Date/Time for deactivated date/time
InactiveBy Text for user who deactivated.
2. Select the module tab of the Database window, and click New. Paste the
code below into the module. Save.
3. In the Before Update event of the *form* (not a control), include this
line:
Call StampRecord(Me)
If you want it to handle the field named "Inactive" as well, use:
Call StampRecord(Me, True)
-----------code begins----------------
Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Function NetworkUserName() As String
' Returns the network login name
Dim lngLen As Long
Dim strUserName As String
strUserName = String$(254, 0)
lngLen = 255
If apiGetUserName(strUserName, lngLen) <> 0 Then
NetworkUserName = Left$(strUserName, lngLen - 1)
End If
End Function
Public Function StampRecord(frm As Form, _
Optional bHasInactive As Boolean = False) As Boolean
On Error GoTo Err_StampRecord
'Purpose: Stamp the user and date/time into the record.
'Return: True if successful.
'Argument: frm = the bound form to be stamped.
' bHasInactive= True if the form has Inactive field.
'Assumes: Fields named EnteredOn, EnteredBy, UpdatedOn, and UpdatedBy.
'Usage: In Form_BeforeUpdate:
' Call StampRecord(Me, True)
Dim strForm As String
Dim strUser As String
strForm = frm.Name 'For error handler.
strUser = NetworkUserName()
If frm.NewRecord Then
frm!EnteredOn = Now()
frm!EnteredBy = strUser
Else
frm!UpdatedOn = Now()
frm!UpdatedBy = strUser
End If
If bHasInactive Then
With frm!Inactive
If .Value = .OldValue Then
'do nothing
Else
If .Value Then
frm!InactiveOn = Now()
frm!InactiveBy = strUser
Else
frm!InactiveOn = Null
frm!InactiveBy = Null
End If
End If
End With
End If
Exit_StampRecord:
Exit Function
Err_StampRecord:
Call LogError(Err.Number, Err.Description, conMod & "StampRecord()",
"Form = " & strForm)
Resume Exit_StampRecord
End Function
-----------code ends------------------
Notes:
1. Replace the error handler with your own, or you can grab the error
logging function from:
http://allenbrowne.com/ser-23a.html
2. If you are using Access security, you can use the CurrentUser() function
to get the user name. If you are not using security, this code grabs the
user name from Windows.
--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Reply to group, rather than allenbrowne at mvps dot org.
.