Board index » delphi » Question: Master/Detail

Question: Master/Detail

I put up a Master/Detail DB which works fine. But I want to be able to edit
the Detail form. No success. Even when I try to execute a SQL statement to
insert a record I get some key error. Solution?

P.S. what are the advantages of using SQL to nest tables over using
mater/detail forms?

Dem

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    

 

Re:Question: Master/Detail


Quote
>I put up a Master/Detail DB which works fine. But I want to be able to edit
>the Detail form. No success. Even when I try to execute a SQL statement to
>insert a record I get some key error. Solution?

>P.S. what are the advantages of using SQL to nest tables over using
>mater/detail forms?

>Dem

One of the easiest ways that I have found to do master/detail relationships is
to use the Detail Query's Datasource property and setting it to the Master's
Datasource component.  Because they put it better than I can, here is what the
help file says:

Set DataSource to automatically fill parameters in a query with fields values
from another dataset. Parameters that have the same name as fields in the other
dataset are filled with the field values. Parameters with names that are not
the same as fields in the other dataset do not automatically get values, and
must be programmatically set. For example, if the SQL property of the TQuery
contains the SQL statement below and the dataset referenced through DataSource
has a Custno field, the value from the current record in that other dataset is
used in the CustNo parameter.

SELECT *

FROM Orders O

WHERE (O.CustNo = :CustNo)

DataSource must point to a TDataSource component linked to another dataset
component; it cannot point to this query component. The dataset specified in
DataSource must be created, populated, and opened before attempting to bind
parameters. Parameters are bound by calling the querys Prepare method prior to
executing the query. If the SQL statement used by a query does not contain
parameters, or all parameters are bound by the application using the Params
property or the ParamByName method, DataSource need not be assigned. The
example below shows setting the DataSource property of Query2 to the data
source for Query1, preparing Query2, and activating Query2.

with Query2 do begin

  DataSource := DataSource1;
  Prepare;
  Open;

end;

If the SQL statement in the TQuery is a SELECT query, the query is executed
using the new field values each time the record pointer in the other dataset is
changed. It is not necessary to call the Open method of the TQuery each time.
This makes using the DataSource property to dynamically filter a query result
set useful for establishing Master-Detail relationships. Set the DataSource
property in the Detail query to the TDataSource component for the Master
dataset.

If the SQL statement uses other than a SELECT query (such as INSERT or UPDATE),
the parameters with the same name as fields in the other dataset still get
values, but the query must be explicitly executed each time the other datasets
record pointer moves. For example, the SQL statement below uses the INSERT
statement and has the parameters Custno and CompanyName.

INSERT INTO Customer

(CustNo, Company)

VALUES (:CustNo, :CompanyName)

Another dataset, Query1 and DataSource1, has a CustNo field but no CompanyName
field. If this dataset is used through the DataSource property, the CompanyName
parameter must be programmatically assigned a value. Because Query1 has a
CustNo field and Query1 is referenced through the DataSource property, the
CustNo parameter automatically receives a value.

with Query2 do begin

  DataSource := DataSource1;
  ParamByName("CompanyName").AsString := Edit1.Text;
  Prepare;
  ExecSQL;

end;

If the SQL statement contains parameters with the same name as fields in the
other dataset, do not manually set values for these parameters. Any values
programmatically set, such as by using the Params property or the ParamByName
method, will be overridden with automatic values. Parameters of other names
must be programmatically given values. These parameters are unaffected by
setting DataSource.

DataSource can be set at runtime or at design-time using the Object Inspector.
At design-time, select the desired TDataSource from the drop-down list or type
in the name.
********************************
Michael Glatz
glatzfa...@aol.com
mgl...@briefcase.com

Other Threads