How to create a variable field on the visual designer and calculate a patient's age:
- Place a variable on the visual design screen
- Display the Report Tree toolbar (View|Toolbars|Report Tree), right click the new variable and select Rename from the context menu. Rename the variable to vAge.
- Right click the variable and select Calculations from the context menu.
- Copy and paste the following code into the code space in the Calculation window:
start copy on line below--
{Calculates the patient's age}
if Patient['Date of Birth'] = 0 then
vAge.AsString := '(no entered birthdate)'
else
begin
DecodeDate(Patient['Date of Birth'],DOByear, DOBmonth, DOBday);
DecodeDate(CurrentDate, NowYear, NowMonth, NowDay);
vAge.Value := NowYear - DOByear;
if (DOBMonth > NowMonth) then vAge.value := vAge.value - 1;
if (DOBMonth = NowMonth) and (DOBday > NowDay) then vAge.value := vAge.value - 1;
end;
-- end copy on the line above
Where “Patient” is written in the code, this needs to be the name of the query the data is pulling from
If you look at the code under the “Calc” tab there is a begin…end statement with the code inside of it. Before the begin statement insert the code:
Var
DOBmonth, DOBday, DOByear : integer;
NowMonth, NowDay, NowYear : integer;
This declares the variables that are used within the code.