Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding 'child' rows: duplicating the key of one-to-many child!

Status
Not open for further replies.

JackMcC

Technical User
Mar 5, 2001
3
US
I am a 'light' user of VFP Ver 3.0b (I have 6.0, have not used it yet. I am doing volunteer work with VFP 3.0).
Is there a general 'approach' to storing 'child' records in a one-to-many relationship, specifically, with regard to the 'many' key of the child? (The child key is the Primary key of the parent.) That is, when STORING a child row, can the "many" key be 'duplicated' from the Primary key (assuming that one is 'current' upon the parent row, as in a one-to-many-form)without, say, having to re-type the key value for the child row? I am visualizing a form, for example, with the parent data in the upper portion and either a grid or text fields for multiple or single child rows in the lower part of the form.
I have wondered if this is done best through use of a VIEW, with TRIGGERS or with EVENT CODE in the form? All the VFP books I have read seem not to address this.(You may have surmised from my question - I am from the mainframe hierarchical/network dbms world). I figure that this is probably 'old hat' and that VFP developers have a 'general way' of doing it. Just a hint or a push in the right direction would be greatly appreciated ( I am a newcomer to this forum, and enjoy the information I see here tremendously!).
Thank you, -Jackmcc (Jack McCarrick)
 
If the child DBF field names are same as that of the parent DBF, the following method could be effectively used.
USE parent
scatter memvar
..
..
all your activities
..
..
When the child record is added...
..
INSERT INTO (ThisForm.cChildAlias) FROM MEMVAR
OR
APPEND BLANK
GATHER MEMVAR
..
Hope this helps

ramani


 
Hello.

First of all, you'll have to keep in mind that the only thing that keeps the parent and the child tables linked is a field (usually having the same name - i.e. ID_ROW).

In my apps I solve this kind of problem using a parameterised view for the 'child part'. Something like that:

a) the 'parent':
SELECT * FROM parent_table INTO CURSOR crParent - or -
I create a view in the database that downloads the parent records.

The resulting cursor is used as RecordSource for the parent grid or the textboxes used to display the parent table.

b) the 'child':
SELECT * FROM child_table INTO CURSOR crChild WHERE child_table.id_row = ?n - or -
I create a view in the database.

I place both views in the DataEnvironment, and the second one have NoDataOnLoad = .T.

In the AfterRowColChange event of the parent grid, I usually have the following code:

n = parent_table.id_row && Establish the parameter
REQUERY(the_second_view) && Download the records
TheSecondGrid.Refresh && and show the result.

Using the views stored in the database gives you the posibility to switch from local data to remote data in no time.

Now, getting to the subject: if you insert records in the child table, you'll have to be sure that the ID_ROW field is filled with the value taken from the parent_table.ID_ROW field. After the insert, grid.refresh.

Hope this helps.

P.S. If you need further information, I'll be glad to send to you a form designed in this way, to study.
Grigore Dolghin
Class Software
Bucharest, Romania
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top