Results 1 to 7 of 7
  1. #1
    shaunacol is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Sep 2012
    Posts
    55

    copying a record

    I have a form which has several subforms within it. I am trying to copy the record in the main and subforms and then post a duplicate record. This is because the record contains details of attractions and events and often there are new similar attractions and events and it will prevent the admin having to reenter the same details. I read up and found some code:

    Private Sub Copy_Click()


    DoCmd.RunCommand acCmdSelectRecord
    DoCmd.RunCommand acCmdCopy
    DoCmd.GoToRecord , , acNewRec
    DoCmd.RunCommand acCmdPaste
    End Sub

    This seems to do the copy and paste but then when I go to change the primary key (SKU) or any field it says "Update or CancelUpdate wthout AddNew or Edit" and I don't know what that means. I can only assume it is freaking out because of duplicate primary keys. My coding is not good enough to create code that updates the primary key. I tried adding the code below which is something I use elsewhere in the database but it says I am missing a = so either my syntax is wrong or there is another problem.

    Private Sub Copy_Click()
    DoCmd.RunCommand acCmdSelectRecord
    DoCmd.RunCommand acCmdCopy
    DoCmd.GoToRecord , , acNewRec
    DoCmd.RunCommand acCmdPaste
    docmd.SetProperty ([sku],acPropertyValue,Left$(DLast("[SKU]","tbl_attraction"),1) & Format$(Val(Right$(DLast("[SKU]","tbl_attraction"),3)+1),"000"))
    End Sub

    So....im not sure this is the best way to do what I want to do. If it is then is this error because of the primary key or maybe because of the subforms? Any help appreciated.



  2. #2
    alansidman's Avatar
    alansidman is offline Indifferent
    Windows 10 Access 2013
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,538
    Perhaps this:

    Code:
    Private Sub copyrecordbutton_Click()
    On Error GoTo Err_copyrecordbutton_Click
    Dim txtOld1 As Variant
    Dim txtOld2 As Variant
    Dim txtOld3 As Variant
    Dim txtOld4 As Variant
    txtOld1 = txtcurrent1.Value
    txtOld2 = txtcurrent2.Value
    txtOld3 = txtcurrent3.Value
    txtOld4 = txtcurrent4.Value
    RunCommand acCmdRecordsGoToNew
    txtnew1.Value = txtOld1
    txtnew2.Value = txtOld2
    txtnew3.Value = txtOld3
    txtnew4.Value = txtOld4
    Exit_copyrecordbutton_Click:
    Exit Sub
    Err_copyrecordbutton_Click:
    MsgBox Err.Description
    Resume Exit_copyrecordbutton_Click
    End Sub

  3. #3
    shaunacol is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Sep 2012
    Posts
    55
    Thanks so much for your reply. Is that the only way to do it as I have over 100 fields within the form and subforms. Thank you.

  4. #4
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Allen Browne has a tutorial for doing just that:

    Duplicate the Record in Form and Subform

    Linq ;0)>

  5. #5
    NTC is offline VIP
    Windows 7 64bit Access 2013
    Join Date
    Nov 2009
    Posts
    2,392
    Definitely you cannot have duplicate primary keys. Generally the primary key should be an auto number field, not necessarily seen by any users. When you go to duplicate a record, one leaves the key field out because it is going to auto generate by the table as part of the creation of the new record. So if you are using an auto number field - just remove that key field from your append query.

    If you are using a self generated primary key then you must create that key in a calculated field and include it in the append query.

  6. #6
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    And if you use the code given in Post #1, to do the copying, the Access Gnomes automatically know to not copy the AutoNumber/PK Field.

    Linq ;0)>

  7. #7
    shaunacol is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Sep 2012
    Posts
    55
    Sorry I am not an experienced programmer so am trying to find the easiest solution without too much code. I have more than 100 fields in the form and subforms. I would rather not have to list all those out and then have issues because im not properly referencing (which always happens to me). If this is the simplest solution then of course I will have to do that. But is there no way to do a simple cut and paste? Its for the boss to use, not end users, so I have even tried opening a query with that record in so he can manually cut and paste but the primary key issue gets in the way. Then I tried an append query but couldn't figure that one out either. I have, however, figured out the reason I get "Update or CancelUpdate wthout AddNew or Edit" error, it is because my form is filtered. Going to keep trying tomorrow but if anyone has any other insights they are greatly appreciated thanks

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

Similar Threads

  1. Replies: 16
    Last Post: 02-06-2013, 03:06 PM
  2. Replies: 5
    Last Post: 08-24-2012, 10:32 AM
  3. copying a record and adding in new text
    By will_clarke in forum Access
    Replies: 1
    Last Post: 01-12-2012, 11:08 AM
  4. Copying Values from One Record to Another
    By Iconoclast in forum Access
    Replies: 5
    Last Post: 08-02-2011, 11:59 AM
  5. Replies: 2
    Last Post: 03-25-2011, 12:22 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