Skip to main content

After coding the SORT statement for my text files, Visual Studio\\Visual COBOL throws the "COBCH0: illegal key" error.  I've looked up the error, used existing examples of the SORT code on several tutorials but still getting this error.

I'm using:

Visual COBOL for Visual Studio vs 9.

Windows 10

My code:

IDENTIFICATION DIVISION.
PROGRAM-ID. SortedFileProg.

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.

SELECT INPUTX ASSIGN TO
"C:\\Application Development\\" &
"Training\\Applications\\COBOL\\AppProgram2\\IN.dat"
ORGANIZATION IS LINE SEQUENTIAL.
SELECT OUTPUTX ASSIGN TO
"C:\\Application Development\\" &
"Training\\Applications\\COBOL\\AppProgram2\\OUT.txt".
SELECT WORK ASSIGN TO
"C:\\Application Development\\" &
"Training\\Applications\\COBOL\\AppProgram2\\WRK.txt".
*
CONFIGURATION SECTION.

DATA DIVISION.
FILE SECTION.
FD INPUTX.
01 INPUT-STUDENT.
05 STUDENT-ID-I PIC 9(5).
05 STUDENT-NAME-I PIC A(25).
FD OUTPUTX.
01 OUTPUT-STUDENT.
05 STUDENT-ID-O PIC 9(5).
05 STUDENT-NAME-O PIC A(25).
SD WORK.
01 WORK-STUDENT.
05 STUDENT-ID-W PIC 9(5).
05 STUDENT-NAME-W PIC A(25).
*
WORKING-STORAGE SECTION.

PROCEDURE DIVISON.
SORT WORK
ON ASCENDING KEY STUDENT-ID-O USING INPUT
GIVING OUTPUT.
DISPLAY 'Sort Successful'.

GOBACK.

END PROGRAM SortedFileProg.

********

Input file:

IN.dat

54321PaulABCDEFGHIJKLMNOPQRSTU
12345DerekABCDEFGHIJKLMNOPQRST

Any help would be appreciated.

After coding the SORT statement for my text files, Visual Studio\\Visual COBOL throws the "COBCH0: illegal key" error.  I've looked up the error, used existing examples of the SORT code on several tutorials but still getting this error.

I'm using:

Visual COBOL for Visual Studio vs 9.

Windows 10

My code:

IDENTIFICATION DIVISION.
PROGRAM-ID. SortedFileProg.

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.

SELECT INPUTX ASSIGN TO
"C:\\Application Development\\" &
"Training\\Applications\\COBOL\\AppProgram2\\IN.dat"
ORGANIZATION IS LINE SEQUENTIAL.
SELECT OUTPUTX ASSIGN TO
"C:\\Application Development\\" &
"Training\\Applications\\COBOL\\AppProgram2\\OUT.txt".
SELECT WORK ASSIGN TO
"C:\\Application Development\\" &
"Training\\Applications\\COBOL\\AppProgram2\\WRK.txt".
*
CONFIGURATION SECTION.

DATA DIVISION.
FILE SECTION.
FD INPUTX.
01 INPUT-STUDENT.
05 STUDENT-ID-I PIC 9(5).
05 STUDENT-NAME-I PIC A(25).
FD OUTPUTX.
01 OUTPUT-STUDENT.
05 STUDENT-ID-O PIC 9(5).
05 STUDENT-NAME-O PIC A(25).
SD WORK.
01 WORK-STUDENT.
05 STUDENT-ID-W PIC 9(5).
05 STUDENT-NAME-W PIC A(25).
*
WORKING-STORAGE SECTION.

PROCEDURE DIVISON.
SORT WORK
ON ASCENDING KEY STUDENT-ID-O USING INPUT
GIVING OUTPUT.
DISPLAY 'Sort Successful'.

GOBACK.

END PROGRAM SortedFileProg.

********

Input file:

IN.dat

54321PaulABCDEFGHIJKLMNOPQRSTU
12345DerekABCDEFGHIJKLMNOPQRST

Any help would be appreciated.

The key must be contained within the record defined for the SD file WORK.

STUDENT-ID-O is defined within the FD record for OUTPUTX.

Also the names of the actual files must be specified in USING and GIVING

Stament should read:

SORT WORK
ON ASCENDING KEY STUDENT-ID-W USING INPUTX
GIVING OUTPUTX.


The key must be contained within the record defined for the SD file WORK.

STUDENT-ID-O is defined within the FD record for OUTPUTX.

Also the names of the actual files must be specified in USING and GIVING

Stament should read:

SORT WORK
ON ASCENDING KEY STUDENT-ID-W USING INPUTX
GIVING OUTPUTX.

Thanks Chris that worked.  I'm not sure why the tutorial and samples used the ouput file key in the sort statement.