Results 1 to 7 of 7
  1. #1
    kevint57 is offline Novice
    Windows 7 64bit Access 2016
    Join Date
    Jan 2017
    Posts
    5

    Update previous records in table when pressing save button on form

    Hi all,

    A bit of background first.
    I have a table that I keep all the records for training that staff have undertaken.
    Among other fields in the table, the main ones are - Surname, First_Name, Element, Date_of_Training, Entry_Date (ie data entry date), Current_Training (which is a yes/No field) and Revision_Date. The table is called tbl_Training.

    I also have a form that I use to enter new records. This form has a Save button in it. The form is called frm_Training_Entry_Form.
    I also have 2 more forms.
    1 form is used to extract ALL the training an individual has undertaken including any refresher training.


    The 2nd form is used to only extract the latest training for all staff in a certain Element.

    What I want to happen is when I hit the save button on the frm_Training_Entry_Form it should update the Current_Training field automatically in the tbl_Training table to No for all previous records for that person, providing the Surname, First_Name and Element are the same as the new record. I don't know how to do this.
    In other words, I only want the 2nd form to display the latest entries for each person for each Element.

    If you could provide a solution to my nightmare I would much appreciate it.

    Cheers,

    Kevin

  2. #2
    Join Date
    Apr 2017
    Posts
    1,679
    Write a saved query to calculate latest Date_of_Training for every person. (Btw. create a separate table to register persons like tblPersons: PersonID, First_Name, Surname, and include PersonID into training records table and remove names from there. Otherwise you'll have a problem when you have 2 different people with identical names. And for solution you need a way to define person uniquely!) A possible query syntax:
    qLastTraining: SELECT PersonID, MAX(Date_of_Training) AS [LastTraining] FROM tblTrainingRecords GROUP BY PersonID

    Now in OnClick event of save button you call an update query of tblTrainingRecords for current person, where you set the value for Current_Training depending on Date_of_Training being equal to LastTraining in qLastTraining or not. The query string will be like
    "UPDATE tblTrainingRecords tr JOIN qLastTraining lt on lt.PersonID = tr.PersonID
    SET tr.Current_Training = Iif(lt.Date_of_Training = tr.LastTraining,"yes","no")
    WHERE lt.PersonID = " & Me.txtPersonIDControl

    P.S. When you change the Current_Training field type to Boolean (TRUE/FALSE), the update query will be simplier.
    ...
    SET tr.Current_Training = (lt.Date_of_Training = tr.LastTraining)
    ...

  3. #3
    kevint57 is offline Novice
    Windows 7 64bit Access 2016
    Join Date
    Jan 2017
    Posts
    5
    Thanks for the tips Arvi.
    I am working through it and will let you know how it goes.
    Cheers,
    Kevin

  4. #4
    kevint57 is offline Novice
    Windows 7 64bit Access 2016
    Join Date
    Jan 2017
    Posts
    5
    Hi Arvi,

    Have done as you suggested. I changed the ID in the Employee table to PersonID and reflected this in the tbl_Training table, removing Surname and First_Name from that table. Both of these tables have a relationship through the PersonID. I also changed the Current_Training field to Boolean (True/False) as suggested.
    How do I "call" an update query in the OnClick event of the save button? I have looked but can't work out how to do it.
    TIA,
    Kevin

  5. #5
    kevint57 is offline Novice
    Windows 7 64bit Access 2016
    Join Date
    Jan 2017
    Posts
    5
    Also, the code comes up with a Syntax error when I paste it into a new query.
    Kevin

  6. #6
    Join Date
    Apr 2017
    Posts
    1,679
    Here is a simple example (I used form events instead of button to trigger updates).

    When form is opened, then all records are updated;
    During session, trainings for current employee are updated whenever a record in trainings subform is added/edited or deleted.
    Attached Files Attached Files

  7. #7
    kevint57 is offline Novice
    Windows 7 64bit Access 2016
    Join Date
    Jan 2017
    Posts
    5
    Hi Arvi,

    Did post on Friday saying it was solved and saying thank you.
    Post may not show until Monday US time or something.
    Also hit the solved button in threads above.

    Cheers.

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

Similar Threads

  1. Replies: 4
    Last Post: 05-03-2017, 10:37 AM
  2. Replies: 2
    Last Post: 09-09-2016, 01:50 PM
  3. Replies: 3
    Last Post: 10-08-2012, 08:40 AM
  4. Replies: 1
    Last Post: 06-15-2012, 10:47 AM
  5. Replies: 7
    Last Post: 10-28-2011, 03:42 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