Solved

Generic Function to Copy Record changing value of Foreign Key

Posted on 2004-09-08
2
183 Views
Last Modified: 2012-08-14
Need a generic function that can be used to append a copy of the current record in a recordset.  All fields will be given the same values as the source record except for the foreign or parent key value.  The table also has an autonumber field, so this field cannot be copied.

0
Comment
Question by:hertzgordman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 4

Accepted Solution

by:
Excalibur_Software earned 250 total points
ID: 12012950
0
 

Author Comment

by:hertzgordman
ID: 12017194
Thanks, but don't see what I need there.

I need to do this from code, specified a new parent key.

This is the code I have worked out so far, but I am not sure if it is the best approach:

Public Function CopyRecord(SourceTable As String, KeyField As Variant, ParentKeyField As String, ParentKeyFilter As Long, ParentKeyNewValue As Variant)
                                       
  On Error GoTo err_CopyRecord


    Dim MyDb As Database
    Dim MySql As String
   
    Dim rsSource As Recordset
    Dim rsTarget As Recordset
    Dim fooBar As Variant
    Dim FieldNo As Integer
    Dim FieldName As String
    Dim SourceFieldValue As Variant
   
    Dim NoFields As Integer
    Dim i As Integer
   
    Set MyDb = CurrentDb
   
    'Source
    MySql = "Select " & SourceTable & ".*" _
             & " FROM " & SourceTable _
             & " WHERE (((" & SourceTable & "." & ParentKeyField & ")=" & CStr(ParentKeyFilter) & "));"
             Debug.Print MySql
             
           
    Set rsSource = MyDb.OpenRecordset(MySql)
    Set rsTarget = MyDb.OpenRecordset(MySql)
   
    If rsSource.EOF Then
       
        'MsgBox ("No data to copy")
       
        rsSource.Close
        rsTarget.Close
        MyDb.Close
       
        Exit Function
   
       
    End If
   
   
    rsTarget.AddNew
   
        NoFields = rsTarget.Fields.Count
        For i = 0 To (NoFields - 1)
       
            FieldNo = i
            FieldName = rsTarget(i).Name
            SourceFieldValue = rsSource(i).Value
           
           
            If FieldName <> KeyField Or KeyField = "NA" Then  'Do not update key field
           
                If FieldName = ParentKeyField Then
                   
                    rsTarget(i).Value = ParentKeyNewValue
               
                Else
               
                    rsTarget(i).Value = SourceFieldValue
               
                End If
           
           
            End If
           
        Next i
       
                 
             
    rsTarget.Update
                   
       
    rsSource.Close
    rsTarget.Close
    MyDb.Close
   
    Exit Function
   
err_CopyRecord:
   
    If Err.Number = 3021 Then
        MsgBox ("No current record")
        Resume Next
    ElseIf Err.Number = 3022 Then
        Resume Next
    ElseIf Err.Number > 0 Then
       
        MsgBox (Err.Number & " " & Err.Description)
   
        Exit Function
    End If
   


End Function

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question