Results 1 to 3 of 3
  1. #1
    mar7632 is offline Advanced Beginner
    Windows 8 Access 2016
    Join Date
    Feb 2019
    Location
    Spain - Madrid
    Posts
    72

    copy a certain record into a new one in the same form.

    Hi again,

    In my form I have one important field called "Reference". Usually, this value is unique but there are sometimes that we receive the same reference with almost the same data, Only there are 2 or 3 fields which are different.

    So, In order to avoid timeconsuming copying again all the fields from another record. Is it possible to to build a button which selects a certain reference and copy all the data again and change the fields which have to be changed?



    Thank you in advance.

  2. #2
    alansidman's Avatar
    alansidman is offline Indifferent
    Windows 10 Access 2016
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,538
    Maybe something like 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
    mar7632 is offline Advanced Beginner
    Windows 8 Access 2016
    Join Date
    Feb 2019
    Location
    Spain - Madrid
    Posts
    72
    Thank you for your reply alansidman.

    I suppose that this code localize the current record you are and then copy all the data to a new one. right?

    Hello everyone, I have found another way to perform this:

    Code:
    Private Sub cmd_copyrecord_Click()
        Dim currentID As Long
        
        'TO DO: change all instances of 'BookID' with the actual name of your table's ID or primary key
        
        If IsNull(BookID) Then
            MsgBox prompt:="Please select the record to copy first.", buttons:=vbExclamation
            Exit Sub
        End If
        
        currentID = BookID
        DoCmd.GoToRecord record:=acNewRec
        
        'TO DO: set the fields to be copied (those that most likely will have the same values)
        'FORMAT: fieldName = Dlookup("fieldname", "tableName", "primaryKeyField=" & currentID)
        
        Author = DLookup("Author", "Books", "BookID=" & currentID)
        Country = DLookup("Country", "Books", "BookID=" & currentID)
        Language = DLookup("Language", "Books", "BookID=" & currentID)
        Genre = DLookup("Genre", "Books", "BookID=" & currentID)
        Publisher = DLookup("Publisher", "Books", "BookID=" & currentID)
        
        Title.SetFocus      'TO DO: change 'Title' with name of field that is going to be edited by the user
        
    End Sub
    this is the link where I found the information: https://www.datanumen.com/blogs/quic...ecords-access/

    Thank you all.
    Last edited by mar7632; 04-29-2019 at 05:10 AM. Reason: new information

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

Similar Threads

  1. VBA in Form to copy record
    By jlgray0127 in forum Forms
    Replies: 2
    Last Post: 05-17-2018, 08:30 AM
  2. Copy Record button on form
    By banpreet in forum Forms
    Replies: 4
    Last Post: 12-15-2016, 04:44 PM
  3. Replies: 2
    Last Post: 09-17-2014, 04:55 PM
  4. Copy record into pop-up form
    By arothacker in forum Forms
    Replies: 5
    Last Post: 03-27-2014, 12:23 PM
  5. Replies: 3
    Last Post: 03-09-2013, 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