This information is for Version 2.0 of Cardbox for Windows.
We recommend that you upgrade to Cardbox 3.

(logo)
Macros: calculating age  

(Technical support)

 

Q. I have a field in my database that contains a person's date of birth.  How do I use the macro system to compute an age rounded down to a whole number of years?

A. All the ingredients are there in the macro system's Calculator tool: it's just a question of knowing how to put them together.  Look up each of the functions we mention in the on-line Help or in the manual, so that you can think of other uses for them as well.

Function Meaning Value
Date(0) Today's date. (Date(1) would be tomorrow's date). 6/3/2000
'11/6/1956' A literal date.  Here, we'll use it for testing.  In an actual macro, you'd want to replace it with the contents of a field. 11/6/1956
Year(Date(0),'11/6/1956') The number of years between the two dates. 43.73
Int(Year(Date(0),'11/6/1956')) The number of years rounded down to the next lower integer. 43

The last stage in evolving the calculator expression that you want is to replace the literal date with the name of the field from which the date will be taken.  For example, if you have a field called DOB containing the date of birth, then the calculator expression would become

Int(Year(Date(0),DOB))

Note that words such as DATE are reserved words for the macro system- so if you have a field called DATE, you will have to change its name before the macro calculator can use it.

 

 


Trying out a calculation

When you are faced with a complex calculation like this, it is always best to try the macro calculator by hand first, without recording anything.  Then you can try out different combinations of functions until you get the result you want.

Recording the calculation in a macro

Once you are sure that you have the calculation you want, you can start recording a macro and use that calculation.  One warning: do not be tempted to use the Clipboard!  If you copy your newly-evolved complicated expression to the Clipboard, then start recording a macro and paste the expression into the calculator's Expression window, the macro system will faithfully record the fact that you pasted something and not the actual expression that you pasted!  The next time you play that macro, the Clipboard will probably contain something else, and that is what will be used...  When recording macros that involve the calculator, the normal rule is: type, don't paste.


2000 Cardbox Software Limited  (logo)