locked
Copy field values from Parent to Child RRS feed

  • Question

  • Hi, I am new to LightSwitch2012 and have some basic knowledge of VB. I am trying to copy values from a parent screen to a child but can't figure it out.

    I have two related entities as follows:

    Business

         Id, Name, Address1, Addresss2, Town, County, Postcode

    Site

         Id, Name, Address1, Addresss2, Town, County, Postcode

    A site must be have a business.

    I have created a New Data Screen which allows for new business details (parent) to be entered and also site details (child). In many cases the Site address is the same as the Business address so was wondering if there is a way to copy the values in the business fields to the site fields, either by clicking a button called "copy address" or a tick box? Whichever is the best way?

    Any advice you can give would be much appreciated.

    Thanks

    Sunday, January 27, 2013 6:05 PM

Answers

  • The code you have there will add 8 new records. All you need is:

    
    
    Private Sub CopyDetails_Execute()
        dim newSite = Me.Sites.AddNew
    
        newSite.Address1 = Me.BusinessProperty.Address1
        newSite.Address2 = Me.BusinessProperty.Address2
        newSite.Town = Me.BusinessProperty.Town
        newSite.County = Me.BusinessProperty.County
        newSite.Postcode = Me.BusinessProperty.Postcode
        newSite.AddNew.Telephone = Me.BusinessProperty.Telephone
        newSite.Fax = Me.BusinessProperty.Fax
        newSite.Email = Me.BusinessProperty.Email
    End Sub
    

    Or something we can do in VB that C# can't:

    Private Sub CopyDetails_Execute()
        dim newSite = Me.Sites.AddNew
    
        With newSite
            .Address1 = Me.BusinessProperty.Address1
            .Address2 = Me.BusinessProperty.Address2
            .Town = Me.BusinessProperty.Town
            .County = Me.BusinessProperty.County
            .Postcode = Me.BusinessProperty.Postcode
            .AddNew.Telephone = Me.BusinessProperty.Telephone
            .Fax = Me.BusinessProperty.Fax
            .Email = Me.BusinessProperty.Email
        End With
    End Sub

    I hope that answers you question?

    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    Please click "Mark as Answer" if a reply answers your question. Please click "Vote as Helpful" , if you find a reply helpful.
     
    By doing this you'll help others to find answers faster.


    • Edited by Yann DuranModerator Friday, February 8, 2013 12:00 AM Fixed typo
    • Marked as answer by jcsf1 Friday, February 8, 2013 7:36 PM
    Thursday, February 7, 2013 11:59 PM
    Moderator

All replies

  • Monday, January 28, 2013 9:40 AM
  • Hi icsf

    I will mark John's reply as answer, if the marked answer is not helpful to solve your problem, please unmark and give quick response.

    With kind regards


    Angie xu
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, February 5, 2013 1:57 AM
  • Thanks for your reply and sorry for the delay.

    The example in the video is referencing another row in the same (child) screen.  The part I get stuck on is how to reference a property/value in the parent screen from the child.

    I have tried another method (see below) which is executed when a button is clicked.  This runs and successfully references the correct field data but creates a new row for each property (I am guessing this is to do with the AddNew command) Is there and "Edit" command that could be used to replace the AddNew command on all but the first line of code so that a new row is created and then populated with subsequent values?

     Private Sub CopyDetails_Execute()
                ' Write your code here.
                Me.Sites.AddNew.Address1 = Me.BusinessProperty.Address1
                Me.Sites.AddNew.Address2 = Me.BusinessProperty.Address2
                Me.Sites.AddNew.Town = Me.BusinessProperty.Town
                Me.Sites.AddNew.County = Me.BusinessProperty.County
                Me.Sites.AddNew.Postcode = Me.BusinessProperty.Postcode
                Me.Sites.AddNew.Telephone = Me.BusinessProperty.Telephone
                Me.Sites.AddNew.Fax = Me.BusinessProperty.Fax
                Me.Sites.AddNew.Email = Me.BusinessProperty.Email
            End Sub


    • Edited by jcsf1 Thursday, February 7, 2013 8:50 PM
    Thursday, February 7, 2013 8:49 PM
  • The code you have there will add 8 new records. All you need is:

    
    
    Private Sub CopyDetails_Execute()
        dim newSite = Me.Sites.AddNew
    
        newSite.Address1 = Me.BusinessProperty.Address1
        newSite.Address2 = Me.BusinessProperty.Address2
        newSite.Town = Me.BusinessProperty.Town
        newSite.County = Me.BusinessProperty.County
        newSite.Postcode = Me.BusinessProperty.Postcode
        newSite.AddNew.Telephone = Me.BusinessProperty.Telephone
        newSite.Fax = Me.BusinessProperty.Fax
        newSite.Email = Me.BusinessProperty.Email
    End Sub
    

    Or something we can do in VB that C# can't:

    Private Sub CopyDetails_Execute()
        dim newSite = Me.Sites.AddNew
    
        With newSite
            .Address1 = Me.BusinessProperty.Address1
            .Address2 = Me.BusinessProperty.Address2
            .Town = Me.BusinessProperty.Town
            .County = Me.BusinessProperty.County
            .Postcode = Me.BusinessProperty.Postcode
            .AddNew.Telephone = Me.BusinessProperty.Telephone
            .Fax = Me.BusinessProperty.Fax
            .Email = Me.BusinessProperty.Email
        End With
    End Sub

    I hope that answers you question?

    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    Please click "Mark as Answer" if a reply answers your question. Please click "Vote as Helpful" , if you find a reply helpful.
     
    By doing this you'll help others to find answers faster.


    • Edited by Yann DuranModerator Friday, February 8, 2013 12:00 AM Fixed typo
    • Marked as answer by jcsf1 Friday, February 8, 2013 7:36 PM
    Thursday, February 7, 2013 11:59 PM
    Moderator
  • Thank you Yann, that works perfectly!

    Friday, February 8, 2013 7:36 PM
  • You're welcome! :-)

    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    Please click "Mark as Answer" if a reply answers your question. Please click "Vote as Helpful" , if you find a reply helpful.
     
    By doing this you'll help others to find answers faster.

    Sunday, February 10, 2013 2:10 PM
    Moderator