PDA

View Full Version : Copy previous record from two tables


ABC123

Gustavo1478
08-13-2014, 05:07 PM
Hello,

Im trying to create an operation that will copy the previously entered record of two tables. I know how to accomplish this when Im copying a record in just one table, but two tables has me stumped.

I have a form based on two tables that are joined as a set, and I want to add a button that when pressed will copy the last record from table a, and table b. Then I want to place this copied record into edit mode to change to a new students ID#. The ID# needs to update the parent and child record.

Is there way to accomplish using the genies? Im barely learning xbasic so this is a bit overwhelming to write. Anything will help, Thanks.

-Ivan

Stan Mathews
08-13-2014, 05:14 PM
Are you saying you want to make one record in a table out of two records in other tables, combining them? The destination has the same fields as both the other tables?

Whether you use action scripting or xbasic the process will involve copying the record from either of the source tables to the destination table and writing the desired fields from the other source table to the new destination table record.

Not the sort of thing the genies can handle.

Gustavo1478
08-13-2014, 05:44 PM
its more like duplicating the record (in the respective tables), then placing the duplicated record into edit mode to change the student ID. This is all happening in a form by the way.

I was afraid you would say the genies are unable to accomplish this.

Do you know of any great learning tools for learning xbasic?

Stan Mathews
08-13-2014, 06:00 PM
If you have two table records from which you want to make one final record it is not duplicating (to Alpha). You have to create a record in the destination table and fill in the fields form both of the source records. Alternatively, copy the record from one table and fill in the fields from the other.

I'll try to work up a sample.

Gustavo1478
08-13-2014, 07:08 PM
See my attached example

If you look at the form, and the table set up, Im not trying to combine records. I'm trying to copy a previous record and replace fields manually. See the button Action Scripting so you can kind of see what im trying to accomplish here. The action scripting I placed there is not doing what I want it to do.

MoGrace
08-13-2014, 08:50 PM
Hi Ivan,
I think the problem in trying to copy the record in the browse and then pasting it into the new parent record browse is the link is also being copied from the source and is not linking to the new record. Perhaps a lookup field rule in the notes table would be a better way to use a previous record after you have duplicated the parent record.

Stan Mathews
08-14-2014, 10:58 AM
See if this does what you want.

Gustavo1478
08-14-2014, 12:08 PM
PERFECT!! :) That did do it stan, that's exactly what I wanted to do. Now I need to research what you did in xbasic so I can understand whats going on and how to do it on my own. I have other tables where I need to perform this type of operation so I need to learn thing quick.

Thank you again stan.

Stan Mathews
08-14-2014, 12:22 PM
Commented version of the script.


'save the current record on the form
topparent.commit()
'establish pointer to parent table (1)
src1 = table.current(1)
'establish pointer to child table(2)
src2 = table.current(2)
'save current record data from both tables to variables
record_data1 = src1.record_data_get()
record_data2 = src2.record_data_get()
'start a new parent record
src1.enter_begin()
'write previous parent record data to parent table
src1.record_data_set(record_data1)
src1.enter_end(.T.)
'get the linking field value from the new record as created by auto-increment
new_id = src1.orientation_id
'start a new child record
src2.enter_begin()
'write previous child record data to parent table
src2.record_data_set(record_data2)
'set/overwrite the linking field value to the new parent record linking value
src2.Orientation_Id_Fk = new_id
src2.enter_end(.T.)
'resynch the form to the set
topparent.resynch()
'refresh the display of the form
topparent.repaint()

Gustavo1478
08-14-2014, 01:31 PM
So just to clarify, to establish a pointer like in "src1=table.current(1)", this points to the first table in the set which is the parent, right?

With that logic if this particular set had 5 tables, and I only wanted to copy a record in the third table in the set, the function would be "src3 = table.current(3)" right? Where "src3" is just the name of the variable. Is my thinking on the right path?

Stan Mathews
08-14-2014, 01:42 PM
Correct. The # supplied refers to the vertical position of the table in the set when viewed in the set editor. No adjustment is made for relative "importance" of the linked table. IOW, count straight down ignoring whether the linked table is linked to the parent or linked to another child table.

Gustavo1478
08-14-2014, 03:19 PM
Is it possible to cut this xbasic code in half? In other words, create an operation that copies src2 record, and then create a different button that will paste the record once you've navigated to a record of your choosing?

I already tried to break this up but I guess i'm not doing it correctly.

