Step 3. Write the Code

Now that your user interface is complete, it is time to write the code. Click on the Source tab to display the source code for the form. You will notice that the editor has automatically created the DEFINE_COM statements for the controls and fields you have included on the form. These statements show the properties defined for the components.

3a. Create a Group for All the Employee Details Fields

To make it easier to deal with all the employee details fields, create a group for them:

 

GROUP_BY NAME(#ALLFLDS) FIELDS(#EMPNO #SURNAME #GIVENAME #ADDRESS1 #ADDRESS2 #ADDRESS3 #POSTCODE #PHONEHME #PHONEBUS #STARTDTE #TERMDATE #DEPTMENT #SECTION #SALARY #MNTHSAL)

 

3b. Write an Event Routine for the Get Button

1.  To write the code to fill the list view according to a partial surname, display the Details tab, select the GET button on the list and then bring the event tab to the front.

2.  Double-click on the Click event. Notice that editor inserts the EVTROUTINE and ENDROUTINE statements for you in the Source view.

Image1

     The icon in front of the Click event in the Details tab changes to indicate that a routine has been specified for the event:

3.  Add the following code in the Click event of the Get button:

 

EVTROUTINE HANDLING(#GET.Click)

   change #surname #partname

   clr_list #ltvW_1

   SELECT FIELDS(#ltvw_1) FROM_FILE(PSLMST2) WITH_KEY(#SURNAME) GENERIC(*YES)

      add_entry #ltvw_1

   endselect 

ENDROUTINE

     The event routine assigns the value entered in the #PARTNAME field to #SURNAME. It then clears the list view. The SELECT loop selects all employees whose surname matches the value of #SURNAME and adds the entries to the list view.

4.  Write an Event Routine to Fetch the Details for an Employee:

a.  Create a Click event routine for the ItemGotSelection event of the list view. This event is triggered when the user selects an employee in the list view.

b.  Now write the following code to fetch the details for the selected employee.

EVTROUTINE HANDLING(#LTVW_1.ItemGotSelection)

   FETCH FIELDS(#ALLFLDS) FROM_FILE(PSLMST) WITH_KEY(#EMPNO) ISSUE_MSG(*YES)

ENDROUTINE 

5.  Write an Event Routine to Enter a New Employee

Create an event routine for the Click event of the New button. Use the event to blank out all the fields so that the details of a new employee can be entered.

 

EVTROUTINE HANDLING(#NEW.Click)

   change #allflds *Null

ENDROUTINE 

6.  Write the Event Routine to Delete an Employee

Create an event routine for the Click event of the Delete button to delete the currently selected employee:

 

EVTROUTINE HANDLING(#DELETE.CLICK)

   DELETE FROM_FILE(PSLMST) WITH_KEY(#EMPNO) ISSUE_MSG(*YES)

ENDROUTINE 

7.  Write the Event Routine to Save Changes

Create an event routine for the Click event of the Save button to either update the details of an existing employee or to insert the details of a new employee:

 

EVTROUTINE HANDLING(#SAVE.CLICK)

check_for in_file(pslmst) with_key(#empno) val_error(*next)

if_status is(*EQUALKEY)

      UPDATE FIELDS(#ALLFLDS) IN_FILE(PSLMST) WITH_KEY(#EMPNO) ISSUE_MSG(*YES)

      IF_STATUS IS(*OKAY)

         MESSAGE MSGTXT('Employee details have been updated.')

      endif 

else

      INSERT FIELDS(#ALLFLDS) TO_FILE(PSLMST)  ISSUE_MSG(*YES)

      IF_STATUS IS(*OKAY)

         MESSAGE MSGTXT('Employee details have been added.')

      endif 

   endif 

ENDROUTINE 

The event routine first checks to see if the employee exists. If it does, it updates the employee details. If no record with the matching employee number is found, it inserts the employee details. A message is displayed after the update and the insert.