Simple DataField Validation & Get Data Info
procedure ValidAndGetInfo(ADataSet: TDataSet;
ASQL, AError, AResultSetField: string; RaiseException: Boolean = true);
var
cds: TClientDataSet;
AList: TStringList;
i: Integer;
begin
cds := TClientDataSet.Create(nil);
AList := TStringList.Create;
try
AList.CommaText := AResultSetField;
cds.Data := MainConnection.GetData(ASQL);
if cds.IsEmpty then
begin
if RaiseException then
raise Exception.Create(AError)
end
else
for i := 0 to AList.Count - 1 do
ADataSet.FindField(AList.Names[i]).Value := cds.FindField(AList.Values[AList.Names[i]]).Value;
finally
cds.Free;
AList.Free;
end;
end;
//invoke example
procedure TMainDataMoudle.cdsSOCustNoValidate(Sender: TField);
begin
ValidAndGetInfo(cdsSO,
Format('select * from Customer where CustNo = %s', [AnsiQuotedStr(Sender.AsString, '''')]),
'Invalid Customer No.',
'J_CustName=CustName,J_Tel=Tel,J_Fax=Fax');
end;