Tracked user name using basAuditTrail module.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • suckerpunchnz
    New Member
    • Feb 2008
    • 2

    Tracked user name using basAuditTrail module.

    Hi there,

    I'm using this audit trail code (basAuditTrail module) to log changes in my database, but I'm needing it to track by the user name logged into Access not by the account logged into Windows. Does anybody know how to change this?

    Your help would be much appreciated.
    Thanks heaps.

    Phil.

    This is the code I'm using....it seems to be a common one found in forums.
    [CODE=VB]Private Sub Form_BeforeUpda te(Cancel As Integer)
    Call AuditTrail(Me, RecordNo) 'RecordNo is the primary key for each table
    End Sub[/CODE]
    In (module) basAuditTrail:
    [CODE=VB]Const cDQ As String = """"

    Sub AuditTrail(frm As Form, recordid As Control)
    'Track changes to data.
    'recordid identifies the pk field's corresponding
    'control in frm, in order to id record.
    Dim ctl As Control
    Dim varBefore As Variant
    Dim varAfter As Variant
    Dim strControlName As String
    Dim strSQL As String
    On Error GoTo ErrHandler
    'Get changed values.
    For Each ctl In frm.Controls
    With ctl
    'Avoid labels and other controls with Value property.
    Select Case ctl.ControlType
    Case acTextBox, acCheckBox, acComboBox
    If .Value <> .OldValue Then
    varBefore = .OldValue
    varAfter = .Value
    strControlName = .Name
    'Build INSERT INTO statement.
    strSQL = "INSERT INTO " _
    & "tblAudit (EditDate, User, RecordID, SourceTable, " _
    & " SourceField, BeforeValue, AfterValue) " _
    & "VALUES (Now()," _
    & cDQ & Environ("userna me") & cDQ & ", " _
    & cDQ & recordid.Value & cDQ & ", " _
    & cDQ & frm.RecordSourc e & cDQ & ", " _
    & cDQ & .Name & cDQ & ", " _
    & cDQ & varBefore & cDQ & ", " _
    & cDQ & varAfter & cDQ & ")"
    'View evaluated statement in Immediate window.
    Debug.Print strSQL
    DoCmd.SetWarnin gs False
    DoCmd.RunSQL strSQL
    DoCmd.SetWarnin gs True
    End If
    End Select[/CODE]
  • missinglinq
    Recognized Expert Specialist
    • Nov 2006
    • 3533

    #2
    CurrentUser() returns the Access user name. Try substituting for Environ("userna me") in your code.

    Welcome to bytes!

    Linq ;0)>

    Comment

    Working...