Active Columns using JScript

This topic adds three active columns to the Employees table from the Nwind.mdb sample database.

 

images\eg_activej_01.gif

 

The Employees table has fields for Employee ID, Last Name, First Name, Birth Date and Hire Date. We will add three active columns that provide the full name of the employee, the age of the employee and the years since the employee was hired.

 

Step 1: Add Active Column

 

Open the Employees table and right click onto any of the column heads and choose Add - Active Column.

 

images\eg_activej_02.gif

 

In the Add Active Column dialog. Provide the name of the function for the Full Name column and other parameters as shown above, including choosing JScript as the language to use. The function name used is the name of the function that we will write into the script in the next step. Press OK and the Employees Script for the Employees table will be created and opened for editing.

 

Step 2: Enter the script

 

To save time, we will enter the functions for all three active columns into the script component at this time:

 

// compose full name from [Last Name] and [First Name]

function fullName() {

 return Record.Data("Last Name") + ", " + Record.Data("First Name");

}

 

// return number of years since given date to current moment

function years(arg) {

 today = new Date();

 todayYrs = today.getFullYear();

 query = new Date(arg);

 queryYrs = query.getFullYear();

 

 return todayYrs - queryYrs;

}

 

// return number of years since [Birth Date] to current moment

function yearsOld() {

 return years(Record.Data("Birth Date"));

}

 

// return number of years since [Hire Date] to current moment

function yearsWorking() {

 return years(Record.Data("Hire Date"));

}

 

Step 3: Add remaining Active Columns

 

Add the remaining active columns by right clicking onto any column head in the table and choosing Add - Active Column.

 

images\eg_activej_04.gif

 

First we add the active column called Years Old. This active column uses the yearsOld function written into the script in the previous step.

 

images\eg_activej_05.gif

 

Next we add the active column called Years Working. This active column uses the yearsWorking function written into the script in the previous step.

 

images\eg_activej_06.gif

 

The result of the above is that three new columns appear in our table which are computed using the functions in the script. Note that two of the functions use the years function defined in the script.

 

See Also

 

See the Scripts topic for information on scripting. See the Active Columns topic for more on active columns.