Page 1 of 2 12 LastLast
Results 1 to 15 of 29
  1. #1
    Marianna_Air is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2012
    Posts
    56

    Duplicate Record

    I'm having trouble with this duplicate code. I want to duplicate the complete record except for the Vendor. Here is the code I'm using

    Code:
    Private Sub cmdDupe_Click___Click()
    'On Error GoTo Err_Handler
        'Purpose:   Duplicate the main form record and related records in the subform.
        Dim strSql As String    'SQL statement.
        Dim lngID As Long       'Primary key value of the new 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
                    !RequestDate = Date
                    !RequestforQuotesID = RequestforQuotesID
                    'etc for other fields.
                .Update
                
                'Save the primary key value, to use as the foreign key for the related records.
                .Bookmark = .LastModified
                lngID = !RequestforQuotesID
                
                'Duplicate the related records: append query.
                If Me.[Request for Quotes Subform].Form.RecordsetClone.RecordCount > 0 Then
                    strSql = "INSERT INTO [INVENTORY TRANSACTIONS] ( REQUESTFORQUOTESID, ProductID, UNITESORDEREDCONV, ) " & _
                        "SELECT " & lngID & " As NewID, ProductID, Quantity, " & _
                        "FROM [INVENTORY TRANSACTIONS] WHERE REQUESTFORQUOTESID = " & Me.RequestforQuotesID & ";"
                    DBEngine(0)(0).Execute strSql, dbFailOnError
                Else
                    MsgBox "Main record duplicated, but there were no related records."
                End If
                
                '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


  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,652
    It would help if you said what the "trouble" was. Offhand, there's an extra comma before FROM in the SQL.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Marianna_Air is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2012
    Posts
    56
    I get a run time error it fails at
    Code:
    Private Sub Command166_Click()
    'On Error GoTo Err_Handler
        'Purpose:   Duplicate the main form record and related records in the subform.
        Dim strSql As String    'SQL statement.
        Dim lngID As Long       'Primary key value of the new 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
                    !EmployeeID = Me.EmployeeID
                    !RequestDate = Date
                    !RequestforQuotesID = GetNextSN("RFQ")
                    'etc for other fields.
                .Update
                
                'Save the primary key value, to use as the foreign key for the related records.
                .Bookmark = .LastModified
               lngID = !RequestforQuotesID
                
                'Duplicate the related records: append query.
                If Me.[Request for Quotes Subform].Form.RecordsetClone.RecordCount > 0 Then
                    strSql = "INSERT INTO [Inventory Transactions] (RequestforquotesID, ProductID, UnitsOrderedConv) " & _
                        "SELECT " & lngID & " As requestforquotesID, ProductID, UnitsOrderedConv " & _
                        "FROM [Inventory Transactions] WHERE requestforquotesID = " & Me.RequestforQuotesID & ";"
                    DBEngine(0)(0).Execute strSql, dbFailOnError
                Else
                    MsgBox "Main record duplicated, but there were no related records."
                End If
                
                '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
    The button will create a new record, but it does not copy the info over from my selected record

  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,652
    What data type is that field (and what does the function above return)?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    Marianna_Air is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2012
    Posts
    56
    The data type in that field is "text" like RFQXXXXX
    The function produces a new record but without the original records info that I want to duplicate in a new record
    I get a run time error 13 "Type Mismatch"
    I was thinking that maybe it was looking for a long integer and not the text maybe??? If so I’m not sure how to fix that

  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,652
    You've declared the variable as Long, so putting a string in there will cause an error. Declare it as string (I'd change the prefix accordingly):


    Dim strID As String
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    Marianna_Air is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2012
    Posts
    56
    This code is still kicking my butt, I've tried everything. I'm getting an error of "too few parameters. Excpected 2." and debug highlites the code in red below
    Code:
    Private Sub Command169_Click()
    'On Error GoTo Err_Handler
        'Purpose:   Duplicate the main form record and related records in the subform.
        Dim strSql As String    'SQL statement.
        Dim strID As String      'Primary key value of the new 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
                    !EmployeeID = Me.EmployeeID
                    !RequestDate = Date
                    !RequestforQuotesID = GetNextSN("RFQ")
                    'etc for other fields.
                .Update
                
                'Save the primary key value, to use as the foreign key for the related records.
                .Bookmark = .LastModified
               strID = !RequestforQuotesID
                
                'Duplicate the related records: append query.
                If Me.[Request for Quotes Subform].Form.RecordsetClone.RecordCount > 0 Then
                    strSql = "INSERT INTO [Inventory Transactions] (RequestforquotesID, ProductID, UnitsOrderedConv) " & _
                        "SELECT " & strID & " As requestforquotesID, ProductID, UnitsOrderedConv " & _
                        "FROM [Inventory Transactions] WHERE requestforquotesID = " & Me.RequestforQuotesID & ";"
                    DBEngine(0)(0).Execute strSql, dbFailOnError
                Else
                    MsgBox "Main record duplicated, but there were no related records."
                End If
                
                '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

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    This is a valuable tool for debugging SQL in code:

    http://www.baldyweb.com/ImmediateWindow.htm

    Offhand, I suspect you need to surround that value in single quotes since it's text not numeric (in both places).
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    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
    You're really over-engineering this thing, I think. This is all you really need:

    Code:
    Private Sub CopyPartialRecordButton_Click()
    
    Dim MyFirstField As String
    Dim MySecondField As String
    Dim MyThirdField As String
    
    'Copy fields to variables
    MyFirstField = Me.FirstField
    MySecondField = Me.SecondField
    MyThirdField = Me.ThirdField
    
    'Go to a new record
    DoCmd.GoToRecord , , acNewRec
    
    'Reverse the process and plug old values into new record
    Me.FirstField = MyFirstField
    Me.SecondField = MySecondField
    Me.ThirdField = MyThirdField
    
    End Sub


    No need to worry about Datatypes. To leave the Vendor Field blank, simply omit it from the list of Controls to populate. You can save the PK to be used as the FK on related Records (not sure why you need to do that) if you need to, and this code will leave you sitting on the newly created, partially duplicated Record.

    Linq ;0)>

  10. #10
    Marianna_Air is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2012
    Posts
    56
    Sorry for the delay on this, I was put on another project for a few days. I like this idea but can't get it to work, it's probably something I've done incorrect. When I select the duplicate record button it does nothing

    Code:
    Private Sub CopyPartialRecordButton_Click()
    Dim MyemployeeID As String
    Dim MyOriginator As String
    Dim MyRequestforQuotesDescription As String
    Dim MyTransactionDescription As String
    Dim MyProductName As String
    Dim MyUnitsOrderedConv As String
     
    'Copy fields to variables
    MyemployeeID = Me.EmployeeID
    MyOriginator = Me.Originator
    MyRequestforQuotesDescription = Me.RequestforQuotesDescription
    MyTransactionDescription = Me.TransactionDescription
    MyProductName = Me.ProductName
    MyUnitsOrderedConv = Me.UnitsOrderedConv
    
    'Go to a new record
    DoCmd.GoToRecord , , acNewRec
    'Reverse the process and plug old values into new record
    Me.EmployeeID = MyemployeeID
    Me.Originator = MyOriginator
    Me.RequestforQuotesDescription = MyRequestforQuotesDescription
    Me.TransactionDescription = MyTransactionDescription
    Me.ProductName = MyProductName
    Me.UnitsOrderedConv = MyUnitsOrderedConv
    
    End Sub

  11. #11
    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
    Are you doing this from a New Record immediately after entering that Record? If so, you may need to force a Save before copying. Immediately prior to the line

    'Copy fields to variables

    enter

    DoCmd.RunCommand acCmdSaveRecord

    and see if that works. If it doesn't help, place this line in the same place

    Msgbox "Click event fired!"

    and see if the Messagebox shows when you click the button.

    Linq ;0)>

  12. #12
    Marianna_Air is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2012
    Posts
    56
    no msgbox, when I input that line

  13. #13
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    Did you try using the Debug.Print to the immediate window as Paul suggested?
    Have you tried stepping through the code with F8?

  14. #14
    Marianna_Air is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2012
    Posts
    56
    Yes I'm stepping through it now, and its hanging up when I come to the fields that are associated with the sub_form information
    Code:
    Private Sub CopyPartialRecordButton_Click()
    Dim MyemployeeID As String
    Dim MyOriginator As String
    Dim MyRequestforQuotesDescription As String
    Dim MyTransactionDescription As String
    Dim MyProductName As String
    Dim MyUnitsOrderedConv As String
    
    'Copy fields to variables
    MyemployeeID = Me.EmployeeID
    MyOriginator = Me.Originator
    MyRequestforQuotesDescription = Me.RequestforQuotesDescription
    MyTransactionDescription = Me.TransactionDescription
    MyProductName = Me.ProductName
    MyUnitsOrderedConv = Me.UnitsOrderedConv
    
    'Go to a new record
    DoCmd.GoToRecord , , acNewRec
    'Reverse the process and plug old values into new record
    Me.EmployeeID = MyemployeeID
    Me.Originator = MyOriginator
    Me.RequestforQuotesDescription = MyRequestforQuotesDescription
    Me.TransactionDescription = MyTransactionDescription
    Me.ProductName = MyProductName
    Me.UnitsOrderedConv = MyUnitsOrderedConv
    End Sub

  15. #15
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    Where is this button located?
    Are you getting an error message?
    Tell us more about your Forms set up .... parent , subform1...

    If you are referring to mainform with ME, then me can not refer to subform...

    see http://access.mvps.org/access/forms/frm0031.htm

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

Similar Threads

  1. Goto Record when Duplicate Record Exists
    By rlsublime in forum Programming
    Replies: 13
    Last Post: 03-22-2012, 03:46 PM
  2. Replies: 5
    Last Post: 03-02-2012, 08:58 PM
  3. Duplicate last record to next record in form
    By jdhaldane in forum Forms
    Replies: 4
    Last Post: 12-17-2010, 04:22 PM
  4. If duplicate record, go to original record
    By BigBear in forum Forms
    Replies: 2
    Last Post: 10-06-2010, 02:58 AM
  5. If duplicate record, go to original record
    By islandboy in forum Access
    Replies: 51
    Last Post: 06-02-2010, 04:17 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