Alpha Software Mobile Development Tools:   Alpha Anywhere    |   Alpha TransForm subscribe to our YouTube Channel  Follow Us on LinkedIn  Follow Us on Twitter  Follow Us on Facebook

Announcement

Collapse

The Alpha Software Forum Participation Guidelines

The Alpha Software Forum is a free forum created for Alpha Software Developer Community to ask for help, exchange ideas, and share solutions. Alpha Software strives to create an environment where all members of the community can feel safe to participate. In order to ensure the Alpha Software Forum is a place where all feel welcome, forum participants are expected to behave as follows:
  • Be professional in your conduct
  • Be kind to others
  • Be constructive when giving feedback
  • Be open to new ideas and suggestions
  • Stay on topic


Be sure all comments and threads you post are respectful. Posts that contain any of the following content will be considered a violation of your agreement as a member of the Alpha Software Forum Community and will be moderated:
  • Spam.
  • Vulgar language.
  • Quotes from private conversations without permission, including pricing and other sales related discussions.
  • Personal attacks, insults, or subtle put-downs.
  • Harassment, bullying, threatening, mocking, shaming, or deriding anyone.
  • Sexist, racist, homophobic, transphobic, ableist, or otherwise discriminatory jokes and language.
  • Sexually explicit or violent material, links, or language.
  • Pirated, hacked, or copyright-infringing material.
  • Encouraging of others to engage in the above behaviors.


If a thread or post is found to contain any of the content outlined above, a moderator may choose to take one of the following actions:
  • Remove the Post or Thread - the content is removed from the forum.
  • Place the User in Moderation - all posts and new threads must be approved by a moderator before they are posted.
  • Temporarily Ban the User - user is banned from forum for a period of time.
  • Permanently Ban the User - user is permanently banned from the forum.


Moderators may also rename posts and threads if they are too generic or do not property reflect the content.

Moderators may move threads if they have been posted in the incorrect forum.

Threads/Posts questioning specific moderator decisions or actions (such as "why was a user banned?") are not allowed and will be removed.

The owners of Alpha Software Corporation (Forum Owner) reserve the right to remove, edit, move, or close any thread for any reason; or ban any forum member without notice, reason, or explanation.

Community members are encouraged to click the "Report Post" icon in the lower left of a given post if they feel the post is in violation of the rules. This will alert the Moderators to take a look.

Alpha Software Corporation may amend the guidelines from time to time and may also vary the procedures it sets out where appropriate in a particular case. Your agreement to comply with the guidelines will be deemed agreement to any changes to it.



Bonus TIPS for Successful Posting

Try a Search First
It is highly recommended that a Search be done on your topic before posting, as many questions have been answered in prior posts. As with any search engine, the shorter the search term, the more "hits" will be returned, but the more specific the search term is, the greater the relevance of those "hits". Searching for "table" might well return every message on the board while "tablesum" would greatly restrict the number of messages returned.

When you do post
First, make sure you are posting your question in the correct forum. For example, if you post an issue regarding Desktop applications on the Mobile & Browser Applications board , not only will your question not be seen by the appropriate audience, it may also be removed or relocated.

The more detail you provide about your problem or question, the more likely someone is to understand your request and be able to help. A sample database with a minimum of records (and its support files, zipped together) will make it much easier to diagnose issues with your application. Screen shots of error messages are especially helpful.

When explaining how to reproduce your problem, please be as detailed as possible. Describe every step, click-by-click and keypress-by-keypress. Otherwise when others try to duplicate your problem, they may do something slightly different and end up with different results.

A note about attachments
You may only attach one file to each message. Attachment file size is limited to 2MB. If you need to include several files, you may do so by zipping them into a single archive.

If you forgot to attach your files to your post, please do NOT create a new thread. Instead, reply to your original message and attach the file there.

When attaching screen shots, it is best to attach an image file (.BMP, .JPG, .GIF, .PNG, etc.) or a zip file of several images, as opposed to a Word document containing the screen shots. Because Word documents are prone to viruses, many message board users will not open your Word file, therefore limiting their ability to help you.

