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

Pascal syntax: Accessing the same record of a table on both parent and child forms

I think my supplementary question on this thread must have gone astray
over the New Year because I've not had any replies to it, either by email
or news.

What I'm trying to do
=====================

What I'm trying to do is have one form with a dbGrid control that allows
the user to locate a record in a database and then opens another form of
dbEdit controls which displays or allows the user to edit the chosen
record. I'm using Delphi 1. My problem really boils down to Pascal
syntax.

I've created Table and Datasource controls on Form1. Form1.dbGrid uses
Form1.Datasource and Form1.Datasource uses Form1.Table.

Steve Koterski (Borland database support) suggested placing a DataSource
on the child form (Form 2) 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. (I realise that in D2/3, I could
make this link statically from within Object Inspector.)

Problem
=======

However 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, so the edit controls know which field of the database to
display/edit. In Form 2's OnShow function I've tried putting statements
of the form:

DBEditAddrSurname.DataField := Form1.AddrTab.AddrTabSURNAME;

where:  DBEditAddrSurname       is a dbEdit control

                Form1                   is the parent form

                AddrTab         is the table object on Form1

                AddrTabSURNAME  is the corresponding database 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 "|"). I have tried
replacing "AddrTabSurname" with "SURNAME" in case it wants the real
database field name rather than the corresponding Delphi object.

I think I've set up the USES corrected: Form1.pas has "uses Form2" in
the "uses" section that comes immediately after "interface". Form2.pas
has "uses Form1" in a "uses" section that comes immediately after the
"implementation" and "{$R *.DFM}" (this is to avoid circular
references).

What am I doing wrong with my Pascal syntax - I think what I'm doing is
sensible (if not, tell me!) but I just can't get the compiler to accept
it.

 

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


Martin

Seems to me that you have at least two routes to go here. Here is one of
them.

Use a Datamodule to supply your datasets to your forms. So that at design
time you would set the value of the dataset source on form 1 to:

Form1.DataSource1.DataSet := DataModule1.Table1;

And on Form2 (The child) you would do exactly the same for its datasource.

Form2.DataSource1.DataSet := DataModule1.Table1;

Now there is no reason to fiddle with anything in the OnShow (unless of
course you want to go into edit mode) procedure, and the edit fields you
connect to data fields at design time.

Form2.DBEdit1.DataSource := DataSource1;
Form2.DBEdit1.DataField := <Whatever>;
...etc...

This way leaves you the least flexibility for change to the child form, but
it does not sound like you are looking for a generic solution.  This is
going to be the easiest to do.

Andy Jensen
        atjen...@bellatlantic.net

Martin Underwood <martin.underw...@{*word*269}.net> wrote in article
<01bd1d2e$4d434fc0$LocalHost@mu>...

Quote
> I think my supplementary question on this thread must have gone astray
> over the New Year because I've not had any replies to it, either by email
> or news.

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


Martin Underwood heeft geschreven ...

Quote
>I've created Table and Datasource controls on Form1. Form1.dbGrid uses
>Form1.Datasource and Form1.Datasource uses Form1.Table.

>Steve Koterski (Borland database support) suggested placing a DataSource
>on the child form (Form 2) 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. (I realise that in D2/3, I could
>make this link statically from within Object Inspector.)

>Problem
>=======

>However 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, so the edit controls know which field of the database to
>display/edit. In Form 2's OnShow function I've tried putting statements
>of the form:

Maybe you are closing the table when Form1 gets deactivated!
Have you tried using a (global) bookmark on the table of form1 and after
opening form2 create a bookmark on the table on this form and assign the
first bookmark to it?

Matthijs.

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


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


Martin Underwood <martin.underw...@{*word*269}.net> wrote in article
<01bd1d2e$4d434fc0$LocalHost@mu>...
[snip]

Quote
> However 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, so the edit controls know which field of the database to
> display/edit. In Form 2's OnShow function I've tried putting statements
> of the form:

> DBEditAddrSurname.DataField := Form1.AddrTab.AddrTabSURNAME;

> where:     DBEditAddrSurname       is a dbEdit control

>            Form1                   is the parent form

>            AddrTab         is the table object on Form1

>            AddrTabSURNAME  is the corresponding database 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 "|"). I have tried
> replacing "AddrTabSurname" with "SURNAME" in case it wants the real
> database field name rather than the corresponding Delphi object.

I eventually solved the problem on my own: the correct syntax is:

DBEditAddrSurname.DataField := 'SURNAME';

using the _literal_ string for the field name (the same name as the field
in the .DB file) and omitting the Form and Table components because the
correct context has already been established by the

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

Having done this for all the fields on the child form, linking each to the
field name in the .DB file (referred to on the parent form), everything
works perfectly!

Other Threads