Search code examples
mysqldelphidatetimedelphi-7delphi-2010

Delphi database created and modified using DBNavigation


Guys am looking how can i optimise my DBNavigation to automatically insert or update date and time in database col when i make changes in DBGrid.

In database i has ID, Name, Description, Created<datetime>, Modified<datetime>

Now when i change some col in dbgrid and click to save (post edit) i want to insert or update date and time when is created or modified!

I serach on google about this but i dont found.

Am new in delphi...

Anyone can give me example or link where i can read more about that!


Solution

  • You can do this using the dataset which is connected to the grid.

    In the IDE, click on the dataset and go to the Object Inspector Events tab.

    Find the BeforePost event, double click in it and enter code like this

    if MyDataSet.State = dsInsert then  //  a new record is being added
      MyDataSet.FieldByName('Created').AsDateTime := Now;
    else
      //  modifying an existing record
      MyDataSet.FieldByName('Modified').AsDateTime := Now;
    

    As noted in a comment, you can populate/update these fields using triggers on the server, but then you have to refresh the data in your Delphi dataset to receive the values in these fields, which is perhaps a bit advanced for where you are at the moment.