Step 3. Add Search Logic

In this step, you will program the SEARCH.Click event to select from the table xEmployee based on the search type selected. This is field STD_CODEL. The select logic will populate the employees list view for each search.

1.  Drag a List View control into row 2. On the Layout ribbon, give it a Size of Fit Both. The list will occupy all of row 2. The Name will be ListView1.

2.  On the Repository tab, expand Fields beginning "S" and

a.  Drag the following fields into ListView1: STD_OBJ, STD_NAME and STD_DESC.

b.  Locate the table xEmployee and drag field xDepartmentCode and xEmployeeStartDate into List View1.

c.  Select each column by clicking the column heading and using the Details tab, change the Captions as follows:

Field

Caption

CaptionType

STD_OBJ

Code

Caption

STD_NAME

Surname

Caption

STD_DESC

Given Names

Caption

 

     Note: We are using the STD_ fields to avoid changing the field values which will be shown on the employee details tab sheet, when list entries have focus. Another solution would be to make the tab folder panel a separate reusable part component.

     Your form should look like the following:

3.  Review the table xEmployee's definition on the Repository tab. Note that it has three indexes, which will support two of the required searches.

     For this simple application, we will read all employee records to find those with the required start date. We could do this more efficiently using the SELECT_SQL command. See the example using the SELECT_SQL command in exercise FRM085 - Maintain Employee and Notes.

4.  The Search.Click event should perform the required search using a CASE/ENDCASE loop for field STD_CODEL.

Evtroutine Handling(#SEARCH.Click)

Case Of_Field(#STD_CODEL)

When (= NAME)

When (= DATE)

When (= DEPT)

Endcase

Endroutine
 

5.  At the top of your code, below the Define_Com statements, define a Group_by for the fields in the list:

Group_By Name(#ListFields) Fields(#xEmployeeIdentification #xEmployeeSurname #xEmployeeGivenNames #xDepartmentCode #xEmployeeStartDate)

6.  Create an AddEntry method routine to set up the ListView columns and add an entry.

     Your code should look like the following:

Mthroutine Name(AddEntry)

#std_obj := #xEmployeeIdentification

#std_name := #xEmployeeSurname.AsNativeString

#std_desc := #xEmployeeGivenNames.AsNativeString

Add_Entry To_List(#ListView1)

Endroutine

7.  Complete the name search using the SELECT/ENDSELECT commands to read the index xEmployeeBySurname. The search should use the option Generic(*yes) to search on a partial name. Invoke the AddEntry routine for each employee matching the search value to ListView1.

Evtroutine Handling(#SEARCH.Click)

Clr_List Named(#ListView1)

Case Of_Field(#STD_CODEL) /* search type */

When (= NAME)

Select Fields(#ListFields) From_File(xEmployeeBySurname) With_Key(#iiiSearchName) Nbr_Keys(*compute) Generic(*YES)

Invoke #com_self.AddEntry

Endselect

When (= DATE)

. . .

8.  Compile and test the form. You should be able to search by surname and populate the employees list view. Note that the surname will start with an upper case. Your search value must reflect this. You could convert the first character of the search value to upper case like this:
#iiiSearchName := #iiiSearchName.replacesubstring( 1 1 #iiiSearchName.uppercase.substring( 1 1 ) )

     Another option would be to add a srvroutine to the server module iiixEmployeeDataServer which searches the table using SELECT_SQL using a Where condition which converts both the search value and the surname to uppercase.

9.  Complete the start date search. This should read all employees in a Select/Endselect loop with a Where() parameter which compares xEmployeeStartDate with SearchDate:

When (= DATE)

Select Fields(#ListFields) From_File(xemployee) Where(#xEmployeeStartDate = #SearchDate)

Invoke #com_self.AddEntry

Endselect

When (= DEPT)

. . .

10. Compile and test your form. You should be able to search by start date. Note an employee's start date from a name search and then perform a start date search.

11. Complete the search by department code using the xEmployeeByDepartment index:

When (= DEPT)

#xDepartmentCode := #SearchDepartment

Select Fields(#ListFields) From_File(xEmployeeByDepartment) With_Key(#xDepartmentCode) Generic(*YES)

Invoke #com_self.AddEntry

Endselect

Endcase

. . .

12. Compile and test the form. Remember that department code has values such as 100, 200 and 300. Of course, we could easily improve this search by providing a combo box for all departments. The field xDepartmentCode has a dynamic picklist component which will automatically provide a combo box based on the xDepartments table. To use this, you may need to compile the dynamic picklist component xDepartmentCodePicklist.