How to validate DBGrid fields before saving?

3

I have DBGrid , but I want it to validate the fields before saving to the database.

I think I have to use the BeforePost event, but I have no idea how to do it. Could someone give me a tip?

    
asked by anonymous 16.03.2015 / 04:44

1 answer

5

Doing this in the TDataSet.BeforePost event is not a bad idea, however as you want to validate the fields in specific, the event DB.TField.OnValidate may best suit this case.

To use this event do:

  • Right click on AdoTable (or equivalent) and click on the first option, Fields Editor .

  • Inthenextwindowselectthefieldthatyouwanttovalidate.

  • IntheObjectInspector,gototheEventstabanddoubleclickonOnValidate.

Now you just have to validate the field, redo the same process for the other required fields. So when the user edit such a field, before the change is saved, OnValidate is called.

For example, if I want to prevent the user from saving the Animal field the giraffe field,

procedure TForm1.TblAnimaisAnimalValidate(Sender: TField);
begin
if Sender.AsString = 'girafa' then
   Abort;
end;
end;
    
16.03.2015 / 06:19