Results 1 to 3 of 3
  1. #1
    RaycoQA is offline Novice
    Windows 10 Access 2007
    Join Date
    Jun 2017
    Location
    Indianapolis
    Posts
    1

    Cloning Record on Form/Subform

    Access 2007:
    I am trying to set up "Cloning" on a form so that we don't have to enter all info every time when most of it is the same.
    I have the main form [frmRFQInput] linked to [tblRFQ]:
    [RFQNumber]
    [RFQDate]
    [RFQDueDate]
    [Supplier]
    [BuyerName]
    [Notes]

    and a subform [frmRFQInputsubform]linked to [tblRFQItems]:
    [ID]......(linked to RFQNumber)
    [PartNumber]
    [Revision]
    [Material]


    [ProcessDescription]
    [Qty1]
    [Qty2]
    [Qty3]
    [Notes]


    I am still getting an error in the code and I am not sure where I have gone wrong.
    It still stops at .Update with the following error:


    Run-time error '3058':
    Index or primary key cannot contain a Null value.
    Code:
    Private Sub Command35_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 ldslD 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
    Supplier = "Enter Supplier"
    'etc for other fields.
    .Update
    'Save the primary key value, to use as the foreign key for the related records.
    .Bookmark = LastModified
         ID = !RFQNumber
    'Duplicate the related RFQ records: append query
    If Me.[frmRFQInputsubform].Form.RecordsetClone.RecordCount > 0 Then
    strSql = "INSERT INTO [tblRFQItems] (PartNumber, Revision, Material, ProcessDescription, Qty1, Qty2, Qty3, Notes) " & _
    "SELECT" & ID & " As NewID, PartNumber,  Revision, Material, ProcessDescription, Qty1, Qty2, Qty3, Notes" & _
    "FROM [tblRFQItems] WHERE ID = " & Me.RFQNumber & ";"
    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, , "Command35_Click"
    Resume Exit_Handler
    End Sub

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Need a space after SELECT:

    "SELECT " & ID &

    and after Notes:

    Notes " & _


    An alternative is VBA to set the DefaultValue property of each control. Fairly common topic. Review https://www.accessforums.net/showthread.php?t=52168
    Last edited by June7; 06-30-2017 at 09:48 AM.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,977
    For info, using a continuous form / datasheet, you can also copy the contents of the previous record by clicking Ctrl+'
    This will only copy the contents of one field at a time - the field you have currently selected

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

Similar Threads

  1. Replies: 1
    Last Post: 03-16-2017, 10:16 AM
  2. Replies: 7
    Last Post: 02-18-2015, 12:07 PM
  3. Replies: 4
    Last Post: 11-06-2014, 05:35 AM
  4. Replies: 6
    Last Post: 08-22-2012, 03:24 AM
  5. Cloning data to other existing records.
    By GraemeG in forum Programming
    Replies: 0
    Last Post: 02-16-2011, 11:53 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