Step 6 Delete Data from the File

Visual LANSA

Step 6. Delete Data from the File

FRM035 - Maintain a Simple Database Table

In this step you will add a Delete button to your form in order to delete an existing record in the file.

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

a.  Set the button Name and Caption to DELETE.

b.  Create a Click event routine for the Delete button.

2.  Your form should appear like this:

3.  In the Delete.Click event routine, add a DELETE command to delete a record from the file. Remember to add the appropriate status error checking. Once the DELETE completes, all fields on the form should be reset to their repository defaults.

     Your finished code should appear as follows:

EVTROUTINE HANDLING(#DELETE.Click)
DELETE FROM_FILE(DEPTAB) WITH_KEY(#DEPTMENT)
IF_STATUS IS(*OKAY)

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

ELSE

IF_STATUS IS(*NORECORD)

MESSAGE MSGTXT(
'Department not found')
ELSE

IF_STATUS IS(*ERROR)

MESSAGE MSGTXT(
'Error deleting Department')
ENDIF

ENDIF

ENDIF

ENDROUTINE

 

4.  Compile and execute the form.

a.  Enter a Department Code of III (your test record) and press the Delete button.

5.  In many applications, users are prompted to confirm that the record is to be deleted. You can add this message using the LANSA Built-In Function MESSAGE_BOX_SHOW.

a.  To accept the user response, you need to define a new field as follows:

DEFINE FIELD(#ANSWER) TYPE(*CHAR) LENGTH(6)

 

b.  In the DELETE.Click event routine, invoke the MESSAGE_BOX_SHOW Built-In Function by adding the following USE command to the very start of the routine:

USE BUILTIN(MESSAGE_BOX_SHOW) WITH_ARGS(YESNOCANCEL NO QUESTION 'Confirm Delete'

'Are you sure you want to delete?') TO_GET(#ANSWER)

 

c.  Immediately after the USE command, check the value of #ANSWER. If the answer is YES, proceed to delete the record.

     Your finished code should appear as follows:

EVTROUTINE HANDLING(#DELETE.Click)
USE BUILTIN(MESSAGE_BOX_SHOW) WITH_ARGS(YESNOCANCEL NO QUESTION 'Confirm Delete'
'Are your sure you want to delete?') TO_GET(#ANSWER)
IF COND(#ANSWER = YES)

DELETE FROM_FILE(DEPTAB) WITH_KEY(#DEPTMENT) IF_STATUS IS(*OKAY)
MESSAGE MSGTXT('Department deleted successfully')
#FORMDATA := *DEFAULT
ELSE
IF_STATUS IS(*NORECORD)
MESSAGE MSGTXT('Department not found')
ELSE
IF_STATUS IS(*ERROR)
MESSAGE MSGTXT('Error deleting Department')
ENDIF
ENDIF
ENDIF
ENDIF
ENDROUTINE

 

     You should review the documentation for the MESSAGE_BOX_SHOW Built in Function in the Technical Reference Guide. It can also be used with MESSAGE_BOX_ADD and MESSAGE_BOX_APPEND Built in Functions to output a message box containing more text.

6.  Compile and execute the form.

a.  Insert some new test data.

b.  Fetch your test record and press the Delete button.

     A confirmation message is displayed.

7.  Close the form.