0

我有一个在 Access 中构建的登录屏幕,我需要将 UserName(它是表“Employee”中的一列)存储在我为存储数据库中发生的更改而构建的“Audit”表中。

当我从自定义登录屏幕登录时,它应该在(可能是tempvars)中捕获用户名,并且它应该保持可用直到用户关闭程序,即在会话期间(登录为“user_x”),我也想要这个要在审核表中捕获的用户名。

我从 Internet 获得了一个代码来捕获数据库中发生的更改,但它使用 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 & ", " _

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

4

1 回答 1

0

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

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

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

于 2019-05-22T04:50:32.337 回答