Access中的自定义审核跟踪以捕获登录详细信息

时间:2019-05-21 19:38:37

标签: access-vba ms-access-forms

我有一个内置于Access中的登录屏幕,我需要将UserName(这是“ Employee”表中的列)存储在“ Audit”表中,该表用于存储在D B。

当我从自定义登录屏幕登录时,应该在(可能是tempvars)中捕获了用户名,并且该名称应保持可用,直到用户关闭程序为止,即在会话期间(以“ user_x”的身份登录)和我也希望在审核表中捕获此用户名。

我从互联网上获得了一个代码来捕获数据库中发生的更改,但是它使用Access登录实用程序。我想对其进行更改,以在用户登录后从我的自定义登录表中捕获登录详细信息。

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.
    If .ControlType = acTextBox Then
      If .Value  .OldValue Then
        varBefore = .OldValue
        varAfter = .Value
        strControlName = .Name
        'Build INSERT INTO statement.
        strSQL = "INSERT INTO " _
           & "Audit (EditDate, User, RecordID, SourceTable, " _
           & " SourceField, BeforeValue, AfterValue) " _
           & "VALUES (Now()," _
           & cDQ & Environ("username") & cDQ & ", " _
           & cDQ & recordid.Value & cDQ & ", " _
           & cDQ & frm.RecordSource & cDQ & ", " _
           & cDQ & .Name & cDQ & ", " _
           & cDQ & varBefore & cDQ & ", " _
           & cDQ & varAfter & cDQ & ")"
        'View evaluated statement in Immediate window.
        Debug.Print strSQL
        DoCmd.SetWarnings False
        DoCmd.RunSQL strSQL
        DoCmd.SetWarnings True
      End If
    End If
    End With
  Next
  Set ctl = Nothing
  Exit Sub

ErrHandler:
  MsgBox Err.Description & vbNewLine _
   & Err.Number, vbOKOnly, "Error"
End Sub

用户使用登录屏幕登录后要在“审核表”中捕获的用户名。

我应该用什么代替

& cDQ & Environ("username") & cDQ & ", " _

捕获我的用户名,该用户名来自我构建的自定义登录屏幕中的成功登录操作。

1 个答案:

答案 0 :(得分:0)

如果要使用用户在登录表单中输入的userName,只需将Environ("username")替换为登录表单中文本框的值。假设您登录表单中的文本框名为txtUserName,然后用以下代码替换您的代码:

= & cDQ & Me!txtUserName.Value & cDQ & ", "

Me!是指执行代码的表单(您的登录表单)。

相关问题