Board index » delphi » Accessing the same record of a table on both parent and child forms.

Accessing the same record of a table on both parent and child forms.

I'm using a dbGrid control to allow the user to select a given record
from a table. Once he has done this, I want to pop up another form
(triggered either by a double-click on the dbGrid or by pressing a
button) which allows the user to view or edit just that record.

I would like the dbEdit controls on the second form to be able to access
the same datasource as the one that I have defined on the parent screen.
But this gives an "Invalid property value" if I set the datasource to
either "DataSrc" or "frmParent.DataSrc" from Object Inspector.

Failing this, I could easily define the same table and datasource on the
child screen, but then I have the problem of making sure that I position
the current record in the "child table" at the same place as in the
table on the parent screen.

In Clipper I'd use the recno() function to identify which record had
been selected on the parent screen and then "go saved_recno" on the
child screen. What's the equivalent way of doing things in Delphi?

Actually, I have a slightly more general question about backward
references in Delphi: how do I set an object on a parent screen from an
event handler procedure of its child screen? My parent form's code has a
"Uses Child" entry and my child code does not have a "Uses Parent"
because this would give a circular reference. My child handler tries to
set the parent control using "Label1.Caption := 'fred';" or
"ParentForm.Label1.Caption := 'fred';" but either gives "Unknown
identifier". What's the correct syntax for doing this?

 

Re:Accessing the same record of a table on both parent and child forms.


Re:Accessing the same record of a table on both parent and child forms.


Hi Martin,

Quote
You wrote:
> Actually, I have a slightly more general question about backward
> references in Delphi: how do I set an object on a parent screen from an
> event handler procedure of its child screen? My parent form's code has a
> "Uses Child" entry and my child code does not have a "Uses Parent"
> because this would give a circular reference. My child handler tries to
> set the parent control using "Label1.Caption := 'fred';" or
> "ParentForm.Label1.Caption := 'fred';" but either gives "Unknown
> identifier". What's the correct syntax for doing this?

To avoid circular references in OP, put the reference to the unit of the  
child form in the interface-USES of the parent form and the reference to  
the unit of the parent form in the implementation-USES of the child form.
Or you can also let Delphi do it all for you by simply choosing the "Use  
Unit" entry in the menu selection "File". Then you can pick from a list of  
all the units that are entered in the current project as to which units  
you want the active form to reference.

P.S. I tend put all my TTables & TQueries in ONE DataModule-type form and  
then reference this one form in all my other forms that have data-aware  
components. I also have the DataModule the first form that is created in  
the project so there is no danger of referencing tables that don't exist  
yet.

Hope this helps. :)

Cheers,
Patrick

Re:Accessing the same record of a table on both parent and child forms.


Re:Accessing the same record of a table on both parent and child forms.


You're almost there.  In the OnShow routine for the child form, set the
datasource for the controls to MainForm.Datasource1 or whatever.  Remember this
has to be a datasource, not the table itself. Alternatively, you could put a
TTable and TDatasource on the child form, then link them to the record on the
main table by specifying, again, in OnShow, the MasterSource and linked fields
for the second table.  BTW, in D3 you can do this all at design time.

In article <01bd152d$17938f60$ce44a8c2@mu>, "Martin Underwood"

Quote
<martin.underw...@{*word*269}.net> writes:
>I'm using a dbGrid control to allow the user to select a given record
from a
>table. Once he has done this, I want to pop up another form
(triggered either
>by a double-click on the dbGrid or by pressing a

button) which allows the

Quote
>user to view or edit just that record.

I would like the dbEdit controls on
Quote
>the second form to be able to access

the same datasource as the one that I
Quote
>have defined on the parent screen.

But this gives an "Invalid property value"
Quote
>if I set the datasource to

either "DataSrc" or "frmParent.DataSrc" from
Quote
>Object Inspector.

Re:Accessing the same record of a table on both parent and child forms.


JefSummers <jefsumm...@aol.com> wrote in article
<19971231153801.KAA04...@ladder02.news.aol.com>...

Quote
> You're almost there.  In the OnShow routine for the child form, set the
> datasource for the controls to MainForm.Datasource1 or whatever.
Remember this
> has to be a datasource, not the table itself. Alternatively, you could
put a
> TTable and TDatasource on the child form, then link them to the record on
the
> main table by specifying, again, in OnShow, the MasterSource and linked
fields
> for the second table.  BTW, in D3 you can do this all at design time.

Thanks to all the people who replied, either by email or news,
suggesting substantially the same thing: that D3 allows the link
between control and dataset to be made in Object Inspector but that in
D1 this link must be made at run-time, eg from an OnShow procedure.

Steve Koterski (Borland database support) suggested placing a DataSource
on the child form and linking all the dbEdit fields to it (using Object
Inspector), then setting

DataSource1.DataSet := Form1.Table1;    {link child's dataset to parent's
table}

in the child form's OnShow procedure.

This gets me a lot further, but I've still got a problem. The dbEdit
fields are blank and won't accept any input. Having defined the DataSet
for DataSource1, I presume for each dbEdit control I need to define the
corresponding field name DataField. In the OnShow function I've tried
putting statements of the form:

DBEditAddrSurname.DataField := frmSelectAddress.AddrTab.AddrTabSURNAME;

where:  DBEditAddrSurname       is a dbEdit control

                frmSelectAddress        is the parent form (what Steve
                                        called Form1")

                AddrTab         is the table object (what Steve
                                        called "Table1")

                AddrTabSURNAME  is the corresponding field, as
                                        displayed by Object Inspector; in
                                        Fields Editor (and the Paradox table
                                        itself) it's called SURNAME

But this won't compile - it gives "field identifier expected", pointing
at "AddrTab.|AddrTabSURNAME" (cursor denoted by "|").

What am I doing wrong. I confess that I'm more fluent in C than Pascal!

Other Threads