Results 1 to 12 of 12
  1. #1
    twgonder is offline Expert
    Windows 10 Access 2021
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658

    At record save, scan through and compare old values to new

    I may have asked something similar before, for a different reason, but I can't seem to find it, at least for doing like this:

    As a record in a form is saved, I would like to loop through all the SOURCE fields (not the controls)
    to find fields that have changed from when the record was first loaded.



    How can I loop through all the source fields?
    Is there some way to get the old value with some Access property or am I better to make a "clone" of the source record
    when it's first loaded, to get around other problems that might pop up?

    Then I need to find the values that changed, and based on this I'll be creating change log records.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Data controls have an OldValue property which you can use to compare to Value property.

    If you want an audit trail log, review 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
    twgonder is offline Expert
    Windows 10 Access 2021
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658
    It looks to have lost one of my replies again:

    Anyways, thanks June7 for the link, I'll check it out.
    The problem with using controls, there are some record fields that change without having a control on the form.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Fields don't have OldValue property. What causes field data to change if not by user edit in control? Must be some code. Maybe have that code compare current field value to the new data before field is changed and act appropriately. Otherwise, need a control even if it is hidden from users.
    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
    twgonder is offline Expert
    Windows 10 Access 2021
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658
    I saw this in June7's link, at the top:
    (Note: Access 2010 contains Data Access Macros (effectively triggers), — a better way to create an audit trail if you use the new database format.)

    Does anyone know where and how this works, or a writeup?

    Note: I found this, but I'm not up on Marcos and I'm not sure this is what I would want, since I want to log all changes more efficiently, but I'll poke at it and see if I can get VBA to work with this. But I'm still not sure of the of the syntax for the basic loop to check all fields.

    After Update macro event | Microsoft Learn

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    I've never used any macros but I expect you could have appropriate DataMacro call the appropriate VBA function.

    I am guessing you could have a If Updated("some field name") Then for every field.
    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.

  7. #7
    twgonder is offline Expert
    Windows 10 Access 2021
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658
    @June7 post# 4

    Yes, code is changing non control fields in the record.

    I think that an old post stated, as you did, that there isn't an oldvalue for fields. Confirmed with this:

    Attachment 50596

    So, with that, maybe there is a way to "clone" the record in the Form_Current procedure?

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Yes, can make copy of record into another table or open a recordset filtered to specific record when form opens. Still don't see how that helps compare change of non-control fields. Unless revised data is committed to table for comparison with temp table or recordset, otherwise what would you compare with?
    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.

  9. #9
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Would recordsetclone be useful?
    I have seen where people walk a recordsetclone yet use the form controls and wonder why something is not being updated in the form?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    I just did a test. Opened recordset and print field and textbox values, edit record on form and commit to table, print values again. The recordset value is change. Same result with RecordsetClone. Whatever comparison you want to do has to be before record is committed.
    Code:
    Sub test()
    Dim rs As DAO.Recordset
    DoCmd.OpenForm "Holidays"
    Set rs = Forms!Holidays.RecordsetClone 
    'Set rs = CurrentDb.OpenRecordset("Holidays")
    'Set rs = Forms!Holidays.Recordset
    Forms!Holidays.tbxHolName = "test"
    Debug.Print rs(1)
    Debug.Print Forms!Holidays!HolName
    Debug.Print Forms!Holidays.tbxHolName
    DoCmd.RunCommand acCmdSaveRecord
    Debug.Print rs(1)
    Debug.Print Forms!Holidays!HolName
    Debug.Print Forms!Holidays.tbxHolName
    End Sub
    Last edited by June7; 08-04-2023 at 10:36 AM.
    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.

  11. #11
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    Depends on why you want to know and what you want to do with the information but consider creating an ADO disconnected recordset from your form recordset on load. Then compare the two again on before update.

    if it is to create an audit log, consider a data macro

  12. #12
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726

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

Similar Threads

  1. button to scan a document to pdf and save to folder
    By charlieb in forum Programming
    Replies: 2
    Last Post: 04-20-2015, 09:40 AM
  2. Replies: 3
    Last Post: 11-20-2014, 05:45 PM
  3. compare two values rounding problem
    By BCJourney in forum Queries
    Replies: 7
    Last Post: 08-06-2013, 07:27 AM
  4. Replies: 1
    Last Post: 02-06-2013, 12:18 PM
  5. Replies: 2
    Last Post: 12-19-2011, 10:51 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