Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 5 of 5 total
Thread Using Data Aware Controls OnExit Method to Maintain Database Updates
Mon, Nov 21 2016 8:11 AMPermanent Link

Michael Riley

ZilchWorks

Avatar

I have never used Delphi's data-aware controls before. Most of the Delphi database examples I've seen use a dbGrid, a dbNavigator, and several dbEdit data aware controls all wired up. When you navigate around the dbGrid the data-aware controls change to reflect the fields of the current record in the dbGrid. If you modify the data in a data-aware control that modification is instantly reflected in the dbGrid when you navigate to a different row.

My application is different from all these examples. I'm using a single-user ElevateDB model. Most of what my application does is based on "What-ifs". When the user is done playing what-ifs they can save the data.

I won't have a dbGrid or dbNavigator available, but I'd like to use data-aware controls.

I plan on using a set of workTables to handle all the what-if activity. Here is the concept:

1. The user selects a single master-detail record to use from the base tables using a dbGrid in a modal window.
2. When the window closes the master-detail data is copied to an empty set of work tables.
3. These work tables only contain one master-detail record at a time.
4. Several data-aware controls are wired up to these work tables.
5. The OnExit method of each data-aware control issues the update to the work tables.
6. User hits save and the base tables are updated with data from the work tables.

My concern is the use of the data-aware-control OnExit method to post updates to the work tables.

I want the user to feel like they are free to roam around and make changes without having to respond to "Update" interruptions. This is the only way I can think of to accomplish this type of freedom and still keep the work-tables updated.

Has anyone done used this type of approach?
What kinds of things do I need to be aware of using this methodology?

Thank you in advance.
Michael Riley
GySgt USMC (Retired)
www.zilchworks.com
Mon, Nov 21 2016 11:18 AMPermanent Link

Roy Lambert

NLH Associates

Team Elevate Team Elevate

Michael


This step

5. The OnExit method of each data-aware control issues the update to the work tables.

confuses me. If you're using tables you don't need to use an event to update the tables, that gets done automatically. About all you need to do is make sure you .Post the work tables BEFORE updating the base tables. Even that shouldn't be necessary but I always like to.

I could be totally misunderstanding what it is you want to achive but a what if usually requires some sort of calculation being done and I think your main problem is to make sure the calculation occurs at the right point. The OnChange event of edit controls is often used for this. If you use that in its simplest form you generally get errors as the calculation is carried out with duff data. What I do is have a single OnChange event for all the appropriate controls. In that a timer is reset (I generally use an interval of 150). The timer is what runs the calculation - making sure its first action is to disable itself. This way the calculation doesn't run whilst someone is typing.

Just make sure that the timer isn't toggled if its already running - I use boolean to track if the calculation is actually ongoing.

Roy Lambert
Mon, Nov 21 2016 11:30 AMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Michael,

Roy is correct, the "update on exit" is behavior is automatic for data-bound controls in Delphi.

However, if you want to check the Modified property of a TDataSet-descendant before doing things like prompting the user during a close without a save, you should be sure to use this method:

function TMyForm.CheckSave: Boolean;
begin
  Result:=True;
  if (MyDataSet.State in [dsInsert,dsEdit]) then
     MyDataSet.UpdateRecord;  <<<<<<<<<<<<<<<<<<< This !!!!
  if MyDataSet.Modified then
     begin
........

The UpdateRecord method makes sure that the process that normally occurs when a user tabs out of a data-bound control also occurs before you check the Modified property.  This will prevent any issues with your code thinking that there weren't any modifications because only one data-bound control was edited, and then the user didn't tab out of the control where the edit was made.

Tim Young
Elevate Software
www.elevatesoft.com
Tue, Nov 22 2016 2:42 AMPermanent Link

Roy Lambert

NLH Associates

Team Elevate Team Elevate

Tim

<<function TMyForm.CheckSave: Boolean;
begin
  Result:=True;
  if (MyDataSet.State in [dsInsert,dsEdit]) then
     MyDataSet.UpdateRecord;  <<<<<<<<<<<<<<<<<<< This !!!!
  if MyDataSet.Modified then
     begin>>

Thanks for that. I don't think I ever knew it which is why I use the OnChange event to set a flag.

Roy Lambert
Sun, Nov 27 2016 1:24 PMPermanent Link

Michael Riley

ZilchWorks

Avatar

>>
Tim Young [Elevate Software] wrote:

Michael,

Roy is correct, the "update on exit" is behavior is automatic for data-bound controls in Delphi.

However, if you want to check the Modified property of a TDataSet-descendant before doing things like prompting the user during a close without a save, you should be sure to use this method:

function TMyForm.CheckSave: Boolean;
begin
  Result:=True;
  if (MyDataSet.State in [dsInsert,dsEdit]) then
     MyDataSet.UpdateRecord;  <<<<<<<<<<<<<<<<<<< This !!!!
  if MyDataSet.Modified then
     begin
........

The UpdateRecord method makes sure that the process that normally occurs when a user tabs out of a data-bound control also occurs before you check the Modified property.  This will prevent any issues with your code thinking that there weren't any modifications because only one data-bound control was edited, and then the user didn't tab out of the control where the edit was made.
>>

Do I feel like a complete idiot. The reason I wasn't seeing the immediate change is because I was looking at the data in the ElevateDB Manager and not a dbGrid in my own application. As soon as I added a dbGrid wired to the same data source I saw what I was expecting to see immediate changes. DOH!

This makes me rethink whether I want to use a set of "work" tables to manage the whole "what-if" scenario. I like the idea of having a clear and distinct set of disconnected data for playing around. Back to the drawing board... again.
Michael Riley
GySgt USMC (Retired)
www.zilchworks.com
Image