banner ads

Database Table Update Delete

Database Table Update Delete

Module pool program is an ABAP/4 program which consists a set of modules with different screens. 
In each and every screen a user can give input by entering data or clicking any button or check boxes. 
Several screens of module pool program must have a screen flow logic by which a user can interact with the 
program. It can be called screen programming or dialog programming also. 
Module pool program cannot be executed directly by pressing F8. 
We need to create Transaction code for a module pool program. 
Now we can execute the program by entering that t-code similar like SE38, SE11 etc.

A module pool program can have any number of screens. All those screens must have separate attribute,
 screen elements and flow logic. Since ABAP is event driven language, screens must have the events also.
 There are four types of events in module pool programming.
1.       PBO (Process Before Output) – When it triggers then it initializes the screen attribute, screen elements, 
       PF status with all buttons of the screen and initial value of any fields if exists.
2.       PAI (Process After Input) – After displaying the screen when user enters any value or clicks any button 
       on that screen then this event is triggered. After triggering this event it moves to the flow logic of the next 
       screen. Hence preparing the next screen output. After doing this the system again triggers the PBO for
       next screen and then the next screen appears.
3.       POV (Process on Value request) – When user press the F4 button then it triggers this event and it
       displays the possible set of data records which need to be entered into that field.
4.       POH (Process on Help request) – When user press the F1 button then it triggers this event and it
       displays the help documentation (if exists) with a pop up screen.

We design the screen by using Screen Painter (SE51) and PF status or menu bar by using Menu Painter (SE41).
 In module pool program we can directly go to screen painter inside the screen by clicking the LAYOUT button.

We can display data records from database table into the screen by using module pool program. We also can modify data records by module pool program also. Here we have created a requirement where we have insert new data, update existing data and delete data by using module pool screen. We have a custom database table ‘zcustomers’. Now it contains fields of customer id, name and address.

Now we already have some records in this table as follows.

Now we have created this module pool program which has the first screen like this.

Here we already have entered the customer number ‘2’ and click the display button.

On the second screen we can see the customer details. The address is not updated there. We can check it from the database table also and we can confirm that the address has not been updated there. Now we shall enter an address on the address field and click on update button.

After updating the address of the customer we are generating a success message at message bar.

Now we can check the database again if the address has been updated or not.

We can also enter a new customer by using this program. We are going to the first screen and entering a new customer ID like ‘6’.  There is a validation whether the ID is already existing or not. To do this we have to click the display button after entering the customer number. If it is not in database then the program will prompt an Information message like this.

Now we click on the create button and the program will move to the next screen.

The customer number which we have entered into the first screen, is carry forwarded to the second screen. And now we need to fill up the details of this customer.

After filling up the data we click on update button to update this new customer into the database. We shall again get a success message for that. Now we shall see the database table and it has been newly updated as well.

In this way if we delete any customer at the first screen level then it will be removed as well.

Step – 1:
Now we are coming to the coding part of this program. At the time of creation of this program we have four include programs as follows. We just need to create all of them and save those.

INCLUDE zsr_top                                 .  " global Data
INCLUDE zsr_o01                                 .  " PBO-Modules
INCLUDE zsr_i01                                 .  " PAI-Modules
INCLUDE zsr_f01                                 .  " FORM-Routines

Firstly we have to declare the data types, tables, variables etc in the global data declaration.

PROGRAM  zsr_mod.

TABLES: zcustomers.

        id      TYPE zcustomers-id,
        name    TYPE zcustomers-name,
        address TYPE zcustomers-address,
       END OF ty_cust.

DATA: wa_cust TYPE zcustomers,

      ok_code1 TYPE sy-ucomm,
      ok_code2 TYPE sy-ucomm.

Step – 2:
Now we shall create the first screen 9001 and maintain the flow logic as follows.


PROCESS AFTER INPUT.  MODULE user_command_9001.

Step – 3:
Now we shall create the PBO – Module status_9001 as follows.

MODULE status_9001 OUTPUT.

ENDMODULE.                 " STATUS_9001  OUTPUT
Step – 4:
After that we have to create the pf status and title bar by double clicking on there.

Step – 5:
After that we shall create the PAI module user_command_9001 as follows:

