by Helen Fedemma

Introduction

Back in 2004, I wrote an Access Archon article (Copying Records and Linked Records) that described a method for copying a record with possible linked data in another table, using an array based on form controls. This article describes a different approach, based on table fields rather than form controls, and also shows how to deal with creating a new record in another table, linked one-to-one with the main table.

The Copying Technique

In my older article, the fields to be copied were picked up from controls on a form, skipping those with “No Copy” in their Tag properties. This technique worked well for a form based on a single table, but not so well when the form is based on a query that includes two or more tables. In the case that inspired this article, a main form was based on a query that included two tables joined one-to-one, and it had a subform based on another table, also linked one-to-one to the main table.

In some cases, the one-to-one table is split off from the main table because the number of fields needed exceeds the limit for an Access table; a one-to-one join might also be needed to make some information confidential. In either case, when a record in the main table is copied, it is also necessary to make new records in the other table(s), so that is what my code does.

The sample database, Copy Record with Calc ID.accdb, has a table of employees which will be familiar to old Access hands, since it dates from early versions of the sample Northwind database. I added a table of financial data, linked to tblEmployeesCalcID one-to-one. The technique used to do the copying in this instance uses a table called tlkpNoCopyFields instead of the Tag property of a control to indicate which fields should not be copied.

VBA Code

There are several ways that the new ID field can be determined – in this sample database, the new EmployeeID value is simply 1 higher than the highest current ID (the next Access Archon will deal with the case of an AutoNumber ID field). The calculation could be more complicated, for example creating a Text ID value based on date or time elements, or it could be manually entered using an InputBox. In any case, once the current and new ID values have been determined, the procedures that do the actual copying can be called.

The first procedure is called from the Copy Record button on frmEmployeesCalcID:

If you import code or objects into a database of your own, you may need to set one or more of these references. The version number may differ, depending on your Office version; check the version you have. References are set in the References dialog, opened from the VBA window. For more information on working with references, see Access Archon #107, Working with References.

Supporting Files

The zip file containing this article, in Word 2007-2010 format, plus the supporting file(s), may be downloaded from the Access Archon page of my Web site, as accarch249.zip, which is the last entry in the table of Access Archon columns for Access Watch.

Document Name

Document Type

Place in

Copy Record with Calc ID.accdb

Access 2007-2010 database (can also be used in higher versions of Access)