Similarly, if you are uploading a zipped archive, you should simply create a .ZIP file and not a self-extracting .EXE as many users will not run your EXE file.
See more
See less

Copying parent and children

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Copying parent and children

    I am struggling trying to copy a parent and associated children into a new record.
    My parent table is "quotation_base" and the first field is "quote_number". My child table is "quote_items" and the first field is "quote_number", there is a one-to-many relationship and I have referential integrity turned on.

    I have an xdialog popup and one of the buttons is "NO", when this is selected I need to create a copy of a previous quote but with a new quote number (autoincrement).

    My xbasic looks like this:
    if response=UI_NO_SELECTED then
    'we need to create another quote with the
    'same header and line items but with a new
    'quote number
    ''XBasic
    dim hdr as p
    dim items as p
    dim old_parent as b
    dim count_items as n
    dim i as n


    hdr=table.current(1)
    items=table.current(2)
    count_items = count(quote_items->quote_number,GRP->quotation_base)
    if count_items>0 then
    dim records[count_items] as b
    i=0
    items.fetch_first()
    while i<count_items
    i=i+1
    records[i]=items.record_data_get()
    items.fetch_next()

    end while
    end if 'count_items>0
    old_parent=hdr.record_data_get()
    hdr.enter_begin()
    ' note, the quote_number is
    ' an autoincrement field
    ' a new quote_number is assigned by the field rule,
    hdr.record_data_set(old_parent)
    hdr.enter_end(.t.)
    if count_items>0 then
    for i=1 to count_items

    items.enter_begin()
    items.record_data_set(records[i])
    items.quote_number=hdr.quote_number

    items.enter_end(.t.)
    next
    end if ' count_items>0
    parent.resynch()
    end

    When I run this I get an error

    "property not found", "items.quote_number subelement not found" . Running debug, the count of the child records is fine and the parent record is successfully copied into a new parent record with a new quote number. The error is generated at the point where the code attempts to modify the quote number in the stored "old" child record before copying it into the new one.

    The code is based on the example at learn alpha.com

    #2
    Re: Copying parent and children

    I'm not an expert, but I have simuler code working and the only difference I see right off is I am putting the new autoincrement into a variable like this: (changes in red)

    Code:
    [COLOR="Red"]dim recid as c[/COLOR] 'or what ever data type your autoincrement field is.
    hdr.enter_begin()
    ' note, the quote_number is
    ' an autoincrement field
    ' a new quote_number is assigned by the field rule,
    hdr.record_data_set(old_parent)
    [COLOR="Red"]recid=hdr.quote_number[/COLOR]
    hdr.enter_end(.t.)
    if count_items>0 then
    for i=1 to count_items
    
    items.enter_begin()
    items.record_data_set(records[i])
    items.quote_number=[COLOR="Red"]recid[/COLOR] 'instead of hdr.quote_number
    items.enter_end(.t.)
    next
    end if ' count_items>0
    parent.resynch()
    end
    Hope this helps.
    Dan
    - Dan Hooley
    - Custom Desktop or Web database development -

    Comment


      #3
      Re: Copying parent and children

      dan - should not make any difference I think...

      Comment


        #4
        Re: Copying parent and children

        Nick,
        What is the field name of the linking field?
        Mike W
        __________________________
        "I rebel in at least small things to express to the world that I have not completely surrendered"

        Comment


          #5
          Re: Copying parent and children

          Originally posted by Mike Wilson View Post
          Nick,
          What is the field name of the linking field?
          quote_number

          Comment


            #6
            Re: Copying parent and children

            NIck, I don't see anything wrong in your script, so I'm thinking this may be a timing issue. On average how many child table records are linked to the quote hdr being copied? Do you have sample tables that we could use?

            Comment


              #7
              Re: Copying parent and children

              There are only two or three on average. The one I was testing last night with debug has three child records.
              I will develop a script tonight and post it.

              Comment


                #8
                Re: Copying parent and children

                Your code seems fine to me and works in admittedly stripped down setting(attached). Your code runs from a button on a form in my example.
                There can be only one.

                Comment


                  #9
                  Re: Copying parent and children

                  Nick, I've modified your code slightly and it runs fine with build 1884_3220. Example is attached. My example is on a button on the only form you'll find in the attached database. The changes adapted your code to the Invoice_header and Invoice_items tables I'm using. I've marked the lines I changed with double asterisks. Suggest you apply the latest patch and try again.

                  Comment


                    #10
                    Re: Copying parent and children

                    Code:
                     items.enter_begin()
                    items.record_data_set(records[i])
                    items.quote_number=hdr.quote_number
                    I believe this is the problem part of your script.

                    In this part of your script you are entering new records and populating the fields with record_data_set() then immediately after that you want to assign a different value to items.quote_number.
                    You cannot do that until the record has been saved first, hence the error message. Alpha does not see items.quote_number untill the record is saved.

                    But wait...before you go and save the records and attempt to change them in a subsequent step:
                    If you save the new records with the old autoinc value, they will get mixed up with the other child records with the same old autoinc value and becomes hard to isolate them to give them the new autoinc number.
                    Hmmmm!
                    While your code is good, I must say and don't take this the wrong way: it looks like the "Lazy man Load". You know what that is? Lazy man does not want to go back and forth, carries a heavy load and spills it off.
                    Instead of using record_data_get() and record_data_set() (heavy load) in your array, specify each and every field unless you have a whole lot of them, and change the value to the new autoinc in the array itself.

                    If you wish to pursue the "Lazy Man Load" approach, it can be done, but you will find out it's more work than you think, so before going down that road, do you really have to use this approach?
                    Last edited by G Gabriel; 12-04-2008, 12:44 PM.

                    Comment


                      #11
                      Re: Copying parent and children

                      Somehow this script seems more complicated than it has to be. I know a lot has been said and written about copying a parent with the children, but what's wrong with (psuedo-code):
                      1-open the parent table
                      2-add a new record with the new autoinc value
                      3-For each child record, add a new record with the new autoinc value.
                      Why even use an array as an intermediary?

                      Comment


                        #12
                        Re: Copying parent and children

                        G, I agree. The populating the array is not necessary.

                        However, there doesn't seem to be a need to commit the child records before "changing" them to include the new quote_number. Take a look at my example. It seems to work just fine, using invoice_header and Invoice_items tables from AlphaSports. My set is very simple, as are my tables. Perhaps Nicks are big enough or complex enough to cause significant delay in handling the disk activity. If that's correct then your idea (set data, commit, change data, commit) might very well solve his problem. Especially if the sequence became : set data, commit, wait for idle, change data, commit. Can't say for certain without sample data from Nick.

                        Comment


                          #13
                          Re: Copying parent and children

                          Tom:
                          I am speaking in sort of abstract here as it is hard for me right now to commit the time to test (multi-tasking and can't get away from what I am doing), but:
                          a-I don't quite recall if record_data_set() when used in the context of table.enter_begin() and end() will commit the new record or you will have to commit it
                          b-But regardless, even if the new record is commited, if you try to change it before resynching, alpha is still looking at the current record.

                          Again, I didn't test any of these concepts, but one way or the other, he has to hit the brake between these 2 lines of code and let alpha either commit and/or resynch.

                          Comment


                            #14
                            Re: Copying parent and children

                            Here is one more of my "abstract" thinking and this has a pretty good shot of being the culprit:
                            You have:
                            items=table.current(2)
                            It is quite possible you are using the wrong number.
                            What is your set structure like?

                            Comment


                              #15
                              Re: Copying parent and children

                              Thanks everyone for the responses.
                              First, about the code in general. It is an almost exact copy of the code listed on the site learn alpha.com, so it's certainly one way of copying the parent into a new parent record and then the children into new child records linked to the parent. You should not need to commit the child records before changing them because you are changing the record in the intermediate array before doing the copy to the new child record.
                              However I think G might be on to something. Even before I saw his post I was thinking about this driving home tonight and because it's a complex set maybe the child records are not in table.current (2) but somewhere else. I did check using debug that the code is counting the correct number of child records in the two different test cases but maybe that was just coincidence and it was counting a different set of child records, they just happened to have the correct number. I will check this out tonight.

                              Comment

                              Working...
                              X