Results 1 to 6 of 6
  1. #1
    edson is offline Advanced Beginner
    Windows 8 Access 2007
    Join Date
    Oct 2014
    Posts
    57

    Funtion code to log field changes into a table

    Fellows, I got the following function code from someone in the net and adapted the code to meet my needs. Unfortunately I lost contact with him.
    As you can see the purpose of this function code is to register into the TblLogChanges every single record exclusion or record change made in any form field of my project when the form is active. In other words if the user changes for example the value of the field Color in the form FrmX from Yellow to Black, the TblLogChanges will have added a line indicating: FrmX, type of change (record change or record exclusion), RecordId (in my case CADID), Current User, Date, and a complimentary string showing Field Color old value=Yellow --> Field Color new value=Black.
    It works fine when the field “old” value is populated (not null).
    However, if the feld old value is null, meaning, if the field value is originally null, or yet if the field is originally empty, and I fill it up with some data, then the code does not add an indicative line into the table TblLogChanges.
    See function code below.
    FYI the function is called in both Before Update and Delete events of the form with the following syntaxes: =logChangeFrmCadastro("E") for record exclusions, and =logChangeFrmCadastro("A") for record changes.
    I have failed so far to make it happen. Reason is I'm able to read and understand most written syntax but still not good enough to create my own codes since I do not know to use the range of VBA commands and syntaxes available yet. This is a knowledge gap I'm doing my best to fill in.
    In the mean time does anybody could tell me please what function code changes are required so the function be able to add an indicative line into TblLogChanges when the user changes a form field from “empty/nothing” to something?
    Thank you.

    Code:
    Option Compare Database
    Option Explicit
     
    Function LogChangeFrmCadastro(strTipo As String)
    On Error Resume Next
     
    Dim db As Database, rslog As Recordset
    Dim frm As Form, I As Integer
    Dim strLog As String
     
    Set db = CurrentDb
    Set rslog = db.OpenRecordset("TblLogChanges")
    Set frm = Screen.ActiveForm
       
    For I = 0 To frm.Controls.Count - 1
        If TypeOf frm.Controls(I) Is TextBox Or TypeOf frm.Controls(I) Is ComboBox Or TypeOf frm.Controls(I) Is CheckBox Or TypeOf frm.Controls(I) Is OptionGroup Or TypeOf frm.Controls(I) Is ListBox Then
            If strTipo = "E" Then
                If strLog = "" Then
                    strLog = frm.Controls(I).Name & " " & frm.Controls(I).Value
                Else
                    strLog = strLog & "," & frm.Controls(I).Name & ":" & frm.Controls(I).Value
                End If
            Else
                If frm.Controls(I).OldValue <> frm.Controls(I).Value Then
                    If strLog = "" Then
                         strLog = strLog & "CADID = " & frm.Controls("CADID") & "," & frm.Controls(I).Name & ":" & frm.Controls(I).OldValue & " --> " & frm.Controls(I).Value
                    Else
                        strLog = strLog & "," & frm.Controls(I).Name & ":" & frm.Controls(I).OldValue & " --> " & frm.Controls(I).Value
                    End If
                End If
            End If
        End If
    Next
       
    rslog.AddNew
    rslog("strNomeForm") = frm.Name
    rslog("strTipoLog") = strTipo
    rslog("mmolog") = strLog
    rslog("strUser") = CurrentUser
    rslog("dtLog") = Now
    On Error Resume Next
    rslog.Update
    rslog.Close
    db.Close
     
    End Function


  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Maybe have to use a text substitute, like:

    Nz(frm.Controls(I).OldValue, "Null")

    or

    Nz(frm.Controls(I).OldValue, "nothing")


    Look at Allen Browne's code http://allenbrowne.com/AppAudit.html
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    edson is offline Advanced Beginner
    Windows 8 Access 2007
    Join Date
    Oct 2014
    Posts
    57
    Hello June7. Happy New Year.
    Text substitute did not work either. Also have tried " If Isnull (strLog) Then... " which also failed.
    I took a look at the Allen Browne's code. Actually you suggested it to me some time ago. However it is too complex for me and it has several restrictions. It seems to me that the one above is more simplist and would fulfill my needs, except so far for the field changes from null/nothing to something.
    Any other ideas please?
    Thank you.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Where does the code fail - on the If Then condition?

    Another thread with similar question http://stackoverflow.com/questions/1...trols-within-v

    If frm.Controls(I).OldValue & "" <> frm.Controls(I).Value & "" Then
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    edson is offline Advanced Beginner
    Windows 8 Access 2007
    Join Date
    Oct 2014
    Posts
    57
    Thank you once more June7. You have helped me a lot. I got my problem solved adapting your suggestion for me to read http://stackoverflow.com/questions/1...trols-within-v
    By the way, I fixed the issue of registering a field data change from nothing (null) to something and vice versa through if conditions. Also I replaced data source by the FrmName. It works as a charm.
    I would like to offer this following code enhancement as a single contribution to others who may be interested on that matter, so if you feel that the enhancement is valid and smart enough, feel free to publish it. I believe it would be more reliable for the readers if you publish it. You have a lot of more credit than I do. Thanks again.

    [code]
    Sub AuditTrail (frm As Form, RecordID As Control)
    Dim ctl As Control
    Dim varBefore As Variant
    Dim varAfter As Variant
    Dim strControlName As String
    Dim StrSQL As String
    On Error GoTo ErrHandler

    For Each ctl In frm.Controls
    With ctl
    If RecordID.Value >= DMax("PrimaryKeyFieldName", "TableName") Then
    GoTo MOVENEXT
    Else
    If .ControlType = acTextBox Or .ControlType = acComboBox Or .ControlType = acCheckBox Or .ControlType = acOptionGroup Or .ControlType = acListBox Then
    If IsNull(.Value) And IsNull(.OldValue) Then
    GoTo MOVENEXT
    Else
    If IsNull(.OldValue) Then
    varBefore = ""
    varAfter = .Value
    strControlName = .Name
    GoTo INSERTION
    Else
    If IsNull(.Value) Then
    varBefore = .OldValue
    varAfter = ""
    strControlName = .Name
    GoTo INSERTION
    Else
    If .Value = .OldValue Then
    GoTo MOVENEXT
    Else
    varBefore = .OldValue
    varAfter = .Value
    strControlName = .Name
    INSERTION:
    StrSQL = "INSERT INTO " _
    & "TblAudit (EditDate, User, RecordID," _
    & " FrmName, SourceField, BeforeValue, AfterValue) " _
    & "VALUES (Now()," _
    & cDQ & CurrentUser & cDQ & ", " _
    & cDQ & RecordID.Value & cDQ & ", " _
    & cDQ & frm.Name & cDQ & ", " _
    & cDQ & .Name & cDQ & ", " _
    & cDQ & varBefore & cDQ & ", " _
    & cDQ & varAfter & cDQ & ")"
    DoCmd.SetWarnings False
    DoCmd.RunSQL StrSQL
    DoCmd.SetWarnings True
    End If
    End If
    End If
    End If
    End If
    End If
    End With
    MOVENEXT:
    Next
    Set ctl = Nothing
    Exit Sub
    ErrHandler:
    MsgBox Err.Description & vbNewLine _
    & Err.Number, vbOKOnly, "Error"
    End Sub
    [code/]

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    It's published by inclusion in this thread. Just a note, the CODE tags not typed properly so the indention was lost. The / goes first, like /CODE.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 7
    Last Post: 08-23-2014, 05:52 AM
  2. FileCopy Funtion
    By dccjr in forum Programming
    Replies: 2
    Last Post: 04-18-2013, 09:04 PM
  3. VBA code to update field in table
    By vickan240sx in forum Access
    Replies: 1
    Last Post: 06-23-2012, 08:24 PM
  4. Adding a field to a table with vba-code
    By Mgomp in forum Programming
    Replies: 4
    Last Post: 03-12-2012, 07:58 AM
  5. Random Rdn() Funtion Help Please
    By graviz in forum Access
    Replies: 1
    Last Post: 12-09-2009, 11:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums