TOPIC: How to autofill current date into a field..
#759
How to autofill current date into a field.. 3 Years, 5 Months ago
I have a date control on an access form, and would like to fill it with current date when user clicks on new button...

How?
 
 
#760
Re:How to autofill current date into a field.. 3 Years, 5 Months ago
The usual way is to define a rule. If your field is called mydate it would be:
Code:

Rule_mydate

function ()
    {
    return Date()
    }


Rules are only triggered when the field is empty or when a dependency is changed. Since this type of rule does not depend on any other field, it will only be triggered when the field is empty, which is what you want for "default" values.

One advantage of using a rule is that if other places in your code output records but do not fill in a date, they will also use the rule. For example:
Code:

QueryOutput("mytable", Record(name: 'Fred'))

Note: However, because this rule will be used whenever the field is accessed (including, for example, Query View) it can be hard to tell whether there is actually data in the table or not (because the rule will fill in a value). One way around this is to name the rule something different from the physical field e.g. Rule_mydate_new and to rename mydate to mydate_new on the Access query. (However, that means you have to do the rename wherever you want the rule, e.g. in QueryOutput's)

Another approach is to use the Access_NewRecord message. To respond to messages from an Access you need to wrap a Controller around the Access. For example:
Code:

MyAccess

Controller
    {
    Controls: (Access mytable)
    Access_NewRecord(data)
        {
        data.mydate = Date()
        }
    }



Note: A database trigger can not be used to set values because it is called after the record has been saved.

See also: If you want to track the last modified time of a record you can name the field with "_TS" (Time Stamp) suffix (e.g. mydate_TS) and the database will automatically set the field whenever a record is output or updated.

Hope that helps.
 
 
andrew
 
#761
Re:How to autofill current date into a field.. 3 Years, 5 Months ago
Thank you Andrew! :)

As always I am amazed how quickly you respond to our newbee questions :)

How can one not try to programm in a language whose creator is so nice :) Only time that we wait for answer is when he goes to Himalayas :)