TOPIC: NewValue  how does it work?
#896
NewValue  how does it work? 3 Years, 3 Months ago
User's manual tells that (Cookbook --> Determining the Source of Messages):


Field controls send a NewValue message when they lose the focus and are dirty (i.e. the user has changed their contents).

Messages include an optional final "source" argument which is a reference to the control that sent the message. We can use this source argument to determine which of the controls sent the message. In this example, if the source is the field named "one" then we give an Alert.

Note: The receiver of a NewValue message is responsible for clearing the dirty state of the control.


and there is an example:

Code:


    NewValue(value, source)
        {
        if (source is .one)
            {
            Alert('one changed')
            .one.Dirty?(false)
            }
        }



OK, I use the standart "invoice" example from User's manual and want to add a field which summarize the line's Total. Rule can't help — it can summarize, but only in "new" record. So I want to catch the moment then user changes qty or price field and update the 'invoice_total' field in Access control.

Here is my code:

Code:


Controller
{
Controls:
(Access
title: "Enter Invoices"
invoice
(Vert
(Horz invoice_number Skip customer Skip invoice_main)
Skip
(Browse
"invoicelines",
columns: (product, qty, price, total_price, invoice)
linkField: invoice,
name: "invoice_main"')
)
)
NewValue(value, source)
{
Alert ("Message"')
}
}



but it looks like Controller doesn't get the 'NewValue' message at all... nothing happens.
 
 
#897
Re:NewValue  how does it work? 3 Years, 3 Months ago
The problem is that the RecordControl used by AccessControl "absorbs" the NewValue messages. It uses them to update the data record it maintains.

There are several other options. You can use RecordControl.AddObserver or you can define Access_RecordChange or Access_AfterField.

But ... I would suggest using a rule. Rules are not limited to "new" records. The reason they may not appear to work on "old" records is that if you save the rule field (i.e. have a field in the database table) then on old records, the field will have a value but no dependencies and therefore the rule does not get triggered. You have several options:

- save the dependencies by creating a _deps field in the database table e.g. for myfield also create myfield_deps

- do not save the rule field, that means it will not have a value on old records so the rule will be triggered, which will then set the dependencies. If you want it in the query (e.g. so you can search on it) you can extend it as a rule field e.g. extend Myfield (captitalized)

- set the dependencies yourself using record.SetDeps in Access_BeforeRecord

Your rule can use data.browsedata to access the rows in the browse.
 
 
andrew
 
#901
Re:NewValue  how does it work? 3 Years, 3 Months ago
Sorry, got a little confused with rule fields. To add a rule field to a query, just use:

... extend my_rule_field


To add the rule field more "permanently" to the table (but still not storing the value):

alter mytable create (My_rule_field)


This is where you need to capitalize.
 
 
andrew
 
#902
Re:NewValue  how does it work? 3 Years, 3 Months ago
Thank you. I've understand that. I've asked a new question about stored dependencies in new topic.