Examples for OPEN
Example 1: Opening a Keyed Database File
Open FileID(VC2EMP) Usage(*Both) AccMth(*Key)
This example opens the file VC2EMP for both input and output operations. When reading from the file keys will be used for random access.
Example 2: Opening a File as Externally Described
/******************************************************/
/* This program has one (required) parameter, the */
/* name of a member within the QCLSRC source file */
/* */
/* The program will list the contents of the */
/* specified source member. Call this example from */
/* the command line in order to see the listing. */
/******************************************************/
Pgm Parm(&Member)
Dcl Var(&Member) Type(*Char) Len(10)
/******************************************************/
/* Declare our variables: */
/* The DCLFCLF will provide the external definition */
/* for QCLSRC */
/* The DCLINDCLF will provide the indicator to */
/* detect End of File (&EOF) */
/******************************************************/
File FileID(QCLSRC)
Inds CLFInd(*Yes)
/******************************************************/
/* Test to see if the &MEMBER parameter was passed */
/* (only needed for ILE programs). If not, end with */
/* an error message. */
/******************************************************/
ChgVar Var(&Member) Value(&Member)
MonMsg MsgID(MCH3601) Exec(Do)
RmvMsg Clear(*All)
SndPgmMsg Msg( +
'Member name is a required parameter')
Return
EndDo
/******************************************************/
/* Open the file, this time with (by default) LVLCHK */
/* */
/* Read the first record of the specified member */
/* */
/* Enter a DoWhile loop conditioned by End of File */
/* (&EOF). So long as EOF is not detected: */
/* 1. Send the line of source read to the default */
/* (*PRV) call message queue */
/* 2. Read the next QCLSRC record and loop */
/******************************************************/
Open FileID(QCLSRC) Mbr(&Member)
Read FileID(QCLSRC) EOF(&EOF)
DoWhile Cond(*Not &EOF)
SndPgmMsg Msg(&SrcDta)
Read QCLSRC EOF(&EOF)
EndDo
/******************************************************/
/* When End of File close the file and return */
/******************************************************/
Close QCLSRC
EndPgm
The source for this example can be found in member RPG_OPNF of source file VC2CLSRC in library VC2CLF.
To compile RPG_OPNF into QTEMP you can use the command
CrtBndCLF Pgm(QTEMP/RPG_OPNF) SrcFile(VC2CLF/VC2CLSRC)
To run the program you can use any of the following commands
Call Pgm(RPG_OPNF) Parm(RPG_OPNF) - list member RPG_OPNF
Call Pgm(RPG_OPNF) Parm(*First) - list the first member
Call Pgm(RPG_OPNF) Parm(*All) - list all members
Example 3: Opening Database and Display Files with External Definitions
Pgm
/******************************************************/
/* Declare the Employee master file VC2EMP, display */
/* VC2EMPDSP1, and CLF related indicators */
/******************************************************/
File FileID(VC2EMP)
File FileID(VC2EMPDSP1)
Inds CLFInd(*Yes)
/******************************************************/
/* Declare the packed decimal field &RcdCnt. This */
/* will be used to determine the current record count */
/* of VC2EMP. If the file is empty (&RCDCNT = 0), */
/* prompt with how to load the file with sample data. */
/******************************************************/
Dcl Var(&RcdCnt) Type(*Dec) Len(10 0)
/******************************************************/
/* Open VC2EMP and verify that records have been */
/* loaded into the file. If not, tell the user how to */
/******************************************************/
Open FileID(VC2EMP) Usage(*Both) AccMth(*Key)
RtvFInfCLF VC2EMP NbrRcds(&RcdCnt)
If Cond(&RcdCnt = 0) Then(Do)
Open VC2FEMPTY Usage(*Both) LvlChk(*No)
Exfmt FileID(VC2FEmpty) RcdFmt(EMPEMP)
Close VC2FEMPTY
Close VC2EMP
Return
EndDo
/******************************************************/
/* Having verified that records exist now open the */
/* display file for the program. */
/******************************************************/
Open VC2EMPDSP1 Usage(*Both)
/******************************************************/
/* Enter a DOWHILE loop until F3 is used */
/* */
/* Within the loop: */
/* 1 Write and read the VC2EMPDSP1 *DSPF record */
/* format PROMPT. PROMPT asks for an employee */
/* number. A valid employee would be 1. The user */
/* can press ENTER or F3 from PROMPT. */
/* 2 If F3 is pressed leave the DoWhile loop */
/* 3 Otherwise read the employee record for the */
/* employee number entered. */
/* 4 If no record is found (&RNF is '1') a message */
/* is displayed based on &IN50 */
/* 5 If a record is found record format CHANGE is */
/* written and read. */
/* CHANGE allows the user to maintain employee */
/* information (ENTER), delete the employee (F23), */
/* return to PROMPT without updating the employee */
/* record (F12), or exit the application (F3). */
/* 6 The user is then returned to the PROMPT display */
/* to enter the next employee number */
/******************************************************/
DoWhile Cond(*Not &IN03)
Exfmt RcdFmt(PROMPT)
If Cond(&IN03) Then(Leave)
Chain &EmpNbr VC2EMP RcdNotFnd(&RNF)
If Cond(&RNF) Then(Do)
ChgVar Var(&IN50) Value('1')
Iterate
EndDo
Exfmt RcdFmt(CHANGE)
Select
When Cond(&IN03) Then(Leave)
When Cond(&IN12) Then(Do)
UnLock VC2EMP
Iterate
EndDo
When Cond(&IN23) Then(Delete VC2EMP)
OtherWise Cmd(Update VC2EMP)
EndSelect
EndDo
/******************************************************/
/* When F3 is used close the files and return */
/******************************************************/
Close VC2EMP
Close VC2EMPDSP1
EndPgm
This example opens the VC2EMP physical file for file maintenance. As the program allows a user to view, update, and delete records in the VC2EMP database the database file is opened for keyed IO and both read and write.
RPG_EMPU1, when initially called, verifies that there are records in the VC2EMP file. If not, the program displays a prompt to inform the caller that records need to be loaded prior to running the program. The display file used for this prompt, VC2FEMPTY, is not declared within the program using the DCLFCLF command. Because DCLFCLF was not used the developer needs to supply both the FILEID and the RCDFMT keywords when using the EXFMT command with the record format EMPEMP. The developer does not have to specify both keywords when using the EXFMT command with record formats of the VC2EMPDSP1 display file. In the case of VC2EMPDSP1 the developer used the DCLFCLF command and the precompiler is able to determine the FILEID to use based on only the RCDFMT keyword being provided by the developer.
The source for the example can be found in member RPG_EMPU1 of source file VC2CLSRC in library VC2CLF. The source for physical file VC2EMP can be found in member VC2EMP of source file QDDSSRC in library VC2CLF. The source for display file VC2EMPDSP1 can be found in member VC2EMPDSP1 of source file QDDSSRC in library VC2CLF. The source for display file VC2FEMPTY can be found in member VC2FEMPTY of source file QDDSSRC in library VC2CLF.
All objects referenced by RPG_EMPU1 should currently exist in library VC2CLF. To compile example RPG_EMPU1 into QTEMP you can use the commands
AddLibLE Lib(VC2CLF)
CrtBndCLF Pgm(QTEMP/RPG_EMPU1) SrcFile(VC2CLSRC)
To test the program you can use the command
Call Pgm(QTEMP/RPG_EMPU1)
From the prompt screen you can enter an employee number such as 52. After using the ENTER key you should see the details of employee 52's VC2EMP record. From this screen you can use command key 23 to delete the record, ENTER to update the record with any changes you may have keyed in, command key 12 to exit the screen without updating the record, and command key 3 to exit the program.
Example 4: Opening a Display File with a Separate Indicator Area
Pgm
/***********************************************/
/* Declare the Employee master file VC2EMP, */
/* display file VC2EMPDSP2 that is defined */
/* with INDARA specified, and indicators */
/* for both a separate indicator area and for */
/* CLF I/O related commands */
/***********************************************/
File FileID(VC2EMP)
File FileID(VC2EMPDSP2)
Inds CLFInd(*Yes) IndAra(*Yes)
/***********************************************/
/* Open the two files. */
/* */
/* VC2EMPDSP2 is opened with a SEPINDARA(*YES) */
/***********************************************/
Open FileID(VC2EMP) Usage(*Both) AccMth(*Key)
Open FileID(VC2EMPDSP2) Usage(*Both) +
SepIndara(*Yes)
/***********************************************/
/* Enter a DOWHILE loop that is exited when */
/* command key 3 is pressed. */
/* */
/* Within the loop: */
/* 1 Write and read the VC2EMPDSP2 *DSPF */
/* record format PROMPT. PROMPT asks for */
/* employee number. A valid employee */
/* number would be 00001. The user can */
/* press ENTER or command key 3. */
/* 2 If command key 3 was pressed leave the */
/* DoWhile loop */
/* 3 Read the employee record for the */
/* employee number entered. */
/* 4 If no record is found (&RNF is '1') a */
/* message is displayed based on &IN50 */
/* 5 If a record is found record format CHANGE */
/* is written and read. */
/* CHANGE allows the user to maintain */
/* employee information (ENTER), delete the */
/* employee (F23), return to PROMPT without */
/* updating the employee record (F12), or */
/* exit the application (F3). */
/* 6 The user is then returned to the PROMPT */
/* display to enter the next employee */
/* number */
/***********************************************/
DoWhile Cond(*Not &IN03)
ExFmt RcdFmt(PROMPT)
If Cond(&IN03) Then(Leave)
Chain &EmpNbr FileID(VC2EMP) +
RcdNotFnd(&RNF)
If Cond(&RNF) Then(Do)
ChgVar Var(&IN50) Value('1')
Iterate
EndDo
ExFmt RcdFmt(CHANGE)
Select
When Cond(&IN03) Then(Leave)
When Cond(&IN12) Then(Do)
UnLock VC2EMP
Iterate
EndDo
When Cond(&IN23) Then(Delete VC2EMP)
OtherWise Cmd(Update VC2EMP)
EndSelect
EndDo
/***********************************************/
/* When F3 is used to exit the DoWhile loop */
/* close the files and return */
/***********************************************/
Close VC2EMP
Close VC2EMPDSP2
EndPgm
This example demonstrates the use of a separate indicator area when using a display file. The only significant changes from the RPG_EMPU1 sample program are:
- changing the display file from VC2EMPDSP1 to VC2EMPDSP2
- adding INDARA(*YES) to the DCLINDCLF command
- adding SEPINDARA(*YES) to the OPEN command for display file VC2EMPDSP2
The source for the example can be found in member RPG_EMPUS of source file VC2CLSRC in library VC2CLF. The source for physical file VC2EMP can be found in member VC2EMP of source file QDDSSRC in library VC2CLF. The source for display file VC2EMPDSP2 can be found in member VC2EMPDSP2 of source file QDDSSRC in library VC2CLF.
The display file DC2EMPDSP2 should currently exist in library VC2CLF. To compile example RPG_EMPUS into QTEMP you can use the commands
AddLibLE Lib(VC2CLF)
CrtBndCLF Pgm(QTEMP/RPG_EMPUS) SrcFile(VC2CLSRC)
To test the program you can use the command
Call Pgm(QTEMP/RPG_EMPUS)
From the prompt screen you can enter an employee number such as 52. After using the ENTER key you should see the details of employee 52's VC2EMP record. From this screen you can use command key 23 to delete the record, ENTER to update the record with any changes you may have keyed in, command key 12 to exit the screen without updating the record, and command key 3 to exit the program.
Example 5: Opening Database and Printer Files with External Definitions
Pgm
/******************************************************/
/* This program creates a printed report listing */
/* employees in key sequence using the VC2EMPDPT */
/* access path (Dept/First name) */
/******************************************************/
/******************************************************/
/* Declare two working variables: */
/* &CURLIN - the current line number printed */
/* &PAGOVR - the overflow line number for a page */
/******************************************************/
Dcl Var(&CurLin) Type(*Dec) Len(3 0)
Dcl Var(&PagOvr) Type(*Dec) Len(3 0)
/******************************************************/
/* Declare our files and indicators */
/* VC2EMPDPT - Employees by department */
/* VC2POSPRT - External printer file */
/* CLF Indicators - &EOF used in program */
/******************************************************/
File FileID(VC2EMPDPT)
File FileID(VC2POSPRT)
Inds CLFInd(*Yes)
/******************************************************/
/* Open the files */
/******************************************************/
Open FileID(VC2EMPDPT) AccMth(*KEY)
Open VC2POSPRT Usage(*Output)
/******************************************************/
/* Retrieve the page overflow line number */
/******************************************************/
RtvFInfCLF FileID(VC2POSPRT) PrtFOvrFlw(&PagOvr)
/******************************************************/
/* Write titles for report on first page */
/******************************************************/
Write RcdFmt(HEADING)
/******************************************************/
/* Read the first record from VC2EMPDPT */
/******************************************************/
Read VC2EMPDPT EOF(&EOF)
DoWhile Cond(*Not &EOF)
/*******************************************/
/* While we have not reached End of File */
/* on VC2EMPDPT: */
/* 1 Retrieve the current print line number*/
/* 2 If the current print line number is */
/* greater than or equal to the overflow */
/* line number then start a new page and */
/* re-write titles on the page */
/* 3 Write the detail employee print line */
/* 4 Read the next employee record */
/* 5 Continue until End of File */
/*******************************************/
RtvFInfCLF VC2POSPRT CurPrtLine(&CurLin)
If Cond(&CurLin *GE &PagOvr) Then( +
Write RcdFmt(HEADING))
Write RcdFmt(DETAILS)
Read VC2EMPDPT EOF(&EOF)
EndDo
/******************************************************/
/* At End of File close the files and return */
/******************************************************/
Close OPNID(VC2EMPDPT)
Close VC2POSPRT
EndPgm
The example program opens the VC2EMPDPT logical file and creates a report using the external printer file VC2POSPRT. The report lists all employees in the CLF provided sample databases. The VC2EMPDPT logical file is keyed by employee first name within department.
The source for the example can be found in member RPG_POSPRT of source file VC2CLSRC in library VC2CLF. The source for logical file VC2EMPDPT can be found in member VC2EMPDPT of source file QDDSSRC in library VC2CLF. The source for printer file VC2POSPRT can be found in member VC2POSPRT of source file QDDSSRC in library VC2CLF.
The logical file VC2EMPDPT and printer file VC2POSPRT should currently exist in library VC2CLF. To compile the RPG_POSPRT program into QTEMP you can use the commands
AddLibLE Lib(VC2CLF)
CrtBndCLF Pgm(QTEMP/RPG_POSPRT) SrcFile(VC2CLSRC)
Example 6: Opening a Database File Under Commitment Control
Pgm
/***********************************************/
/* Declare the Employee master file VC2EMP and */
/* indicators used with CLF I/O Commands */
/***********************************************/
File FileID(VC2EMP)
Inds CLFInd(*Yes)
/***********************************************/
/* Declare &ANSWER variable. This is used to */
/* control whether program commits or rolls */
/* back database changes */
/***********************************************/
Dcl Var(&Answer) Type(*Char) Len(1)
/***********************************************/
/* Setup logic that would not typically be */
/* in an application program. For */
/* 1 End journaling of VC2EMP if it is active */
/* (it should not be) */
/* 2 Delete journal CLFJRN in VC2CLF if it */
/* exists (it should not) */
/* 3 Delete journal receiver CLFJRNRCV in */
/* VC2CLF if it exists (it should not) */
/* 4 Create journal receiver CLFJRNRCV */
/* 5 Clears physical file VC2EMP and VC2DPT */
/* 6 Creates example program DEV_LOAD (that */
/* loads records into the VC2EMP and */
/* VC2DPT database file) into QTEMP */
/* 7 Calls DEV_LOAD */
/* 8 Starts journaling of VC2EMP */
/* 9 Starts commitment control */
/***********************************************/
EndJrnPF File(VC2CLF/VC2EMP)
MonMsg MsgID(CPF0000)
DltJrn Jrn(VC2CLF/CLFJRN)
MonMsg MsgID(CPF0000)
DltJrnRcv JrnRcv(VC2CLF/CLFJRNRCV) DltOpt(*IgnInqMsg)
MonMsg MsgID(CPF0000)
CrtJrnRcv JrnRcv(VC2CLF/CLFJRNRCV)
CrtJrn Jrn(VC2CLF/CLFJRN) JrnRcv(VC2CLF/CLFJRNRCV)
ClrPFM File(VC2CLF/VC2EMP)
ClrPFM File(VC2CLF/VC2DPT)
CrtBndCLF Pgm(QTEMP/DEV_LOAD) SrcFile(VC2CLF/VC2CLSRC)
Call Pgm(QTEMP/DEV_LOAD)
StrJrnPF File(VC2CLF/VC2EMP) Jrn(VC2CLF/CLFJRN) +
Images(*Both)
StrCmtCtl LckLvl(*Chg)
/***********************************************/
/* With setup done, the application program: */
/* 1 Opens the VC2EMP database file for both */
/* input and output, keyed access, and under */
/* commitment control */
/* 2 Reads by key the employee record for */
/* employee number 52 */
/* 3 If employee 52 is not found then the */
/* previous setup logic failed and the */
/* program displays an error message */
/* 4 If employee 52 is found the program: */
/* 4A Deletes the employee */
/* 4B Prompts the user to commit the */
/* deletion of employee 52 */
/* 4C If the user responds 'Y' the delete */
/* is committed */
/* If the user responds 'N' the delete */
/* is rolled back */
/* 5 Reads by key the employee record for */
/* employee number 52 */
/* 6 Displays a message to the user about */
/* whether or not the record was found */
/***********************************************/
Open FileID(VC2EMP) Usage(*Both) AccMth(*Key) +
CmtCtl(*Yes)
Chain (52) VC2EMP RcdNotFnd(&RNF)
If Cond(&RNF) Then(SndUsrMsg MsgID(VC25051) +
MsgF(VC2CLF/VC2MSG))
Else Cmd(Do)
Delete FileID(VC2EMP)
SndUsrMsg Msg('Commit delete? Y or N') +
Values(Y N) MsgRpy(&Answer)
If Cond(&Answer *EQ Y) Then(Commit)
Else Cmd(RollBack)
Chain (52) VC2EMP RcdNotFnd(&RNF)
If Cond(&RNF) Then(SndUsrmsg +
MsgID(VC25052) MsgF(VC2CLF/VC2MSG))
Else Cmd(SndUsrMsg MsgID(VC25053) +
MsgF(VC2CLF/VC2MSG))
EndDo
/***********************************************/
/* Having completed the transaction the */
/* program now: */
/* 1 Closes the VC2EMP database file */
/* 2 Ends commitment control */
/* 3 Ends journaling of VC2EMP */
/* 4 Deletes the journal CLFJRN */
/* 5 Deletes the journal receiver CLFJRNRCV */
/* 6 Returns */
/***********************************************/
Close OpnID(VC2EMP)
EndCmtCtl
EndJrnPF File(VC2CLF/VC2EMP)
DltJrn Jrn(VC2CLF/CLFJRN)
DLtJrnRcv JrnRcv(VC2CLF/CLFJRNRCV) DltOpt(*IgnInqMsg)
EndPgm
The source for the example can be found in member RPG_CMTCTL of source file VC2CLSRC in library VC2CLF. The source for physical file VC2EMP can be found in member VC2EMP of source file QDDSSRC in library VC2CLF.
The VC2EMP database file should currently exist in library VC2CLF. To create RPG_CMTCTL into QTEMP you can use the commands
AddLibLE Lib(VC2CLF)
CrtBndCLF Pgm(QTEMP/RPG_CMTCTL) SrcFile(VC2CLSRC)
To run RPG_CMTCTL you can use the command
Call Pgm(QTEMP/RPG_CMTCTL)