none
Copy parent/child records

    Question

  • I know that there are a couple of threads that talk about this, but the "answer" code wasn't all too helpful for a n00b like me. 

    I would like to make a button that, when clicked, copies the selected item and its child records into a new entry. I know this can be done, I am just not sure how to do it myself. If anyone could show me an example in code (VB/English, please) and explain a little, I would appreciate it very much. I have no experience manually creating records (let alone child records), so that is what I need the most help with. 

    p.s. I am looking to learn, not to copy. I am really hoping that someone will explain the commands a little bit. Thanks!


    MessageBox.Show("Hello, world.")

    Friday, June 15, 2012 7:18 PM

Answers

  • I figured out what the issue was- I was messing everything up with the line "newGSDetail.Greensheet = gsDetails.Greensheet"

    Here is the complete functional code that copies a parent record, copies all of the child records, and then opens a modal window to edit the details of the parent record (to save it under a new work order number, etc.):

            Private Sub CopyGreensheet_Execute()
                ' Create new greensheet and record the selected item as the old greensheet to be copied.
                Dim newGreensheet As Greensheet = New Greensheet()
                Dim oldGreensheet = Me.Greensheet.SelectedItem
    
                newGreensheet.DrawingNumber = oldGreensheet.DrawingNumber
                newGreensheet.DateEntered = Date.Today.ToShortDateString()
    
                ' Step through the details of the old greensheet and copy the values over to the new one.
                For Each gsDetails As GreensheetDetails In oldGreensheet.GreensheetDetails
                    Dim newGSDetail As GreensheetDetails = newGreensheet.GreensheetDetails.AddNew()
    
                    newGSDetail.Part = gsDetails.Part
                    newGSDetail.QtyEa = gsDetails.QtyEa
                    newGSDetail.Description = gsDetails.Description
                    ' [etc, etc]
                Next
    
                ' Open a modal window to edit the work order information for the new parent record.
                Me.Greensheet.SelectedItem = newGreensheet
                Me.OpenModalWindow("GS_Edit")
            End Sub


    MessageBox.Show("Hello, world.")



    • Marked as answer by c4ctopus Monday, June 18, 2012 1:17 PM
    • Edited by c4ctopus Monday, June 18, 2012 1:25 PM
    Monday, June 18, 2012 1:17 PM

All replies

  • Hi

    Please go through the below threads to achieve your requirement.

    http://social.msdn.microsoft.com/Forums/en-US/lightswitch/thread/e746cee8-8439-4b28-a012-8dd2e89d3e71

    http://social.msdn.microsoft.com/Forums/bs-Cyrl-BA/lightswitch/thread/a1a7a894-5ff4-415e-a915-f9e344525864

    Hope this help

    Regards..


    Rashmi Ranjan Panigrahi
    www.lightswitchspecial.blogspot.in

    If you found this post helpful, please “Vote as Helpful”. If it answered your question, please “Mark as Answer”.
    This will help other users to find their answer quickly.

    Friday, June 15, 2012 10:09 PM
  • I am using the first link you gave me as a reference. I have very similar code. I can successfully make a parent record and copy values from the old parent record to the new. But I am unsuccessful when it comes to transferring the child records from the old entry to the new one. Here is the code that I have so far:

            Private Sub CopyGreensheet_Execute()
                Dim newGreensheet As Greensheet = New Greensheet()
                Dim oldGreensheet = Me.Greensheet.SelectedItem
                newGreensheet.DrawingNumber = Me.Greensheet.SelectedItem.DrawingNumber
                newGreensheet.DateEntered = Date.Today.ToShortDateString()
    
                For Each gsDetails As GreensheetDetails In oldGreensheet.GreensheetDetails
                    Dim newGSDetail As GreensheetDetails = newGreensheet.GreensheetDetails.AddNew()
                    newGSDetail.Greensheet = gsDetails.Greensheet
                    newGSDetail.Part = gsDetails.Part
                    newGSDetail.Description = gsDetails.Description
                Next
    
                Me.Greensheet.SelectedItem = newGreensheet
                Me.OpenModalWindow("GS_Edit")
            End Sub
    As you can see, I am trying to create a new parent record, copy all the child records into the new one, and open a modal window to edit the details of the parent record before saving. Everything works fine except that the new child records are going right back into the old parent record instead of being copied into the new one. Not sure what to do here.


    MessageBox.Show("Hello, world.")


    • Edited by c4ctopus Monday, June 18, 2012 12:49 PM
    Monday, June 18, 2012 12:39 PM
  • I figured out what the issue was- I was messing everything up with the line "newGSDetail.Greensheet = gsDetails.Greensheet"

    Here is the complete functional code that copies a parent record, copies all of the child records, and then opens a modal window to edit the details of the parent record (to save it under a new work order number, etc.):

            Private Sub CopyGreensheet_Execute()
                ' Create new greensheet and record the selected item as the old greensheet to be copied.
                Dim newGreensheet As Greensheet = New Greensheet()
                Dim oldGreensheet = Me.Greensheet.SelectedItem
    
                newGreensheet.DrawingNumber = oldGreensheet.DrawingNumber
                newGreensheet.DateEntered = Date.Today.ToShortDateString()
    
                ' Step through the details of the old greensheet and copy the values over to the new one.
                For Each gsDetails As GreensheetDetails In oldGreensheet.GreensheetDetails
                    Dim newGSDetail As GreensheetDetails = newGreensheet.GreensheetDetails.AddNew()
    
                    newGSDetail.Part = gsDetails.Part
                    newGSDetail.QtyEa = gsDetails.QtyEa
                    newGSDetail.Description = gsDetails.Description
                    ' [etc, etc]
                Next
    
                ' Open a modal window to edit the work order information for the new parent record.
                Me.Greensheet.SelectedItem = newGreensheet
                Me.OpenModalWindow("GS_Edit")
            End Sub


    MessageBox.Show("Hello, world.")



    • Marked as answer by c4ctopus Monday, June 18, 2012 1:17 PM
    • Edited by c4ctopus Monday, June 18, 2012 1:25 PM
    Monday, June 18, 2012 1:17 PM