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.
The Relationships diagram for the database is shown in Figure A:
Figure A. The Relationships diagram
And the tabbed form in Figures B and C:
Figure B. The General Data tab of the Employees form
Figure C. The Confidential Data tab of the Employees form
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:
Private Sub cmdCopyRecord_Click()
On Error GoTo ErrorHandler
Dim intReturn As Integer
Dim lngID As Long
Dim lngNewID As Long
Dim strPrompt As String
Dim strTitle As String
'Get current ID value
lngID = Nz(Me![EmployeeID])
'Get new ID value (can be replaced with another calculation, as needed)
lngNewID = DMax("[EmployeeID]", "tblEmployeesCalcID") + 1
Debug.Print "New ID: " & lngNewID
strTitle = "Question"
strPrompt = "Copy Employee ID " & lngID & " to a new record?"
intReturn = MsgBox(prompt:=strPrompt, _
Buttons:=vbQuestion + vbYesNo, _
Title:=strTitle)
If intReturn = vbNo Then
GoTo ErrorHandlerExit
End If
If CopyRecordCalcID(lngID, lngNewID) = True Then
strPrompt = "Copy of Employee ID " & lngID _
& " to Employee ID" & lngNewID & " successful"
Me.Requery
DoCmd.GoToRecord record:=acLast
Else
strPrompt = "Copy of Employee ID " & lngID _
& " to Project #" & lngNewID & " not successful"
End If
MsgBox prompt:=strPrompt, _
Buttons:=vbInformation + vbOKOnly, _
Title:=strTitle
ErrorHandlerExit:
Exit Sub
ErrorHandler:
MsgBox "Error No: " & Err.Number _
& " in " & Me.ActiveControl.Name & " procedure; " _
& "Description: " & Err.Description
Resume ErrorHandlerExit
End Sub
The two following procedures in the basUtilties module are called in turn to do the copying:
Public Function CopyRecordCalcID(lngID As Long, lngNewID As Long) _
As Boolean
On Error GoTo ErrorHandler
If CopyTableCalcID("tblEmployeesCalcID", lngID, lngNewID) _
= False Then
CopyRecordCalcID = False
GoTo ErrorHandlerExit
End If
If CopyTableCalcID("tblConfidentalDataCalcID", lngID, lngNewID) _
= False Then
CopyRecordCalcID = False
GoTo ErrorHandlerExit
End If
CopyRecordCalcID = True
ErrorHandlerExit:
Exit Function
ErrorHandler:
MsgBox "Error No: " & Err.Number _
& " in CopyRecordCalcID procedure; " _
& "Description: " & Err.Description
Resume ErrorHandlerExit
End Function
Public Function CopyTableCalcID(strTable As String, _
lngID As Long, lngNewID As Long) As Boolean
On Error GoTo ErrorHandler
Dim intCounter As Integer
Dim lngFieldCount As Long
Dim rstNoCopy As DAO.Recordset
Dim rstSource As DAO.Recordset
Dim rstTarget As DAO.Recordset
Dim strField As String
Dim strNoCopyTable As String
Dim strSearch As String
Dim varData As Variant
CopyTableCalcID = False
Debug.Print "Creating a new record in " & strTable
strNoCopyTable = "tlkpNoCopyFields"
Set rstSource = CurrentDb.OpenRecordset(strTable, dbOpenDynaset)
Set rstTarget = CurrentDb.OpenRecordset(strTable, dbOpenDynaset)
Set rstNoCopy = CurrentDb.OpenRecordset(strNoCopyTable, dbOpenDynaset)
lngFieldCount = rstSource.Fields.Count
Debug.Print lngFieldCount & " fields in " & strTable
strSearch = "[EmployeeID] = " & lngID
Debug.Print "Search string: " & strSearch
rstSource.FindFirst strSearch
'Iterate through table fields, copying values from each
'one that has data to a new record (except for fields that
'should not be copied)
rstTarget.AddNew
For intCounter = 0 To lngFieldCount - 1
strField = rstSource.Fields(intCounter).Name
'Debug.Print "Field name: " & strField _
& " (Field No. " & intCounter & ")"
'Search for field name in No Copy table
strSearch = "[FieldName] = " & Chr(39) & strField & Chr(39)
'Debug.Print "Search string: " & strSearch
rstNoCopy.MoveFirst
rstNoCopy.FindFirst strSearch
If strField = "EmployeeID" Then
rstTarget.Fields(intCounter) = lngNewID
ElseIf rstNoCopy.NoMatch = True Then
varData = rstSource.Fields(intCounter)
'Debug.Print "Field data: " & varData
If IsNull(varData) = False And varData <> "" Then
rstTarget.Fields(intCounter) = varData
Debug.Print strField & " in " & strTable & " copied"
End If
Else
'Field should not be copied
Debug.Print strField & " in " & strTable & " not copied"
End If
NextField:
Next intCounter
rstTarget.Update
CopyTableCalcID = True
ErrorHandlerExit:
Exit Function
ErrorHandler:
MsgBox "Error No: " & Err.Number _
& " in CopyTableWithExceptions procedure; " _
& "Description: " & Err.Description
Resume ErrorHandlerExit
End Function
References
The code in the sample database needs the following references (in addition to the default references):
Microsoft Word 14.0 Object Library (for SortDeclarations procedure only)
Microsoft Forms 2.0 Object Library (for SortDeclarations procedure only)
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) | Wherever you want |