Results 1 to 6 of 6
  1. #1
    klnlsu is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Apr 2015
    Posts
    107

    Copy a record and open in form

    I have a record open in a form. I would like to copy that record into the same linked Access table and then open that copied record in another form. What is the most efficient way to accomplish this in VBA code? I know I am being kind of vague, but I'm not sure what details I might need to add. Let me know.



    Thanks.

  2. #2
    jwhite is offline Competent Performer
    Windows 10 Access 2013 32bit
    Join Date
    Dec 2012
    Location
    North Carolina
    Posts
    349
    Run an Update query using Currentdb.Execute that will fetch a record from the table that matches the primary key of the record you are viewing (WHERE YourPrimaryKeyField = YourPrimaryKeyBeingViewed) and add the new record, then open a recordset using @@IDENTITY to get the PK of the record just added, and finally filter/refresh the form to view the record for PK just added. See: http://stackoverflow.com/questions/8...msaccess-query

  3. #3
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    You'd use the keys on the 2 forms to run an append query...
    Code:
    sub btnCopy_click()
    Docmd.openQuery  "qaCopyData"
    end sub
    The query would use the key on the form as the source ,but user must enter the key from the target.
    there could be any # of forms open so the app would not know which to use,so the user should enter the target key by hand.
    the query will ask for the target id as a param.....

    insert into table (ID, field2) select [Enter Target ID] , field2 from table where [id] = forms!myForm!txtID

  4. #4
    klnlsu is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Apr 2015
    Posts
    107
    I have tried using the INSERT INTO command using the following code:
    Dim db As Database
    Set db = CurrentDb
    db.Execute "INSERT INTO tblOEOrderHeader SELECT * FROM tblOEOrderHeader WHERE OrderHeaderID = OrderHeaderIDTxt", dbFailOnError

    OrderHeaderID is the autonumber field of the record and OrderHeaderIDTxt is the textbox on the form that holds the autonumber field of the record I am trying to duplicate. I have tried the command with and without the "dbFailOnError" parameter.

    I get this error message:
    "Too few parameters. Expected 1."

  5. #5
    jwhite is offline Competent Performer
    Windows 10 Access 2013 32bit
    Join Date
    Dec 2012
    Location
    North Carolina
    Posts
    349
    I presume your OrderHeaderID is numeric, therefore:
    db.Execute "INSERT INTO tblOEOrderHeader SELECT * FROM tblOEOrderHeader WHERE OrderHeaderID = " & Me.OrderHeaderIDTxt, dbFailOnError

    But I wouldn't use * - I would list the fields.

  6. #6
    klnlsu is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Apr 2015
    Posts
    107
    Thank you for correcting my syntax. That worked.

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

Similar Threads

  1. Copy Record button on form
    By banpreet in forum Forms
    Replies: 4
    Last Post: 12-15-2016, 04:44 PM
  2. Replies: 2
    Last Post: 09-17-2014, 04:55 PM
  3. Copy record into pop-up form
    By arothacker in forum Forms
    Replies: 5
    Last Post: 03-27-2014, 12:23 PM
  4. Replies: 3
    Last Post: 03-09-2013, 10:39 AM
  5. Replies: 1
    Last Post: 05-03-2012, 02:25 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