This will give you access to the Windows Logon name
a) Create a MODULE with the following
Option Explicit
Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Public Function GetUserName() As String
' Returns the network login name
Dim lngLen As Long, lngX As Long
Dim strUserName As String
strUserName = String$(254, 0)
lngLen = 255
lngX = apiGetUserName(strUserName, lngLen)
If (lngX > 0) Then
GetUserName = Left$(strUserName, lngLen - 1)
Else
GetUserName = vbNullString
End If
End Function
' -==== End of Module ===
b) in table into which you are adding rows, have a column
CreatedBy type text length as needed
c) in any form that can create (insert) a record
1) Have field bound to CreatedBy - it may/should be VISIBLE = FALSE
2) code as follows:
Private Sub Form_BeforeInsert(Cancel As Integer)
Me.CreatedBy = GetUserName() & " " & Now()
End Sub
' --------
Big Brother option
a) Instead of column CreatedBy, how about "ChangeLog" - type MEMO
b) Private Sub Form_BeforeInsert(Cancel As Integer)
Me.ChangeLog = Now() & " Created by: " & GetUserName()
End Sub
c) Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim str_Memo As String
If Not IsNull(Me.ChangeLog) Then
str_Memo = Chr$(13) & Chr$(10) & Me.ChangeLog
End If
Me.ChangeLog = Now() & " Changed by:" & GetUserName() & str_Memo
End Sub
' ---------
For Deluxe version, you could capture the "before" value of any field that
gets changed and store it as part of change log, so you can see what changes
were made by whom. To do that, grab values of interested column(s) in the
Form_Current() event, and compare and record differences in the Before_Update
event as above. This has proved valuable tracking down inconsistent data -
where the data passes validation but just "looks wrong".