Step 5 Update Data in a File

Visual LANSA

Step 5. Update Data in a File

FRM035 - Maintain a Simple Database Table

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

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

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

1.  Drag and drop a push button to the form.

a.  Set the button Name and Caption to UPDATE.

b.  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 DEPTAB file. 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(DEPTAB) WITH_KEY(#DEPTMENT) VAL_ERROR(*NEXT)
IF_STATUS IS(*OKAY)

MESSAGE MSGTXT(
'Department updated successfully')
#FORMDATA := *DEFAULT

ELSE

IF_STATUS IS(*NORECORD)

MESSAGE MSGTXT(
'Department not found')
ELSE

IF_STATUS IS(*ERROR)

MESSAGE MSGTXT(
'Error updating Department')
ENDIF

ENDIF

ENDIF

ENDROUTINE

 

4.  Compile and execute the form.

a.  Fetch your III test record that you inserted in Step 3. Insert Data to a File.

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

     Notice that the program validations you added on the DEPTDESC 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 this rule in the Repository.

5.  Close the form.

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

DEFINE FIELD(#RETCODE) TYPE(*CHAR) LENGTH(2)
UPDATE FIELDS(#FORMDATA) IN_FILE(DEPTAB) IO_STATUS(#RETCODE)
 

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

CASE OF_FIELD(#RETCODE)
WHEN VALUE_IS(= OK)
MESSAGE MSGTXT('Department updated successfully')
WHEN VALUE_IS(= NR)
MESSAGE MSGTXT('Department not found')
WHEN VALUE_IS(= ER)
MESSAGE MSGTXT('Error updating Department')
WHEN VALUE_IS(= EF)
MESSAGE MSGTXT('End of file.')
WHEN VALUE_IS(= BF)
MESSAGE MSGTXT('Beginning of file.')
WHEN VALUE_IS(= EQ)
MESSAGE MSGTXT('Equal key found.')
WHEN VALUE_IS(= NE)
MESSAGE MSGTXT('No equal key found.')
OTHERWISE
MESSAGE MSGTXT('Unidentified file operation return code.')
ENDCASE

 

     Note that if you do not explicitly specify the IO_STATUS option, the return code is automatically stored in the IO$STS field. You can use a CASE statement with this field instead:

CASE OF_FIELD(#IO$STS)
WHEN VALUE_IS(= OK)
...
ENDCASE