User access security linking with table records

N

Nick

Hello All

I wonder if anyone can help?

I am building a multiple user database with user access security and I would
like to add some code so that every record added by each user has that user's
user name included in each record... if that makes any sense?

Does anyone know how to do this at all?

I would be willing to make a contribution for a complete solution.

Many thanks in advance and kind regards

Nick
 
R

Rob Parker

Hi Nick,

Assuming that your underlying table has a field named AddedBy, simply
include the following code in the BeforeInsert event of the form you are
using to add new records:
Me!AddedBy = CurrentUser

You can use similar code in the form's BeforeUpdate event to record the
username of the last person to edit the record, to a field named LastEdit:
Me!LastEdit = CurrentUser

HTH,

Rob
 
R

Roger Carlson

On my website (www.rogersaccesslibrary.com), is a small Access database
sample called "AuditTrail.mdb" which illustrates how to do this (plus a lot
more).

It uses an API call to get the network login name. Since you are using
Access security, you wouldn't need to use that. You could just use the
Username property of the workspace object. Something like:

DBEngine(0).UserName

--
--Roger Carlson
MS Access MVP
Access Database Samples: www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L
 
N

NKTower

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".
 
J

Jamie Collins

I am building a multiple user database with user access security and I would
like to add some code so that every record added by each user has that user's
user name included in each record

I would suggest that adding such metadata to every row in every table
will only lead to pain; what will happen when they want field-level
metadata...? Also consider that an audit trail is usual external by
design: how can you ensure bias will not be introduced into your
design by accident or otherwise? i.e. who will audit the audit tool?!

My suggestion is to consider porting the data to a more capable
engine, one that supports triggers (makes capturing changes much, much
easier), and purchasing an appropriate (e.g. SOX-compliant) tool for
the job rather than rolling your own.

Jamie.

--
 
Top