Step 4. Fill the Lists
FRM055 - List Component Basics
Important Concept
- Sections belong to a department. If a new department is selected, the SECTS combo box needs to be rebuilt.
- Employees belong to a section. If a new section is selected, the EMPLOYS list view needs to be rebuilt.
1. Create a subroutine (SUBROUTINE / ENDROUTINE) to build, rebuild the SECTS combo box. Give the subroutine a Name of SECTS. Once again your logic should use the CLR_LIST, SELECT, ADD_ENTRY, ENDSELECT commands. This time your SELECT needs to read the file SECTAB with a key of DEPTMENT.
A subroutine can be defined anywhere in your form code, but not inside an event or method routine.
Note:
- File SECTAB is keyed on DEPTMENT and SECTION. You need to select records using the high level key DEPTMENT only.
- You should now be aware that a selected entry in combo box DEPTS will automatically populate the variable DEPTMENT in the form code.
Your code should look like the following:
Subroutine Name(SECTS)
Clr_List Named(#SECTS)
Select Fields(#SECTS) From_File(sectab) With_Key(#deptment)
Add_Entry To_List(#SECTS)
Endselect
Endroutine
2. A subroutine is executed using the EXECUTE command. Add code at the end of the form Initialize event routine to execute SECTS. Your code should now look like the following:
Evtroutine Handling(#com_owner.Initialize)
Set Com(#com_owner) Caption(*component_desc)
Clr_List Named(#DEPTS)
Select Fields(#DEPTS) From_File(deptab)
Add_Entry To_List(#DEPTS)
Endselect
Execute Subroutine(SECTS)
Endroutine
3. The SECTS combo box will need to be rebuilt if a new department is selected in DEPTS. Create an ItemGotSelection event routine for DEPTS. Select the DEPTS combo box and do this from the
tab or use the context menu on the DEPTS combo box.Execute subroutine SECTS in this event routine. Your code should look like the following:
Evtroutine Handling(#DEPTS.ItemGotSelection) Options(*NOCLEARMESSAGES *NOCLEARERRORS)
Execute Subroutine(SECTS)
Endroutine
4. Compile and test your form. Notice that when the form initially loads, the first section is not displayed although the SECTS combo box contains the correct entries.
Notice also that if you select a different department, the SECTS combo box contains the correct entries but continues to display the last selected entry, until a valid entry is selected in SECTS.
5. Correct this behavior by adding the following code to the end of the SECTS subroutine:
Get_Entry Number(1) From_List(#SECTS)
#SECTS.currentItem.focus := true
The GET_ENTRY will retrieve a specific entry number from a list. Usually it is executed after logic which locates an entry, for example LOC_ENTRY. See a later exercise for an example of this. In this particular case you are positioning to the top of the SECTS list.
CurrentItem is the current row in the list component. CurrentItem.focus is the focus property of the current list item.
To discover these properties for any component use
help for that component, in this case combo box SECTS
Expand the CurrentItem by double clicking on the list item, PRIM_CBIT.
Expand the list item properties to find the Focus property.
6. Compile and retest your form. When it initially loads it should now look like the following:
Try selecting a new department. The first section in the list will always be displayed.
7. When it initially loads, the form should load all three lists
Create subroutine EMPLOYS. Add logic to this subroutine to clear the list EMPLOYS, select from logical file PSLMST1 with a key of DEPTMENT and SECTION and add entries to the EMPLOYS list view. Your code should look like the following:
Subroutine Name(EMPLOYS)
Clr_List Named(#EMPLOYS)
Select Fields(#EMPLOYS) From_File(pslmst1) With_Key(#deptment #section)
Add_Entry To_List(#EMPLOYS)
Endselect
Endroutine
8. Add an execute subroutine EMPLOYS to the end of subroutine SECTS. The EMPLOYS list will be rebuilt every time the SECTS combo box is rebuilt. Your code should look like the following:
. . . . .
Get_Entry Number(1) From_List(#SECTS)
#SECTS.currentItem.focus := true
Execute Subroutine(emPLOYS)
Endroutine
9. Create an ItemGotSelection event routine for combo box SECTS. Add code to execute subroutine EMPLOYS. Your code should look like the following:
Evtroutine Handling(#SECTS.ItemGotSelection) Options(*NOCLEARMESSAGES *NOCLEARERRORS)
Execute Subroutine(EMPLOYS)
Endroutine
10. Compile and test your form.
- When the form initially loads, all three lists should be populated.
- When a new department is selected, the SECTS list and EMPLOYS list should be rebuilt
- When a new section is selected, the EMPLOYS list should be rebuilt. Note that there may be some sections for which no employees exist.