How to validate Dbgrid fields before saving?

Asked

Viewed 2,029 times

3

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

I believe I have to use the event BeforePost but I have no idea how to do it. Someone could give me a hint?

  • 1

    Maybe this can be done at the event OnValidate, it occurs before the data is written. What kind of validation you wanted to do?

  • @Qmechanic73 I just want to see if all the columns are filled. I didn’t know about this event, but how do I capture this data before it’s written?

1 answer

5


Do this at the event TDataSet.BeforePost not a bad idea, but how do you want to validate the fields in particular, the event DB.TField.OnValidate can serve better for this case.

To use this event do:

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

    inserir a descrição da imagem aqui

  • In the next window select the field you want to validate.

  • In the Object Inspector, go on the tab Events and double click on OnValidate.

    inserir a descrição da imagem aqui

Now all you have to do is validate the field, remake the same process for the other required fields. So when the user edits such a field, before the change is saved, the OnValidate is called.

For example, if I want to prevent the user from saving in the field Animal the word giraffe, can do:

procedure TForm1.TblAnimaisAnimalValidate(Sender: TField);
begin
if Sender.AsString = 'girafa' then
   Abort;
end;
end;
  • Perfect, thanks again very much. Works just right.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.