Try this:
Code:
=====================================
Function fCopyRecord(strTable As String, varPKVal) As Long
'Copies a record in a specified table
'Accepts table name and Primary key value.
'Currently assumes a simplistic single PK field
'The fairly generic function below will add a copy of a record to the
same
table
'based on an existing PK value and return the PK value of the new
record.
'You could then use similar code to create the child records.
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim tdf As DAO.TableDef
Dim idx As DAO.Index
Dim fld As DAO.Field
Dim strPKName As String
Dim strFields As String
Set db = CurrentDb
Set tdf = db(strTable)
For Each idx In tdf.Indexes
If idx.Primary Then
strPKName = idx.Fields(0).Name
Exit For
End If
Next
For Each fld In tdf.Fields
If fld.Name <> strPKName Then
strFields = strFields & ",[" & fld.Name & "]"
End If
Next
strFields = Mid(strFields, 2)
Set qdf = db.CreateQueryDef("")
qdf.SQL = "INSERT INTO [" & strTable & "] (" & strFields & ") " & _
"SELECT " & strFields & " FROM [" & strTable & "] " & _
"WHERE [" & strPKName & "]= " & varPKVal
qdf.Execute
If qdf.RecordsAffected > 0 Then
With db.OpenRecordset("SELECT @@Identity")
fCopyRecord = .Fields(0)
.Close
End With
End If
Set db = Nothing
Set qdf = Nothing
Set tdf = Nothing
Set idx = Nothing
Set fld = Nothing
End Function
=====================================
Good Luck,
PC