by Matt Hamilton - mhamilton@bunge.com.au
When I first started creating database applications, I was frustrated with the problem of the user being able to press the "Edit" and "Delete" buttons even when there was no data in the table. Since there was nothing to Delete or Edit, the statements "Table1.Edit" and "Table1.Delete" would either raise an exception or do something non-intuitive.
Originally I got around that problem by checking for data first. That is, in the OnClick event for the buttons, I placed this:
So, back in my Delphi 1 days, I would catch the AfterPost and AfterDelete events on the TTable, and disable or enable the relevant buttons when the the table changed.
But then came datamodules. Now all my tables and datasources sat there! What to do? The datamodule shouldn't know anything about the form with the buttons on it, so it made no sense to catch those same events anymore.
The answer is perhaps obvious, but it took me a while to find it. Put your TDataSource on the form instead of on the datamodule, and catch the OnDataChange event. This event has a parameter called "Field", which is nil when all the fields have changed (eg when scrolling or deleting). It is therefore possible to do something like this:
It all comes down to "user-friendliness"!
Hope this helps someone...