My goal is to be able to navigate to a parent record and edit roughly 3 fields, then enter a note in the child table in regards to the edited fields, copy said note from the child table. Then navigate to a different parent record, manually edit roughly three fields again, and paste the previous record note to this record (all while updating the linking field ID's ofcourse). I tried to establish variables within the form to store the info, but the variable keeps losing its definition???

Gustavo1478
08-14-2014, 03:31 PM
Well, I got it to copy and paste, with two separate buttons. But this only works if I stay on the same parent record. Once I navigate and try to paste, it does nothing. Also the ID's are not updating :thinking:

Stan Mathews
08-14-2014, 03:41 PM
Sorry, your description lost me.

Stan Mathews
08-14-2014, 04:18 PM
This part of the script captures the values in the current record.


'establish pointer to parent table (1)
src1 = table.current(1)
'establish pointer to child table(2)
src2 = table.current(2)
'save current record data from both tables to variables
record_data1 = src1.record_data_get()
record_data2 = src2.record_data_get()

If you are splitting the script into two parts, the second script will not know anything about what happened before unless you make the variables available to it.


dim shared src1 as P
dim shared src2 as P
dim shared record_data1 as B
dim shared record_data2 as B
'establish pointer to parent table (1)
src1 = table.current(1)
'establish pointer to child table(2)
src2 = table.current(2)
'save current record data from both tables to variables
record_data1 = src1.record_data_get()
record_data2 = src2.record_data_get()

Now the first of the second script should establish the connection to the same variables.


dim shared src1 as P
dim shared src2 as P
dim shared record_data1 as B
dim shared record_data2 as B
'rest of second script goes here

Gustavo1478
08-14-2014, 04:57 PM
Heres how I split the script:

"Copy Script"
dim shared src2 as P
dim shared record_data2 as B

'establish pointer to child table(2)
src2 = table.current(2)

'save current record data from both tables to variables
record_data2 = src2.record_data_get()

"Paste Script"
'get the linking field value from the new record as created by auto-increment
new_id = src2.Apprentice_wp_id_fk
'start a new child record
src2.enter_begin()
'write previous child record data to parent table
src2.record_data_set(record_data2)
'set/overwrite the linking field value to the new parent record linking value
src2.Apprentice_wp_id_fk = new_id
src2.enter_end(.T.)
'resynch the form to the set
topparent.resynch()
'refresh the display of the form
topparent.repaint()

Which is copying the record just fine but its not replacing the ID linking field to the new parent record? What am I doing wrong.

Gustavo1478
08-14-2014, 05:05 PM
I think I see the problem, am I missing the pointer to the parent table?

so script 1&2 should still have:

dim shared src1 as P
src1.table_current(1)

Except since I don't want to copy anything from the first table, Ill leave out the record_data_get() part. Is that right?

and script two should point to table one to overwrite the field in table 2 of the record we just pasted.

Stan Mathews
08-14-2014, 05:09 PM
'get the linking field value from the new record as created by auto-increment
new_id = src2.Apprentice_wp_id_fk


This is not were the linking value is established. The child table needs the value of the auto-increment (I assume) field in the parent table to which you want to link. If you are navigating to a diifferent parent record or beginning a new parent record you would


"Paste Script"
'get the linking field value from the current parent record or new parent record as created by auto-increment
src1 = table.current(1)
new_id = src1.Apprentice_wp_id_fk
'start a new child record
src2.enter_begin()
'write previous child record data to parent table
src2.record_data_set(record_data2)
'set/overwrite the linking field value to the new parent record linking value
src2.Apprentice_wp_id_fk = new_id
src2.enter_end(.T.)
'resynch the form to the set
topparent.resynch()
'refresh the display of the form
topparent.repaint()

You may have a different name for Apprentice_wp_id_fk in the parent table so you would change this to match.

Gustavo1478
08-14-2014, 05:14 PM
UPDATE

Hers my Script 1 "Copy Script"

dim shared src1 as P
dim shared src2 as P
dim shared record_data2 as B

'establish pointer to child table(2)
src1 = table.current(1)
'establish pointer to child table(2)
src2 = table.current(2)

'save current record data from both tables to variables
record_data1 = src1.record_data_get()
record_data2 = src2.record_data_get()


Heres my script 2 "Paste Script"

'get the linking field value from the new record as created by auto-increment
new_id = src1.Apprentice_id_pk
'start a new child record
src2.enter_begin()
'write previous child record data to parent table
src2.record_data_set(record_data2)
'set/overwrite the linking field value to the new parent record linking value
src2.Apprentice_wp_id_fk = new_id
src2.enter_end(.T.)
'resynch the form to the set
topparent.resynch()
'refresh the display of the form
topparent.repaint()

The first script goes off without a hitch, the 2nd script gives me an error which I wrote in red. It states "Access is denied"

Stan Mathews
08-14-2014, 05:26 PM
I think you need to either establish or reestablish the parent table pointer in the paste script. When you establish it in the first script and then navigate to another parent record the pointer still references the original record (I think, never tried anything like this before).

So in the paste script I would

src1 = table.current(1)
new_id = src1.Apprentice_id_pk

At least I'm hoping that's the issue.

Gustavo1478
08-14-2014, 05:40 PM
JACKPOT! That worked stan, thank you again. I really appreciate your patience and help with this.

Since you say you've never done anything like this before, is this a good way of doing this? Your obviously experienced and knowledgeable, so Im wondering if im going about this application in the right way?

Stan Mathews
08-14-2014, 05:44 PM
I've never done anythng like this in terms of any application. My work is data warehousing and data mining. I combine input from outside sources that is related but not inherently compatible. One example is inovicing to use from multiple vendors. I take the invoicing data from them electronically and normalize it to one standard and report profitability, etc to our buyers.