Step 3. Add Search Logic
FRM065 - Using List Components
In this step you will program the SEARCH.Click event to select from the file iiiEmployees based on the
radio button settings. The select logic will populate the list view EMPLOYS for each search.1. Create a
Event for the SEARCH button2. Use the Command Assistant to create a Group_By, EMPDATA, containing the fields required to populate list view EMPLOYS. Your code should look like the following:
Group_By Name(#empdata) Fields(#IIIEMPLOYNUMBER #IIIDEPTCODE #IIISURNAME #IIIGIVENNAME #IIISTARTDATE #IIIDEPPJF)
Define the Group_by at the form level, below the component definitions. It will later be used in another routine.
Note: You could have defined the Group_by in the SEARCH.Click event an itt could still be used in other routines.
3. In the SEARCH.Click event routine, complete the SELECT logic to perform if the RDBN_1 is checked. This should search file iiiEmployees on surname, using a logical file, allowing a generic search.
a. Add a logical file
to your employee file iiiEmployees.b. Give the logical file a key of SURNAME.
c. Compile your file. Review your compile options carefully. You need to rebuild
and .Complete your search logic based on the following pseudo code:
Clear list EMPLOYS
If RDBN_1 is checked
Select fields EMPDATA from file iiiVEMP02, with key iiiSRCNME with generic = yesbul
Set up field FULLNAME
Add entry to list EMPLOYS
End select
Endif
Change fields in the Group_by EMPDATA to *default values
Your code should now look like the following:
Evtroutine Handling(#SEARCH.Click)
Clr_List Named(#EMPLOYS)
* Surname search
If (#RDBN_1.buttonChecked = true)
Select Fields(#empdata) From_File(iiivemp02) With_Key(#iiiSRCNME) Generic(*YES)
#fullname := #iiiSurname + ', ' + #iiiGivenName
Add_Entry To_List(#EMPLOYS)
Endselect
#EMPDATA := *default
Endif
Endroutine
4. Compile and test your form. Review exercise
if necessary to check what records were added to file iiiEmployees. In REP005 exercise you created a maintenance form for iiiEmployees. Use this to add more records if required.The SELECT which is using GENERIC(*YES) will retrieve all records if the search key is blank.
The list view should now be populated using the Surname search option.
5. Create another logical for the employee file iiiEmployees:
a. Add a logical file iiiVEMP03 – By Start Date.
b. Add a key of iiiStartDate.
c. Compile the file and rebuild logical files and OAM.
6. Extend the SEARCH.Click event to handle the start date search (RDBN_2 checked). The SELECT command has an OPTIONS() parameter which provides additional read options such as read using start key and read backwards. Review the SELECT command in the for further details.
Extend your SEARCH.Click logic based on the following
If RDBN_2 is checked
Select fields EMPDATA from file iiiVEMP03, with key iiiSRCDTE using Options, *STARTDATE and *BACKWARDS
Set up field FULLNAME
Add entry to list EMPLOYS
End select
Endif
Your additional code should look like the following:
If (#RDBN_2.buttonChecked = true)
Select Fields(#empdata) From_File(iiivemp03) With_Key(#iiiSRCDTE) Options(*STARTKEY *BACKWARDS)
#fullname := #iiiSurname + ', ' + #iiiGivenName
Add_Entry To_List(#EMPLOYS)
Endselect
Begincheck
If (#EMPLOYS.items.itemcount = *zeroes)
Message Msgtxt('No Employees from this Start Date')
Set_Error For_Field(#iiiSRCDTE)
Endif
Endcheck
Endif
7. Compile and test your form. If you added employees with start dates earlier than the current date, enter todays date as your search value. When searching by Start Date, the latest date should be at the top of the list view.
8. Extend your SEARCH.Click event to handle the Location search (search by department code - iiiSRCDEP). The SELECT command on this occasion should read the logical file iiiEmpByDeptView, with a key of iiiSRCDEP.
Since iiiSRCDEP must exist on the file iiiDepartments (see validation rule at file level, in file iiiEmployees for field iiiDeptCode) your program could check for valid department before continuing the search.
Extend your SEARCH.Click logic based on:
If RDBN_3 is checked
Begin check
Filecheck field iiiSRCDEP in file iiiFILDEPT. Issue a message if not found.
End check
Select fields EMPDATA from file iiiVEMP01, with key iiiSRCDEP.
Set up field FULLNAME
Add entry to list EMPLOYS
End select
Endif
Your additional code should look like the following:
* Location Search
If (#RDBN_3.buttonChecked = true)
Begincheck
Filecheck Field(#iiiSRCDEP) Using_File(iiiDepartments) Msgtxt('Department Not Found')
Endcheck
Select Fields(#empdata) From_File(iiiEmpByDeptView) With_Key(#iiiSRCDEP)
#fullname := #iiiSurname + ', ' + #iiiGivenName
Add_Entry To_List(#EMPLOYS)
Endselect
Endif
9. Compile your form and test it. Select Search by Location, enter an invalid department code and click the Search button.
Based on exercise
you should be aware that the FILECHECK will highlight the field iiiSRCDEP and issue a message, if the field does not exist in file iiiDepartments. The ENDCHECK will branch to the ENDROUTINE on error.Ensure that the list view is populated when a valid department is entered for which employees exist.
Sample Solution for SEARCH.Click Event Handling Routine
Evtroutine Handling(#SEARCH.Click)
Clr_List Named(#EMPLOYS)
* Surname search
If (#RDBN_1.buttonChecked = true)
Select Fields(#empdata) From_File(iiivemp02) With_Key(#iiiSRCNME) Generic(*YES)
#fullname := #iiiSurname + ', ' + #iiiGivenName
Add_Entry To_List(#EMPLOYS)
Endselect
Endif
* Start Date Search
If (#RDBN_2.buttonChecked = true)
Select Fields(#empdata) From_File(iiivemp03) With_Key(#iiiSRCDTE) Options(*STARTKEY *BACKWARDS)
#fullname := #iiiSurname + ', ' + #iiiGivenName
Add_Entry To_List(#EMPLOYS)
Endselect
Begincheck
If (#EMPLOYS.items.itemcount = *zeroes)
Message Msgtxt('No Employees from this Start Date')
Set_Error For_Field(#iiiSRCDTE)
Endif
Endcheck
Endif
* Location Search
If (#RDBN_3.buttonChecked = true)
Begincheck
Filecheck Field(#iiiSRCDEP) Using_File(iiiDepartments) Msgtxt('Department Not Found')
Endcheck
Select Fields(#empdata) From_File(iiiEmpByDeptView) With_Key(#iiiSRCDEP)
#fullname := #iiiSurname + ', ' + #iiiGivenName
Add_Entry To_List(#EMPLOYS)
Endselect
Endif
Endroutine