Brad Gies Guest
|
Posted: Tue Oct 14, 2003 9:56 pm Post subject: Validating Inserts/Updates in Remote Data module |
|
|
I am working on a project that is using ClientDataSets on the client, and
TADOQuery's and TDataSetProviders in the remote data module. I am having a
problem validating everything I need to validate, although I assume what I'm
doing should be quite simple.
First, we have an ADOQuery call it quMaster, which has a TDataSource
(dsMaster) and TDataSetProvider (dspMaster) attached. A detail query
(quDetail) has it's datasource property set to the TDataSource attached to
quMaster.
Then in the client, we have a ClientDataSet (cdsMaster) which uses dspMaster
as it's provider and brings in a dataset that includes the nested detail
table quDetail, as one of it's fields. I then display the details in a grid
attached to the master, and another grid attached to the detail client data
set, and can make any modifications I want with no problem.
When I want to save the data, I post the datasets starting with the detail
and then the master, and then call the ApplyUpdates method for cdsMaster. In
the remote data module, the BeforeUpdateRecord event fires and I now want to
validate the data.
For Insert checks, I want to do the following :
- Make sure I have detail records for any master before the master is
inserted and abort if not.
- Make sure that all detail records that have a certain flag set total
zero.
I've been doing this by using the following code
Case UpdateKind of
ukInsert : begin
// if this is a master table insert then...
if DeltaDS.FindField('rcvID') <> nil then
begin
// check to make sure at least one detail record has
been entered.
DetailFieldIndex :=
DeltaDS.FieldByName('quReceivableDetail').Index;
DetailDataSet :=
TDataSetField(DeltaDS.Fields[DetailFieldIndex]).NestedDataSet;
if DetailDataSet.RecordCount < 1 then
ErrorMessage := ErrorMessage + 'Unable to save to
database. You have a master receivable record without any items ';
// now check to make sure the NonEDI items total
zero
SendEDITotal := 0.00;
DetailDataSet.First;
While not DetailDataSet.Eof do
begin
if
DetailDataSet.FieldByName('rcdSendEDI').AsInteger = 0 then
SendEDITotal := SendEDITotal +
(DetailDataSet.FieldByName('rcdQuantity').AsFloat *
DetailDataSet.FieldByName ('rcdUnitPrice').AsFloat);
DetailDataSet.Next;
end;
if abs(SendEDITotal) > 0.005 then
ErrorMessage := ErrorMessage + 'The Total of items
marked must be zero. ';
But this same code just started to fail when modifying a detail record
because the nested table appears to not contain any records (RecordCount =
0), and I would like to do the check every time anything changes. Can anyone
tell me how I should be doing this?
|
|