|
Copy a Record in a Set |
|||||
|
One night I ran across a post
on the Alpha forum asking for help in duplicating a record in a form based
on a set. The user asking for help referenced a script "Duplicate a set's entries", written by Dr. Peter Wayne,
that was posted on learnalpha.com. He was unable to get Dr. Wayne's script
to work, so I decided to take a look. |
|||||
I received a copy of the user's database and began my investigation. The sample application the user supplied had the following set: |
''XBasic script by Peter Wayne |
||||
![]() |
|||||
| As you can see, the set has 1 child table with a 1:M link and 4 grandchild tables, 3 with a 1:M link and 1 with a 1:1 link all on the child table. Dr. Wayne's script only copied the parent and any child records, so I needed to modify his script to also include the grandchild records. I began by creating pointers for table.current(3), (4), (5) and (6). I then used the code that copied the child records and modified it to copy the grandchild records and placed them in the same loop. Well, this attempt failed poorly. Not only did the script not copy the grandchild records, it also went into an endless loop duplicating the first child record over and over again until I shut A5 completely down. It seemed to me like the FETCH_NEXT() was failing, but why? I was obviously doing something wrong. I posted a message on the board with the assumption that the fetch_next function was failing. Two responses from the board suggested I used the "outline level" for the fetch_next(). Outline level? Huh? After reading the docs on it, I came to the conclusion that this is not what I need. At this point I decided to contact Dr. Wayne and ask him for his help. | |||||
| I explained to him I was trying to help another user on the board and I am having a problem too. I explained to him that I thought that the fetch_next() function was failing and attached a very descriptive example that he could see that I was right. He replied to my request for help and corrected my script which included the outline level. OH NO!! (not the exact reaction, but .. ) I realized that I posted what I see as a very uneducated answer on the message board back to people that obviously knew more then I did. Talk about embarrassing! Well my understanding of the outline level for the fetch_next() is still not 100%, so I will save that for another article written by someone with a better understanding then me. Anyway, back to the issue at hand. After some trial and error, plus another email to Dr. Wayne, I got a working script that would do the job and do it well. | |||||
| To make it easier to follow the script I created a new set shown to the right. The only change besides the table names I made from the original set structure was placing the 1:1 linked grandchild table first. This is not absolutely necessary, however, it needed to be the first grandchild record copied in the script so I structured the set the same way. The linking field between the parent(header) and child(mychild) tables is called "Transno" which is an autoincremented field. The | |||||
![]() |
|||||
| linking field between the child and grandchild tables is called "Ptransno" which is also an autoincremented field. "Transno" is incremented when a new parent record is created and "Ptransno" is incremented when a new child record is created. Below is the working script with comments to better understand how it works. | |||||
| dim mm as p dim mc as p dim i as n dim old_mm as b ''Set up table pointers for parent and child tables mm = table.current(1) mc = table.current(2) g1 = table.get("mygrandchild1") g2 = table.get("mygrandchild2") g3 = table.get("mygrandchild3") g4 = table.get("mygrandchild4") mm2 = table.open(mm.name_get()) mc2 = table.open(mc.name_get()) g11 = table.open(g1.name_get()) g22 = table.open(g2.name_get()) g33 = table.open(g3.name_get()) g44 = table.open(g4.name_get()) ''First, the header record gets copied ''Now
we copy the child and grandchild records 'Go to
the new record that was just created 'Close
Opened tables |
|||||
|
If you would like a working demo of this article, feel free to contact me at mailto:keytech@keytech2000.com?Subject=Copy a record in a set. Many thanks to Dr. Peter Wayne for his help and guidance. |
|||||