MODULE user_command_9001 INPUT.

  CASE ok_code1.
    WHEN 'DISP'.
      PERFORM get_customer.
    WHEN 'CRT'.
      PERFORM create_customer.
    WHEN 'CANC'.
      CLEAR: zcustomers-id.


ENDMODULE.                 " USER_COMMAND_9001  INPUT
Step – 6:
Next we shall create all those subroutines as follows:

FORM get_customer .

  IF zcustomers-id IS NOT INITIAL.
      FROM zcustomers INTO wa_cust
      WHERE id = zcustomers-id.

    IF sy-subrc = 0.
      CALL SCREEN 9002.
      MESSAGE 'Customer doesn''t exist' TYPE 'I'.
    MESSAGE 'Enter a Customer Number' TYPE 'I'.

ENDFORM.                    " get_customer
FORM create_customer .

  IF zcustomers-id IS NOT INITIAL.

    "Validation is needed if customer is already present
      FROM zcustomers INTO wa_cust
      WHERE id = zcustomers-id.

    IF sy-subrc = 0"It means customer is already in database
      MESSAGE 'Customer already exist' TYPE 'I'.

    ELSE"It means customer is not present in the database
      "Hence it needs to be created

      CLEAR: wa_cust, zcustomers-name, zcustomers-address.
      "Only zcustomers-id needs to be passed to screen 9002

      CALL SCREEN 9002.
    MESSAGE 'Enter a Customer Number' TYPE 'S'.

ENDFORM.                    " create_customer

Step – 7:
Here we can see that the program is calling screen 9002 so we have to configure that now. On screen 9002 we have the following.

PROCESS AFTER INPUT.  MODULE user_command_9002.

Step – 8:
Similarly we have to create the PBO for 9002 as follows.

MODULE status_9002 OUTPUT.

  IF ok_code1 = 'DISP'.
    PERFORM customer_output.

ENDMODULE.                 " status_9002  OUTPUT

Step – 9:
Now after creating the pf status and title bar for screen 9002 we need to create the subroutine of customer_output.

FORM customer_output .

  IF wa_cust IS NOT INITIAL.

    "Screen fields       Work area fields
    zcustomers-id      = wa_cust-id.
    zcustomers-name    = wa_cust-name.
    zcustomers-address = wa_cust-address.

ENDFORM.                    " customer_output
Step – 10:
Now we need to create the PAI for screen 9002 as follows.

MODULE user_command_9002 INPUT.

  CASE ok_code2.
    WHEN 'UPDT'.
      PERFORM update_customer.
    WHEN 'DEL'.
      PERFORM delete_customer.
      LEAVE TO SCREEN 9001.

ENDMODULE.                 " user_command_9002  INPUT

Step – 11:
Next we need to create the subroutines to update and delete customer.

FORM update_customer .

  IF zcustomers-id IS NOT INITIAL.

    "Passing the values to the internal work area
    wa_cust-id      = zcustomers-id.
    wa_cust-name    = zcustomers-name.
    wa_cust-address = zcustomers-address.

    IF ok_code1 = 'CRT'.

      "Inserting new Customer into database while creating
      INSERT into zcustomers values wa_cust.


      "Updating the database by using the work area
      UPDATE zcustomers FROM wa_cust.

    IF sy-subrc = 0"If the update is successful
      MESSAGE 'Customer details updated successfully' TYPE 'S'.

    ELSE"If the update is not successful
      MESSAGE 'Customer is not updated' TYPE 'E'.

ENDFORM.                    " update_customer
FORM delete_customer .

  IF zcustomers IS NOT INITIAL.

    "Work area fields     Screen fields
    wa_cust-id          = zcustomers-id.
    wa_cust-name        = zcustomers-name.
    wa_cust-address     = zcustomers-address.

    "Delete data records from database table
    DELETE zcustomers FROM wa_cust.

    IF sy-subrc = 0"If deletion is successful
      CLEAR: wa_cust, zcustomers.
      MESSAGE 'Data has been removed successfully' TYPE 'S'.
      LEAVE TO SCREEN 9001.

    ELSE"If deletion is not successful
      MESSAGE 'Data has not been removed' TYPE 'E'.

ENDFORM.                    " delete_customer
Step – 12:
Since module pool program cannot be executed directly we need to create a transaction code.

Now after doing this we have to activate the whole program and then check the t-code from the system.

No comments