16. Table Fields : How Table Fields Can Be Used in an Application : loadtable Statement—Load a Data Set
 
Share this page                  
loadtable Statement—Load a Data Set
To place data in a data set underlying an initialized table field, use the loadtable statement. Each execution of the loadtable statement adds one row of values to the data set. It is possible to load more rows than the table field can display. The user can access unseen rows by scrolling to them.
This statement has the following syntax:
loadtable formname tablename
(columnname = data {, columnname = data});
formname
Specifies the name of the form in which the table field tablename is displayed
tablemode
Specifies one of the display modes described in Table Field Display Modes (read, update, query or fill). The default mode is fill.
columnname
Specifies the name of the table field column into which data is being loaded. It can be either a displayed or a hidden column.
data
Specifies the value, either a constant or a program variable, to be loaded into the column. Any column not specified is given a value of zero or blanks or a null, depending on its data type and whether the column is nullable.
Forms statements that refer to a column name must always refer to the column's internal name, as defined in VIFRED or as specified in the inittable statement for hidden columns. This name does not necessarily bear any relationship to the column's title, which appears above the column on the form.
By default, rows loaded into a table field using loadtable have a state of UNCHANGED. (See Table Field Row States in this chapter for more information about states.) You can, however, override this default when you load the table. This is done by specifying the columnname as the constant _state and assigning it a value corresponding to an alternate row state, such as UNDEFINED or NEW. See Setting Row States.
The loadtable statement can occur at any point in the program after the table field has been initialized. If it is included either before or during the initialization of a display loop (see the following example), the values loaded appear when the form is first displayed. It can also appear inside an activate section, so that the table field is loaded as a result of the user selecting an operation.
This example initializes and loads a table field and its data set with values retrieved from a database table:
exec sql include sqlca;
 /* Data declarations listed earlier */
exec sql whenever sqlerror stop;
 exec sql connect personnel;
 exec sql declare emp_cursor cursor for
     select ename, age, sal, eno
     from employee
     where dept = :dept;
 exec frs forms;
exec frs forminit empform;
exec frs inittable empform employee update
     (sal = float4, eno = integer4);
 /* Get the department name and retrieve information */
exec frs prompt ('Enter department name: ', :dept);
exec sql select floor
     into :floor
     from department
     where dept = :dept;
 /* Display the form and initialize the fields */
exec frs display empform;
exec frs initialize (department = :dept, floor = :floor);
exec frs begin;
     exec sql open emp_cursor;
     exec sql whenever not found goto done;
    loop until no more rows
         exec sql fetch emp_cursor
              into :ename, :age, :sal, :eno;
         exec frs loadtable empform employee
              (ename = :ename, age = :age
              sal = :sal, eno = :eno);
         end loop;
 done:
exec sql close emp_cursor;
exec frs end;
 exec frs finalize;
 /* Update database table */
exec frs endforms;
exec sql disconnect;
This example prompts the user for a department name, fills the simple fields of the form with information on that department and then fills the table field with rows for the employees in the department. The loadtable statement is included as part of the initialization of the form's display loop; thus when the form appears, the table field already contains rows of values. Because the table field is in update mode, the runtime user can browse and update the rows in the table field. After the rows have been updated, the program must access the values in each row to use them to update the database table. This process is accomplished with the unloadtable statement.