Step 6. Delete Data from the Table

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

1.  Drag drop a push button to the form in row 1, column 2.

a.  Give the button an Alignment of Bottom Center and Flow of Up

b.  Change all buttons to have a margin Bottom of 10

c  Set the button Name to DELETE and Caption to Delete.

d.  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 table. Remember to add the appropriate status error checking, this time using a CASE/ENDCASE loop for field IO$STS. When the record is deleted successfully, all fields on the form should be reset to their Repository defined defaults.

     Your finished code should appear as follows:

Evtroutine Handling(#DELETE.Click)

Delete From_File(xDepartments) With_Key(#xDepartmentCode) Val_Error(*NEXT)

Case Of_Field(#IO$STS)

When Value_Is(= OK)

Message Msgtxt('Department deleted successfully')

#formdata := *default

When (= NR)

Message Msgtxt('Department not found')

When (= ER)

Message Msgtxt('Error deleting department')

Endcase

Endroutine

 

4.  Compile and execute the form.

a.  Enter a Department Code of 50 (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 (BIF) MESSAGE_BOX_SHOW. This BIF displays a standard Windows message box, which may be configured to show a Title, a Caption and a number of message lines and a configurable number of buttons and image. The message box returns the name of the button that was clicked.

     See the Technical Reference guide for more details on MESSAGE_BOX_SHOW.

a.  To accept the user response, you need to define a new field as follows. Add this definition at the top of the source, below the Group_By statement.
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 at 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.

MESSAGE_BOX_SHOW

WITH_ARGS

Description

Value

1

The buttons to be displayed

YESNOCANCEL

2

Specify default button

NO

3

Icon to display

QUESTION

4

Message box title

Confirm Delete

5

Message text

Are you sure you want to delete?

 

     Your finished code should appear as follows:

Evtroutine Handling(#DELETE.Click)

Use Builtin(MESSAGE_BOX_SHOW) With_Args(YESNOCANCEL NO QUESTION 'Confirm Delete' 'Are you sure you want to delete?') To_Get(#answer)

If Cond(#answer = YES)

Delete From_File(xDepartments) With_Key(#xDepartmentCode) Val_Error(*NEXT)

Case Of_Field(#IO$STS)

When Value_Is(= OK)

Message Msgtxt('Department deleted successfully')

#formdata := *default

When (= NR)

Message Msgtxt('Department not found')

When (= ER)

Message Msgtxt('Error deleting department')

Endcase

Endif

Endroutine

 

     Note: 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,  MESSAGE_BOX_APPEND and MESSAGE_BOX_CLEAR Built in Functions to output a message box containing more text.

     Note: A Built in Function is a called program, which is implemented with the RDML USE command. LANSA has a large number of supplied Built in Functions. See this topic in the Technical Reference guide. You can also develop your own Built in Functions.

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.