【问题标题】:Copying the text from the combo box instead of the primary key in my Access audit trail复制组合框中的文本,而不是我的 Access 审计跟踪中的主键
【发布时间】:2020-06-09 03:52:48
【问题描述】:

我已经为此工作了一个星期,但仍然没有找到方法......在我的访问文件中,我有以下代码可以为对表单所做的更改创建审计跟踪,你能帮忙吗我修改下面的代码,以便在从组合框或选项组进行更改时,审核记录显示组合框/选项组内的文本值,而不是与该文本值关联的主键?

非常感谢您。

K

Option Compare Database

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, acComboBox, acListBox, acOptionGroup

      If IsNull(.Value) And Not IsNull(.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

      ElseIf IsNull(.OldValue) And Not IsNull(.Value) 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

      ElseIf .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 Select
    End With
  Next
  Set ctl = Nothing
  Exit Sub

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

【问题讨论】:

  • 将文本字段包含为组合框的列,并通过其索引引用该列。索引以 0 开头。Me.combobox.Column(1)。至于 OptionGroup(单选按钮?),那就更复杂了。编辑问题以显示 OptionGroup 的数据。
  • 包含文本字段作为组合框的列?
  • 组合框行源,如SELECT ID, fieldname FROM table;。列数:2;列宽:0";2"。 0 宽度将隐藏列。这是基本功能。
  • @June7,我会添加这个作为答案。如果您可以将其逐步介绍一下,那将是一个很好的解释。

标签: vba ms-access


【解决方案1】:

对于组合框,将文本字段包含为组合框的列,并通过其索引引用该列。组合框行源,如 SELECT ID, fieldname FROM table;。设置列数:2; ColumnWidths:0";2"(0 将隐藏列)。索引从 0 开始,因此如果文本字段是第二列,则其索引为 1。Me.combobox.Column(1)

假设选项组使用单选按钮,每个单选按钮都有一个数字 OptionValue。选项组框架采用选中的单选按钮 OptionValue 作为值。将这些数字转换为相关的描述性文本。对表执行 DLookup() 或硬编码转换。带有 OptionValue 1、2、3 的 3 个单选按钮的示例: Choose(Me.optGender, "Female", "Male", "Not Given")

【讨论】:

    猜你喜欢
    • 2015-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-31
    • 1970-01-01
    • 2017-06-28
    相关资源
    最近更新 更多