RE: Last Saved By
Hi there! This is how I record the user who modified a record using the Windows API function. (This uses the name of the person logged onto the computer, such as jdoe or jsmith.)
First, create two fields in your table:
LastUpdate - Date/Time
LastUpdateUser - Text
Then, add those fields to your form.
Next, create a function in a new module (name it something like modlGetUserName).
Type the following into the module (or copy/paste from here):
_____________________
Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, _
nSize As Long) As Long
Function GetCurrentUserName() As String
'Retrieve name of current user using Windows API
Dim strName As String
Dim lngChars As Long
Dim lngRet As Long
strName = Space(255)
lngChars = 255
lngRet = GetUserName(strName, lngChars - 1)
If lngChars > 0 Then
GetCurrentUserName = Left(strName, lngChars)
Else
GetCurrentUserName = "Unable to retrieve name."
End If
End Function
________________________
Now, code the BeforeUpdate event of your form like this:
______________________
Private Sub Form_BeforeUpdate(Cancel As Integer)
'When info is updated, date and user automatically inserted into fields
Me.LastUpdate = Now()
Me.LastUpdateUser = GetCurrentUserName() 'refer to modlGetUserName
End Sub
___________________________
This will work if you keep the field and function names exactly the same as I've typed them here. But, if you understand the basic concept, you can change them to fit your needs.
On my form, I made the LastUpdate and LastUpdateUser fields locked, and not enabled so that the user can't change the data, but can visually see who made the last changes.
(I modified this code from "Automating Microsoft Access with VBA" by Susan Sales Harkins and Mike Gunderloy, in the Business Solutions series. It's a great book!)
I hope this helps!
