Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Hashirama is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Jul 2014
    Location
    Mount olympus
    Posts
    11

    Move a record from one table to another upon an event

    I don't know where to put this. Moderators please move this to the appropriate section if required


    I have 2 tables, a and b.
    Then I have records in each table with fields ,Name and Status(Done and Done_s)
    I want records having "Done_s" to be moved to b whenever the change has occurred. Is this possible?(I have tried somethings with Macros but didn't work)

    I am completely new to access. So any help will be appreciated

  2. #2
    burrina's Avatar
    burrina is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Freeport,Texas
    Posts
    1,383
    Hi, I don't use Macros so no help there. But if you want to copy a record from one table to another, why is my question? Duplicating data is a bad idea! Why not instead use a query for your form based on both tables?

    HTH

  3. #3
    Hashirama is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Jul 2014
    Location
    Mount olympus
    Posts
    11
    Quote Originally Posted by burrina View Post
    If you want to copy a record from one table to another, why is my question? Duplicating data is a bad idea! Why not instead use a query for your form based on both tables?
    No. I don't want to copy data. I want to Move it to b.

  4. #4
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Hi -

    A "move" actually be thought of as two operations: first, a copy from a to b, and then a Delete from a.

    When do you want the move to occur - as soon as a user makes the change from "done" to "Done_S"? Not really a good idea, IMO, because what happens if a user makes a mistake - how do you get the record back?

    Assuming are using a form and editing TableA, what you want to do could be done with three lines of code in the On_click event of a command button on the form:

    currentdb.execute "Insert into TableB select * from TableA where Status = 'Done_s' ", dbfailonerror
    currentdb.execute "Delete from TableA where Status = 'Done_s' ", dbFailonerror
    me.requery

    line 1 copies the records from Table A to table B
    line 2 deletes them from Table A once they have been copied
    line 3 updates the records you see on the form to remove the moved ones

    HTH

    John
    Last edited by John_G; 07-08-2014 at 11:58 AM. Reason: fix a coding mistake

  5. #5
    Hashirama is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Jul 2014
    Location
    Mount olympus
    Posts
    11
    Quote Originally Posted by John_G View Post
    Hi -

    A "move" actually be thought of as two operations: first, a copy from a to b, and then a Delete from a.

    When do you want the move to occur - as soon as a user makes the change from "done" to "Done_S"? Not really a good idea, IMO, because what happens if a user makes a mistake - how do you get the record back?

    Assuming are using a form and editing TableA, what you want to do could be done with three lines of code in the On_click event of a command button on the form:

    currentdb.execute "Insert into TableB select * from TableA where Status = 'Done_s' ", dbfailonerror
    currentdb.execute "Delete from TableA where Status = 'Done_s' ", dbFailonerror
    me.requery

    line 1 copies the records from Table A to table B
    line 2 deletes them from Table A once they have been copied
    line 3 updates the records you see on the form to remove the moved ones

    HTH

    John
    Thanks. I am actually using just a plain table. Do you think a form is a better way?

  6. #6
    Hashirama is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Jul 2014
    Location
    Mount olympus
    Posts
    11
    Click image for larger version. 

Name:	Don.PNG 
Views:	10 
Size:	158.2 KB 
ID:	17165
    This is what I am trying to do (Done_no is just another option)
    It's just facebook page shares, I want to keep track of them all.


    A form shows one record only . So I can't use it

  7. #7
    burrina's Avatar
    burrina is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Freeport,Texas
    Posts
    1,383
    You should NOT be working in your tables directly! YES, a form. Tables are for storing data, forms are for entering data. John_G gave you some good advice.

  8. #8
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    If you are just entering or updating the data directly in the table, there is not a "better" way - there is "no" way. Tables do not have events, and cannot react in that manner to changes. That's what forms are for.

    John

  9. #9
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    "A form shows one record only . So I can't use it"

    Not true - it depends on the "Default View" setting in form design. The "Single Form" shows only one record at a time, yes, but "Continuous Forms" and "Datasheet" views allow multiple records to show at the same time, and you can scroll through them.

    John

  10. #10
    Hashirama is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Jul 2014
    Location
    Mount olympus
    Posts
    11
    Oh, thanks guys. Thank you very much.
    But there is no button in the form. It is a drop down list. So what about using "After update" event?

  11. #11
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    The After Update of a dropdown list would probably work too - what is in the dropdown list, and what (if any) table field is it bound to?

    John

  12. #12
    Hashirama is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Jul 2014
    Location
    Mount olympus
    Posts
    11
    Quote Originally Posted by John_G View Post
    The After Update of a dropdown list would probably work too - what is in the dropdown list, and what (if any) table field is it bound to?

    John
    It's {Done,Done_no,Done_s,No,No_s}

    {Done_s,No_s} will always go to "In debt" table(My image above)
    {Done,Done_no} will go to "Done" table.
    {No} will go to "No answer" table

  13. #13
    Hashirama is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Jul 2014
    Location
    Mount olympus
    Posts
    11
    Anyone?
    .
    .
    .

  14. #14
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Hi -

    Yes, the after update will work. The only change needed is that the table a record is moved to will vary according to the value selected, and the criteria to identify the record will change. But, as I said earlier, you'd have to be careful not to make a mistake, because as soon as you change the status, the record is gone from the table you are looking at.

    "Done" and "Done_no" won't have to be moved as they are already in the proper table.

    Your code could be something like this:

    Code:
    Select Case Status
      Case "Done_s", "No_s"
         currentdb.execute "Insert into [In Debt] select * from [Done] where ID = " & me!ID, dbfailonerror
         currentdb.execute "Delete from [Done] where ID = " & me!ID, dbFailonerror
         me.requery
      Case "No"
         currentdb.execute "Insert into [No Answer] select * from [Done] where ID = " & me!ID, dbfailonerror
         currentdb.execute "Delete from [Done] where ID = " & me!ID, dbFailonerror
         me.requery
      case "Else"
        '
        '  No action required
        '
    End Select
    One possible pitfall here - if ID is an Autonumber field, there is no guarantee that it will be the same is the tables you are moving the records to ([No Answer] or [In Debt])

    John

  15. #15
    Hashirama is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Jul 2014
    Location
    Mount olympus
    Posts
    11
    Is there no way to make an ID which is unique to all the tables?

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 07-30-2013, 03:40 PM
  2. Replies: 19
    Last Post: 06-05-2012, 08:19 AM
  3. Replies: 7
    Last Post: 01-02-2012, 06:19 PM
  4. Move a single record to an archive table
    By 10 Gauge in forum Forms
    Replies: 7
    Last Post: 02-14-2011, 06:50 AM
  5. Move a record from one table to another table.
    By botts121 in forum Programming
    Replies: 4
    Last Post: 06-25-2009, 12:53 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