Board index » delphi » Inserting detail in master/detail grids

Inserting detail in master/detail grids

Hi,

Is it possible, when using master/detail dbGrids with TIBDatasets linked by
the DataSource property to have the FK of the detail QInsert set
automatically by the parameter derived from the master dataset?
For example, if master is COUNTRY and detail is CUSTOMER and
QInsert.Sql of the detail dataset is:
insert into CUSTOMER
  (CUSTOMER, COUNTRY)
values
  (:CUSTOMER, :COUNTRY)

I find that in the NewRecord event handler the COUNTRY parameter is set to
the master COUNTRY value, but it is cleared sometime before the insert is
executed.

The only way I have been able to make it work is to explicitly set the field
value (rather than the parameter) in the NewRecord event handler. EG
  ibDataset2.FieldByName('COUNTRY').AsString :=
ibDataset1.FieldByName('COUNTRY').AsString;

Since the 2 datasets are linked by datasource I had thought there would have
been a more elegent way. Have I missed something obvious here?

Thanks in advance,

Brian

 

Re:Inserting detail in master/detail grids


What I do is the following:
insert into CUSTOMER
  (CUSTOMER, COUNTRY)
values
  (:CUSTOMER, :Pk_COUNTRY)

I don't use the NewRecord, but edit the InsertSQL code
in order to take the value directly from the DataSource
(the Master).
Pk_COUNTRY is a field in the Master, as Fk_COUNTRY
(or COUNTRY in your example) is the corresponding field
in the Database (the Detail table).

The doc says that if a field is not found in the Database,
it is searched in the Datasource. So use different names
for the country field in these two tables, and it will work.

Henri Cesbron

Brian Dunstan a crit dans le message <8qa1tf$r...@linux.mers.com>...

Quote
>Hi,

>Is it possible, when using master/detail dbGrids with TIBDatasets linked by
>the DataSource property to have the FK of the detail QInsert set
>automatically by the parameter derived from the master dataset?

>For example, if master is COUNTRY and detail is CUSTOMER and
>QInsert.Sql of the detail dataset is:
>insert into CUSTOMER
>  (CUSTOMER, COUNTRY)
>values
>  (:CUSTOMER, :COUNTRY)

>I find that in the NewRecord event handler the COUNTRY parameter is set to
>the master COUNTRY value, but it is cleared sometime before the insert is
>executed.

>The only way I have been able to make it work is to explicitly set the
field
>value (rather than the parameter) in the NewRecord event handler. EG
>  ibDataset2.FieldByName('COUNTRY').AsString :=
>ibDataset1.FieldByName('COUNTRY').AsString;

>Since the 2 datasets are linked by datasource I had thought there would
have
>been a more elegent way. Have I missed something obvious here?

>Thanks in advance,

>Brian

Re:Inserting detail in master/detail grids


Quote
Brian Dunstan wrote:

> Hi,

> Is it possible, when using master/detail dbGrids with TIBDatasets linked by
> the DataSource property to have the FK of the detail QInsert set
> automatically by the parameter derived from the master dataset?

Not at this time.  I was hoping to get this into the 4.3 update but didn't have
enough time and hope to get it into the 4.4.  Until then you will have to use
the OnNewRecord event.

--
Jeff Overcash (TeamB)
      (Please do not email me directly unless  asked. Thank You)
The fool escaped from paradise will look over his shoulder and cry
Sit and chew on daffodils and struggle to answer why?
As you grow up and leave the playground
Where you kissed your Prince and found your frog
Remember the jester that showed you tears, the script for tears. (Fish)

Other Threads