Results 1 to 9 of 9
  1. #1
    mick3911 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Feb 2013
    Location
    Wirral, UK
    Posts
    66

    Copying a single record to same table

    When a user enters the required data and clicks on the ’Save’ button, I would like to copy the record and paste it into the same table with one exception in that the Date Completed is placed into the PrevTrgCompDate field.
    I have the following code which works great except that the Date Completed data is not placed into the PrevTrgCompDate field. (Last line of the code).


    Can someone point out to me where I am going wrong and the best way to fix this issue.

    Code:
    Private Sub btnSave_Click()
    
    If IsNull(Me.Date_Completed) Then
    MsgBox "Date Completed is required"
    Me.Date_Completed.SetFocus
    
    Else
    
    If IsNull(Me.Trained_By) Then
    MsgBox "Trained By is required"
    Me.[Trained_By].SetFocus
    
    Else
    
    Me.Required.Value = False
    
    Dim currentID As Long
    
    currentID = TrainingID
    
    DoCmd.GoToRecord record:=acNewRec
    
    EmployeeID = DLookup("EmployeeID", "tblTrainingHistory", "TrainingID=" & currentID)
    TrainingItemID = DLookup("TrainingItemID", "tblTrainingHistory", "TrainingID=" & currentID)
    TrainingCodeSOPNo = DLookup("TrainingCodeSOPNo", "tblTrainingHistory", "TrainingID=" & currentID)
    TrainingDescription = DLookup("TrainingDescription", "tblTrainingHistory", "TrainingID=" & currentID)
    IssueNo = DLookup("IssueNo", "tblTrainingHistory", "TrainingID=" & currentID)
    PrevTrgCompDate = DLookup("Date_Completed", "tblTrainingHistory", "TrainingID=" & currentID)

  2. #2
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    I suspect that the date completed hasn't been saved so it doesn't find a record.
    Try a

    Code:
    If Me.Dirty Then Me.Dirty = False
    to force a save just before all the Dlookups.

    Also I would be prefixing all the updated controls with Me.txtMyControlName to reference them on the form correctly.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  3. #3
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,933
    just curious as to why you need to save a second record - the previous completed date is easy to determine

  4. #4
    mick3911 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Feb 2013
    Location
    Wirral, UK
    Posts
    66
    Hi Minty,

    Unfortunately that hasn't solved it. The same still happens.

  5. #5
    mick3911 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Feb 2013
    Location
    Wirral, UK
    Posts
    66
    The idea is that there is an existing record (training item), when training is completed the user enters the relevant details.
    The DB is set up so that dependant on the training item, refresher training will be required, the 'copied record' will then be completed when the refresher training training is completed and so on.
    We need to keep all training records as historic data

  6. #6
    mick3911 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Feb 2013
    Location
    Wirral, UK
    Posts
    66
    As a note, I have tried an Append query (here is the SQL code)

    Code:
    INSERT INTO tblTrainingHistory ( EmployeeID, TrainingItemID, TrainingCodeSOPNo, TrainingDescription, IssueNo, PrevTrgCompDate, [Refresher Due] )SELECT tblTrainingHistory.EmployeeID, tblTrainingHistory.TrainingItemID, tblTrainingHistory.TrainingCodeSOPNo, tblTrainingHistory.TrainingDescription, tblTrainingHistory.IssueNo, tblTrainingHistory.[Date Completed], qryEmployeeHistory.[Due Refresher]
    FROM (tblTrainingItems INNER JOIN tblTrainingHistory ON tblTrainingItems.TrainingCodeSOPNo = tblTrainingHistory.TrainingCodeSOPNo) INNER JOIN qryEmployeeHistory ON tblTrainingItems.TrainingItemID = qryEmployeeHistory.TrainingItemID;
    But this duplicates the record multiple times.

  7. #7
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    That would work but you need to concatenate in a where clause to restrict it just adding the one record.

    Code:
    WHERE TrainingID=" & Me.currentID
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  8. #8
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,933
    We need to keep all training records as historic data
    I understand that, just not why you need the previous date field since it is easily determined. However you've answered my question and I don't want to distract you from your problem

  9. #9
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,933
    looking at the problem

    you said

    which works great except that the Date Completed data is not placed into the PrevTrgCompDate field
    so that implies there is something wrong with this line

    PrevTrgCompDate = DLookup("Date_Completed", "tblTrainingHistory", "TrainingID=" & currentID)

    since the other fields are being populated so currentID is presumably correct

    so either you have a typo or date completed is blank

    and looking at your sql you have [date completed] and in your code "Date_Completed", one with an underline and one with a space

    Since the sql works in that it does not generate errors, I assume the space is the correct spelling, so you need

    PrevTrgCompDate = DLookup("[Date Completed]", "tblTrainingHistory", "TrainingID=" & currentID)


    Note it is generally a bad idea to have spaces in field and table names - it causes problems like this

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

Similar Threads

  1. Replies: 3
    Last Post: 08-02-2015, 12:42 AM
  2. Replies: 2
    Last Post: 12-05-2013, 12:09 PM
  3. Replies: 12
    Last Post: 04-09-2012, 08:47 AM
  4. Replies: 4
    Last Post: 11-07-2011, 07:25 PM
  5. Replies: 5
    Last Post: 03-23-2011, 10:39 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