Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2012
    Posts
    12

    Copying the Current Record into a new Record (with designated new primary key)

    I've read a few things about how to copy records and mostly what I see is that copying doesn't work cause of the primary key being unable to be duplicated. However, I found this thread;
    https://www.accessforums.net/access/...ord-15269.html

    Where the individual managed to get a copy working using this example;
    http://www.allenbrowne.com/ser-57.html

    I'm getting the same error she got at the end where it says "Item not found in this collection". Please note that I am trying to copy the current record in my form.

    So I think the important bit about my database is that my primary key is not automatic, it's a user designated number. The number is basically a series of numbers representing the date in a field called cboStartDate (for example: 240823 where 24 is the Japanese year, 08 is the month, and 23 is the day) and then two digit iterations up for how many records start on that date (example: 24082301, 24082302, 24082303.) Optimally, I would like to be specifying the number via a message box at the time of copy if automating that part isn't easy. Is this workable?

    Here is the code I am using;


    Code:
    Private Sub CopyJob_Click()
    'On Error GoTo Err_Handler
        'Purpose:   Duplicate the current form record
       
        'Save any edits first
        If Me.Dirty Then
            Me.Dirty = False
        End If
        
        'Make sure there is a record to duplicate.
        If Me.NewRecord Then
            MsgBox "Select the record to duplicate."
        Else
            'Duplicate the main record: add to form's clone.
            With Me.RecordsetClone
                .AddNew
     'This is my record's primary key. This part doesn't snag.
                    !JobOrderID = Me.JobOrderID.Value + 1
    'This is an alphanumeric field on my form and where the code stops working.
                    !Combo351 = Me.Combo351
                    !SubName = Me.SubName
                    !JobOrderDate = Date
                    !cboStartDate = Me.cboStartDate
                    !Combo312 = Me.Combo312
                    !Check509 = Me.Check509
                    !Check529 = Me.Check529
                    !cboEndDate = Me.cboEndDate
                    !InstructorName = Me.InstructorName
                    !Eva1Due = Me.Eva1Due
                    !Eva2Due = Me.Eva2Due
                    !Eva3Due = Me.Eva3Due
                    !EvaluationNotes = Me.EvaluationNotes
                    !IntrRate = Me.IntrRate
                    !MonStart1 = Me.MonStart1
                    !MonFinish1 = Me.MonFinish1
                    !TueStart1 = Me.TueStart1
                    !TueFinish1 = Me.TueFinish1
                    !WedStart1 = Me.WedStart1
                    !WedFinish1 = Me.WedFinish1
                    !ThuStart1 = Me.ThuStart1
                    !ThuFinish1 = Me.ThuFinish1
                    !FriStart1 = Me.FriStart1
                    !FriFinish1 = Me.FriFinish1
                    !SatStart1 = Me.SatStart1
                    !SatFinish1 = Me.SatFinish1
                    !SunStart1 = Me.SunStart1
                    !SunFinish1 = Me.SunFinish1
                    'etc for other fields.
                .Update
                
                'Save the primary key value, to use as the foreign key for the related records.
                .Bookmark = .LastModified
    
                'Display the new duplicate.
                Me.Bookmark = .LastModified
            End With
        End If
    
    Exit_Handler:
        Exit Sub
    
    Err_Handler:
        MsgBox "Error " & Err.Number & " - " & Err.Description, , "cmdDupe_Click"
        Resume Exit_Handler
           
    End Sub
    I do not have any subforms on this form, so I removed the append query. I hope that didn't mess something up.

    I'm grateful for any help!

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    In this line:


    !Combo351 = Me.Combo351

    the first part would have to be the name of the field in the data. What you have is presumably the name of a control on the form (which is okay for the second part). Thus I'd expect something like:


    !FieldName = Me.Combo351
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Join Date
    Aug 2012
    Posts
    12
    That was it! Thank you for helping me to identify my silly mistake!

    It works perfectly now.

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    Join Date
    Aug 2012
    Posts
    12
    Is it inappropriate to ask a question about the 2nd half of my goal?

    With regard to the Order ID, I have created a user prompt to specify the first 6 digits, but I'd like to be able to ask the database to assign the last two digits. So if I put in 240822 then the database would look to see if 24082201 exists. If not- create. If it already exists, increment up to 24082202 and repeat. Alternately find the max of the 2 digit serial with those 6 digits matching and add one to make it a one step instead of a loop. I dunno how to do either, or which approach would be best.

    I can start a new question thread if it is inappropriate to continue this one. Please advise.

    ~~~~~~~~~~
    Oops, I figured it out.

    I found a VBA command called dmax and I'm just telling it to use the criteria of the left 6 digits of my table field as matching the user provided number and then adding + 1 to the result. It seems to be working exactly as I need it to.
    Last edited by Instructor Support; 08-24-2012 at 12:31 AM. Reason: =DMax("OrderID", "JobsTable", "Left(OrderID, 6) =" & UserEntered) + 1

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Sorry, I missed the follow-up question. You used the method I would have recommended.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Copying Values from One Record to Another
    By Iconoclast in forum Access
    Replies: 5
    Last Post: 08-02-2011, 11:59 AM
  2. Set Current Record based on Primary Key Input
    By andersonEE in forum Forms
    Replies: 2
    Last Post: 06-24-2011, 08:23 AM
  3. Replies: 2
    Last Post: 03-25-2011, 12:22 PM
  4. Replies: 0
    Last Post: 02-25-2011, 09:40 AM
  5. copy current record to new record
    By er_manojbisht in forum Forms
    Replies: 1
    Last Post: 02-27-2010, 05:31 PM

Tags for this Thread

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