Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Calculated field in free table
Message
From
22/02/2010 19:04:51
 
 
To
22/02/2010 18:21:04
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
01450377
Message ID:
01450382
Views:
55
>Say we were working with a FREE table and wanted to have a calculated field where Field4 = Field1+Field2+Field3. I could do this with a form method triggered by the lost focus event for Fields 1-3, but is that the best way? Is there another way that would be more reliable similar to a stored procedure in a database table?

Do you actually need to store the calculated value? There are some circumstances where that's appropriate, such as checksums for accounting or other purposes, or if the table(s) are huge and it's computationally too expensive to recalc the sums on the fly. But in many circumstances, storing a calculated result is considered bad practice because the sum may get out of sync with the contents of the other columns.

IOW the most reliable way to keep the sum field updated is to not have one at all :)

If you only need it for display purposes you could just have an unbound textbox on a form, and update it as you suggest. If you don't like the idea of code tied to a form, you could abstract it out to a method, class/object etc. Access/Assign might be useful.

I believe that if you're willing to set up a temporary/scratch VFP database container, that can work with free tables. You then might be able to do things like set up a temporary view e.g.
CREATE SQL VIEW MyView AS ;
  SELECT ;
  * ;
  , Field1 + Field2 + Field3 AS FieldSum ;
  FROM MyFreeTable ;
  ...
If you do actually need the column present in the physical table, you need to roll your own referential integrity (unless you can define RI on a free table from within a temp DBC, which I'm not sure about).
Regards. Al

"Violence is the last refuge of the incompetent." -- Isaac Asimov
"Never let your sense of morals prevent you from doing what is right." -- Isaac Asimov

Neither a despot, nor a doormat, be

Every app wants to be a database app when it grows up
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform