An Extended Example
The following example presents an embedded application, designed to illustrate some common features of forms-based applications. This example uses the employee table in the personnel database. The employee table has the following description:
exec sql declare employee table
(eno integer2 not null,
ename char(20) not null,
age integer1 not null,
sal float4 not null,
dept integer2 not null);
The form used in the application, empform, contains fields that correspond in name and type to the columns in the employee table.
The application allows the runtime user to retrieve an employee's data based on a unique employee number and to update or delete the data.
exec sql include sqlca;
exec sql begin declare section;
eno integer;
ename character_string(20);
age integer;
sal float;
dept integer;
queried integer;
/* Flag indicates "Query" menu item selected */
found integer;
/* Flag indicates some rows were found */
exec sql end declare section;
/*
** STOP on database errors,
** but CONTINUE on database warnings and
** NOT FOUND conditions.
*/
exec sql whenever sqlerror stop;
exec sql connect personnel;
exec frs forms;
exec frs forminit empform;
exec frs display empform;
exec frs initialize;
exec frs begin;
queried = 0; /* No "Query" selected yet */
exec frs end;
exec frs activate menuitem 'Help';
exec frs begin;
exec frs submenu;
exec frs activate menuitem 'Application';
exec frs begin;
/* provide help about current application */
exec frs help_frs (subject = 'Application',
file = 'applicat.hlp');
exec frs end;
exec frs activate menuitem 'MenuOperations';
exec frs begin;
/* provide help about current menu operations */
exec frs help_frs (subject = 'Menu',
file = 'menu.hlp');
exec frs end;
exec frs end;
exec frs activate menuitem 'Query';
exec frs begin;
exec frs getform empform (:eno = eno);
if (eno = 0) then
exec frs message
'You must enter an employee number 0';
exec frs sleep 2;
exec frs resume field eno;
end if;
found = 0;
exec sql select ename, age, sal, dept, 1
into :ename, :age, :sal, :dept, :found
from employee
where eno = :eno;
if (found = 0) then
exec frs message 'No employee found';
exec frs sleep 2;
exec frs clear field eno;
else
exec frs putform empform
(eno = :eno, ename = :ename, age = :age,
sal = :sal, dept = :dept);
queried = 1;
end if;
exec frs end;
exec frs activate menuitem 'Update';
exec frs begin;
if (queried = 0) then
exec frs message
'You must Query before you Update';
exec frs sleep 2;
exec frs resume;
end if;
exec frs validate;
/*Confirm data is valid before updating */
exec frs getform empform
(:eno = eno, :ename = ename, :age = age,
:sal = sal, :dept = dept);
exec sql update employee
set ename = :ename, age = :age, sal = :sal,
dept = dept
where eno = :eno;
exec sql commit;
queried = 0;
exec frs end;
exec frs activate menuitem 'Delete';
exec frs begin;
if (queried = 0) then
exec frs message
'You must Query before you Delete';
exec frs sleep 2;
else
exec frs getform empform (:eno = eno);
exec sql delete from employee where eno = :eno;
exec sql commit;
queried = 0;
end if;
exec frs end;
exec frs activate menuitem 'End', frskey3;
exec frs begin;
exec frs breakdisplay;
exec frs end;
exec frs finalize;
exec frs endforms;
exec sql disconnect;
For more extensive examples of forms applications, consult your host language companion guide.
Last modified date: 08/28/2024