Results 1 to 4 of 4
  1. #1
    cgclower is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Jul 2011
    Posts
    6

    Update one table when another table changes

    Hello!



    I have a database with two tables. I am trying to update one of the tables every time a record is added to or deleted from the other table. Right now, I am planning on using this VBA code:

    Code:
     
    Sub Form_AfterInsert()
     
    DoCmd.RunSQL "INSERT INTO  SecondTable VALUES([some value from FirstTable], [another value from FirstTable])"
     
    End Sub
    My question is how do I get the values from the first table after they are inserted/before they are deleted? I was hoping for some sort of ActiveRecord function that would have that data, but I am not finding anything like that.

    Thoughts? Advice?

    Thanks for the help!

    Update: I guess adding an account could be tracked in the same table, but I can't keep track of the deletions there. I am looking for a way to save information out of a record that is about to be deleted. This make sense? Thanks!

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,847
    A technique is to not actually delete records. If you have a field in that table, say "QuasiDelete" or something that is a boolean, you simply update that field to Yes indicating "Deleted".
    You adjust other code to always check the QuasiDelete field. We used to call this ShadowDeletes in the old days, but you always had a copy of a "deleted" record.... just in case.

    Another approach is to have a table that records the record deleted, time of deletion, etc. Just for recovery/audit purposes.
    Good luck.

  3. #3
    cgclower is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Jul 2011
    Posts
    6
    Quote Originally Posted by orange View Post
    Another approach is to have a table that records the record deleted, time of deletion, etc. Just for recovery/audit purposes.
    Good luck.
    Yes, that is exactly what I want to do!

    Um...any idea how to go about that?

    Thank you!

  4. #4
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    your SQL statement won't work

    when you're doing an insert it should look like

    INSERT INTO <tablename> (<field1>, <field2>, etc) VALUES (<value1>, <value2>, etc)

    Where the values would have to be enclosed by ' marks for text fields.

    Secondly what are you actually trying to accomplish by updating the second table? deleting records from a database is permanent and I really would not encourage anyone to do it with code. Instead as orange has said add a marker that is basically a void which you can exclude on all data entry, forms, reports etc.

    I would like to know why you are trying to do this, are you attempting to create a log of who made changes to records in your database?

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

Similar Threads

  1. Replies: 2
    Last Post: 12-22-2010, 01:46 PM
  2. Replies: 0
    Last Post: 11-30-2010, 12:23 PM
  3. Replies: 1
    Last Post: 05-17-2010, 12:03 PM
  4. Replies: 3
    Last Post: 01-21-2010, 08:10 PM
  5. Replies: 4
    Last Post: 09-03-2009, 02:01 PM

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