Tracking record changes in another table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Becky99
    New Member
    • Mar 2008
    • 1

    #1

    Tracking record changes in another table

    In the database I'm working on (2003) I have a client table that includes a client status field, and I have a linked notes table that includes the note entry date, note type, and note details. Each client has multiple notes.

    Each time the client status is changed in the client table, I would like to have a note automatically created in the notes table that details the status change and the note type.

    For example: if the user changes the client status in the client table from "Pending" to "Attending" , I would like a note to be created for that client with the note type of "Attending" and note details stating that the status has been changed from "Pending" to "Attending. "

    These changes are all done by the user through forms. I don't need to know which user made the change. How do I do this? I know some VB, but I'm not advanced.
  • dzsoundnirvana
    New Member
    • Sep 2008
    • 2

    #2
    I am trying to remember where i found this but I use the following:

    1: create a table with the name of AuditTrail with the following fields:
    EditRecordID,Ed itDate,User,Rec ordID,SourceTab le,SourceField, BeforValue,Afte rValue (I set all to text/255)

    2: Create a standard module with a name of basAuditTrail and insert the following (this is not my work and no copyright information was given)
    [HTML]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
    Select Case ctl.ControlType
    Case acTextBox, acCheckBox, acComboBox, acOptionGroup
    If .value <> .OldValue Then
    varBefore = .OldValue
    varAfter = .value
    strControlName = .name
    'Build INSERT INTO statement.
    strSQL = "INSERT INTO " _
    & "AuditTrail (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 If
    End Select
    End With
    Next
    Set ctl = Nothing
    Exit Sub[/HTML]
    3:on each form that you want to track changes insert the following to a [HTML]Form_BeforeUpda te() [/HTML] event

    [HTML]Call AuditTrail(Me, Form name right here)[/HTML] so it would look something like this

    [HTML]Private Sub Form_BeforeUpda te(Cancel As Integer)
    Call AuditTrail(Me, Form name right here)
    End Sub[/HTML]

    Comment

    Working...