COBOL Tutorial

Progress0 of 0 lessons

COBOL Screen Section

The Screen Section is a part of the Data Division (in COBOL implementations that support it) where you define a full-screen layout: where each label and field appears (line and column) and whether each field is input, output, or both. You then use DISPLAY to show the screen and ACCEPT to read user input. Not all compilers support Screen Section; for example, IBM mainframe COBOL often uses CICS BMS for 3270 screens. This page describes the Screen Section for compilers that do support it (e.g. Micro Focus).

Explain Like I'm Five: What Is the Screen Section?

Instead of writing DISPLAY and ACCEPT one line at a time, you draw the whole screen once: "At line 2, column 10 put the label 'Name:'; at line 2, column 20 put an input field for the name." That layout is the Screen Section. When the program runs, DISPLAY shows the whole form and ACCEPT lets the user type in the input areas.

Where the Screen Section Lives

The Screen Section appears in the Data Division, after other sections (e.g. FILE SECTION, WORKING-STORAGE SECTION). It is introduced with the words SCREEN SECTION. Under it you define one or more screens, each starting with a level 01 entry that has a screen name. Subordinate entries (e.g. 03, 05) define the lines, columns, and field types.

Line and Column Positioning

Each screen item can have LINE and COLUMN (or equivalent clauses, depending on compiler) to fix its position. LINE is the row number; COLUMN is the column number. So you can place a title at LINE 1 COLUMN 25, a label at LINE 3 COLUMN 5, and an input field at LINE 3 COLUMN 25. Exact syntax may be LINE NUMBER IS 5 or LINE 5; check your compiler manual.

Field Types: TO, FROM, USING, VALUE

Screen Section field types
PhraseTypeEffect
VALUE literalLiteral/promptOutput only (fixed text)
FROM data-nameOutput fieldDisplays value of data-name
TO data-nameInput fieldUser input stored in data-name
USING data-nameUpdate fieldShows value and accepts input

VALUE is for fixed text (labels, prompts). FROM displays the value of a data item but does not allow input. TO designates an input field; the value the user types is stored in the named data item. USING shows the current value of the data item and allows the user to change it (display and edit).

Example Screen Section Layout

In this example, MAIN-SCREEN is the 01-level screen name. The first item is a literal at line 1, column 20. The next items are a label and an input field on line 3, and a label and an output/update field on line 4. The data items (e.g. WS-USER-NAME) would be defined in WORKING-STORAGE. Syntax details (e.g. LINE NUMBER IS) depend on your compiler.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
DATA DIVISION. SCREEN SECTION. 01 MAIN-SCREEN. 05 LINE 1 COLUMN 20 VALUE "Customer Entry". 05 LINE 3 COLUMN 5 VALUE "Name: ". 05 LINE 3 COLUMN 20 PIC X(30) TO WS-USER-NAME. 05 LINE 4 COLUMN 5 VALUE "Balance: ". 05 LINE 4 COLUMN 20 PIC Z(7)9.99 FROM WS-BALANCE. PROCEDURE DIVISION. DISPLAY MAIN-SCREEN ACCEPT MAIN-SCREEN *> User can type into WS-USER-NAME; WS-BALANCE is display-only STOP RUN.

DISPLAY and ACCEPT with the Screen

DISPLAY screen-name sends the screen to the terminal: all literals and FROM/USING fields show their current values. ACCEPT screen-name lets the user move the cursor and type into TO and USING fields; when they finish (e.g. press Enter), the input is stored in the corresponding data items. Some systems let you DISPLAY and ACCEPT in one operation or support attributes (e.g. highlight, reverse video) in the screen definition.

Step-by-Step: Defining a Simple Screen

  • In the Data Division, add a SCREEN SECTION and a 01-level screen name.
  • Add entries for each line: use LINE and COLUMN, and VALUE for labels or FROM/TO/USING for data fields linked to Working-Storage items.
  • In the Procedure Division, DISPLAY the screen name to show the form.
  • ACCEPT the screen name so the user can fill in or edit input fields.
  • Use the Working-Storage items that were linked with TO or USING for validation or further processing.

Test Your Knowledge

1. A Screen Section field with TO phrase is used for:

  • Display only
  • Input only
  • Both display and input
  • Neither

2. Where is the Screen Section defined?

  • Procedure Division
  • Environment Division
  • Data Division
  • Identification Division

Related Concepts

Related Pages