Step 5. Update Data in a Table

In this step you will add an Update button to your form in order to update an existing record in the table.

In this first example, you will update using the key to the table. In Step 7. Update and Delete Last

Record, you will modify the form to update the last record that was fetched.

1.  Drag a push button on to row 1, column 2.

a.  On the Layout ribbon, give it an Alignment of Bottom Center and Flow of Up.

b.  Give it a margin Bottom of 10.

c.  On the Details tab, set the button Name to UPDATE and Caption to Update.

d.  Create a Click event routine for the UPDATE button.

2.  Your form should appear like this:

3.  In the UPDATE.Click event routine, add an UPDATE command to update an existing record in the xDepartments table. Remember to add the appropriate status error checking. Once the UPDATE completes successfully, all fields on the form should be reset to their repository defaults.

     Your finished code should appear as follows:

Evtroutine Handling(#UPDATE.Click)

Update Fields(#formdata) In_File(xDepartments) With_Key(#xDepartmentCode) Val_Error(*next)

If_Status Is(*OKAY)

Message Msgtxt('Departmenmt successfully updated')

#formdata := *default


If_Status Is(*NORECORD)

Message Msgtxt('Department not found')


If_Status Is(*ERROR)

Message Msgtxt('Error updating department')






4.  Compile and execute the form.

a.  Fetch your Department code 50 test record that you inserted in Step 3. Insert Data to a Table.

b.  Change the Description to XYZ and press the Update button.

     Note: The program validations you added on the xDepartmentDescription field in the previous step are only applied when a new record is inserted with the INSERT command, but not when the record is changed with the UPDATE command. If you wanted the rules to be applied to both INSERT and UPDATE, the best solution would be to place the validation rules in the Repository. Alternatively, all program level validation rules could be placed into a validation subroutine in the form.

5.  Close the form.

6.  Using the IF_STATUS command is the recommended technique for checking the status of table operations. An alternative technique is to use the IO$STS field or another field to store the status code. For example:

IF COND(#retcode *NE OK) 

. . .

     You can now check the value of RETCODE to determine the status returned by the update. You could use a CASE statement as follows:

MESSAGE MSGTXT('Department updated successfully')
MESSAGE MSGTXT('Department not found')
MESSAGE MSGTXT('Error updating Department')
MESSAGE MSGTXT('End of table.')
MESSAGE MSGTXT('Beginning of table.')
MESSAGE MSGTXT('Equal key found.')
MESSAGE MSGTXT('No equal key found.')
MESSAGE MSGTXT('Unidentified table operation return code.')


     Note: If you do not explicitly specify the IO_STATUS() keyword, the return code is automatically stored in the IO$STS field. You could also simply use a CASE statement for this field: