0

I have a project in Delphi 7 that uses a database in MySQL to store some configuration. Whenever I change a config, a "Save" button is enabled. The OnClick procedure in this button calls TADOQuery.Edit, Select the fields with the SQL property, TADOQuery.Open and set the various FieldsByName. In the end, it TADOQuery.Post the configuration and Requery it.

This works well, only if at least one of these fields is actually changed.

If, for example, I check a checkBox (originally unchecked) and then unCheck it again, the Save button would go enabled, but the actual data in database doesn't change. In this case, when I call Post, an Exception is raised.

I saw this question that would solve my problem, checking if there ara any modification, but as soon as I set the first field, the Modified property of TADOQuery becomes true, invalidating this solution.

Another option would be to check, before setting the field, if it will actually change, setting an flag to, in the end, actually post it or not. But, there are hundreds of fields to do that, which will be pretty boring to do.

A third alternative I thought is to create a new field in database with a "Last Modified" datestamp, which forces to always have at least one modification, but I prefer to not mess with the existing database.

Is there any other way to know if a TADOQuery.Post will trigger an exception, because no data has really changed? How can I solve this problem? Or there's a simple workaround for it?

The ADOQuery variable is dynamically created in the Save button's routine (and free'd in the end).

Community
  • 1
  • 1
ricardomenzer
  • 268
  • 3
  • 16
  • I worked with CBuilder++ long time ago, and I "think" there was something called OldValue and NewValue when you edit and modify a recordset, but I'm really not sure. Long, long time ago. – McNets Oct 27 '16 at 21:16
  • Maybe this: http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/DB_TField_OldValue.html – McNets Oct 27 '16 at 21:17
  • Humm... that seems good, but for one field it fails accessing the oldValue. I don't know why. For now, I put the `post` in a try..except and check the exception message if it's the messsage for when this happens. It will obviously not work when the user use a different language in the system, but it is what I got for now... – ricardomenzer Oct 28 '16 at 18:26

1 Answers1

0

It would be a nice approach to use CheckBrowseMode() instead of Post().

ADOQuery1.CheckBrowseMode

CheckBrowseMode(): Automatically posts or cancels data changes when the active record changes.

You can read more about here: http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/DB_TDataSet_CheckBrowseMode.html

Leo Melo
  • 196
  • 5