P
PC User
I have a functional function that can duplicate a record so that I can
edit a specific field and not have to enter all the data for all the
fields in a new record. However, now I have a table that is in a one-
to-many relationship with another table and this function will only
duplicate the "one' table. As for the "many" table, I'm looking for
ideas to approach this problem or maybe someone already has a function
that can do this. The function that I have working for the "one"
table is as follows:
===================================================
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
===================================================
Any help on this will be appreciated.
Thanks,
PC
edit a specific field and not have to enter all the data for all the
fields in a new record. However, now I have a table that is in a one-
to-many relationship with another table and this function will only
duplicate the "one' table. As for the "many" table, I'm looking for
ideas to approach this problem or maybe someone already has a function
that can do this. The function that I have working for the "one"
table is as follows:
===================================================
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
===================================================
Any help on this will be appreciated.
Thanks,
PC