You are on page 1of 10

How to use bdc on table control.

1) Suppose we need to enter some records in a ztable through table maintenance generator SM30. For this first you have
to create a program from SHDB after recording .record how a record is entered in the table zvbak from the transaction SM30
and create a program. Now that source code is required in this program.
Let the table is zvbak and it has four fields, so first create an internal table to get the records from the flat file. And start
coding like this.
DATA: BDCDATA LIKE BDCDATA OCCURS 0 WITH HEADER LINE.
DATA: BEGIN OF ITAB OCCURS 0,
VBELN TYPE VBELN,
VKORG TYPE VKORG,
VTWEG TYPE VTWEG,
SPART(2),
END OF ITAB.
daTA: MESSTAB LIKE BDCMSGCOLL OCCURS 0 WITH HEADER LINE.
2) This count variable is to count the number of lines displayed at one time in the table control.
DATA: V_COUNT1 TYPE I.
DATA: V_COUNT(3) TYPE C.
DATA: V_CHAR(16).
3) Now take the file from where records are to be uploaded.
PARAMETERS: P_FILE TYPE LOCALFILE.
start-of-selection.
DATA: W_FILE TYPE STRING.
W_FILE = P_FILE.
4) Call function gui_upload to upload the records from flatfile to an internal table itab.
CALL FUNCTION 'GUI_UPLOAD'

EXPORTING
filename

= W_FILE

FILETYPE

= 'DAT'

* HAS_FIELD_SEPARATOR

=''

* HEADER_LENGTH

=0

* READ_BY_LINE

= 'X'

* DAT_MODE

=''

* CODEPAGE

=''

* IGNORE_CERR

= ABAP_TRUE

* REPLACEMENT

= '#'

* CHECK_BOM

=''

* VIRUS_SCAN_PROFILE

* NO_AUTH_CHECK

=''

* IMPORTING
* FILELENGTH
* HEADER

=
=

tables
data_tab

= ITAB

* EXCEPTIONS
* FILE_OPEN_ERROR

=1

* FILE_READ_ERROR

=2

* NO_BATCH

=3

* GUI_REFUSE_FILETRANSFER
* INVALID_TYPE

=5

=4

* NO_AUTHORITY

=6

* UNKNOWN_ERROR

=7

* BAD_DATA_FORMAT

=8

* HEADER_NOT_ALLOWED

=9

* SEPARATOR_NOT_ALLOWED
* HEADER_TOO_LONG

= 10
= 11

* UNKNOWN_DP_ERROR

= 12

* ACCESS_DENIED

= 13

* DP_OUT_OF_MEMORY
* DISK_FULL
* DP_TIMEOUT
* OTHERS

= 14
= 15
= 16

= 17

.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*

WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.
5) Now call function BDC_open_group to open the session.
CALL FUNCTION 'BDC_OPEN_GROUP'
EXPORTING
CLIENT
* DEST
GROUP
* HOLDDATE

= SY-MANDT
= FILLER8
= 'ZBDC'
= FILLER8

KEEP

= 'X'

USER

= SY-UNAME

* RECORD
PROG

= FILLER
= SY-CPROG

* DCPFM

= '%'

** DATFM

= '%,'

* IMPORTING
* QID

EXCEPTIONS
CLIENT_INVALID

=1

DESTINATION_INVALID

=2

GROUP_INVALID

=3

GROUP_IS_LOCKED

=4

HOLDDATE_INVALID

=5

INTERNAL_ERROR

=6

QUEUE_ERROR
RUNNING

=7
=8

SYSTEM_LOCK_ERROR
USER_INVALID
OTHERS

=9

= 10
= 11

.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*

WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.
6) Here we will use the source code obtained from the recording.
perform bdc_dynpro
perform bdc_field

using 'SAPMSVMA' '0100'.


using 'BDC_CURSOR'

'VIEWNAME'.
perform bdc_field

using 'BDC_OKCODE'
'=SHOW'.

perform bdc_field

using 'VIEWNAME'
'ZVBAK'.

perform bdc_field

using 'VIMDYNFLDS-LTD_DTA_NO'
'X'.

perform bdc_dynpro
perform bdc_field

using 'SAPLZVBAK' '0001'.


using 'BDC_CURSOR'

'ZVBAK-VBELN(01)'.
perform bdc_field

using 'BDC_OKCODE'
'=AEND'.

perform bdc_dynpro
perform bdc_field

using 'SAPLZVBAK' '0001'.


using 'BDC_CURSOR'

'ZVBAK-VBELN(01)'.
perform bdc_field

using 'BDC_OKCODE'
'=NEWL'.

7) Now when bdcdata table is being filled with the records then keep on incrementing the count variable by 1 as it is
representing the line number of the table control . concatenate the field name with the count to represent the row and
column of the table control.
LOOP AT ITAB.

V_COUNT1 = V_COUNT1 + 1.
V_COUNT = V_COUNT1.
CONDENSE V_COUNT.
perform bdc_dynpro
*perform bdc_field
*

using 'SAPLZVBAK' '0001'.


using 'BDC_CURSOR'

'ZVBAK-SPART(01)'.

CLEAR V_CHAR.
CONCATENATE 'ZVBAK-VBELN(' V_COUNT ')' INTO V_CHAR.
perform bdc_field

using V_CHAR
ITAB-VBELN.

CLEAR V_CHAR.
CONCATENATE 'ZVBAK-VKORG(' V_COUNT ')' INTO V_CHAR.
perform bdc_field

using V_CHAR
ITAB-VKORG.

CLEAR V_CHAR.
CONCATENATE 'ZVBAK-VTWEG(' V_COUNT ')' INTO V_CHAR.
perform bdc_field

using V_CHAR
ITAB-VTWEG.

8) When the last line of the table control is reached then again reset the value of the count as one and set the command of
page up i.e. '=P+' inside the loop only. This is done only for that screen where table control is being used.
IF V_COUNT1 = 24.
V_COUNT1 = 1.
perform bdc_field

using 'BDC_OKCODE'
'=P+'.

ENDIF.

* CLEAR V_CHAR.
*CONCATENATE 'ZVBAK-SPART(' V_COUNT ')' INTO V_CHAR.
*perform bdc_field
*

using V_CHAR
ITAB-SPART.

ENDLOOP.
9) After the loop statement continue the steps as per recording.
perform bdc_dynpro
perform bdc_field

using 'SAPLZVBAK' '0001'.


using 'BDC_CURSOR'

'ZVBAK-VBELN(02)'.
perform bdc_field

using 'BDC_OKCODE'
'=SAVE'.

perform bdc_dynpro
perform bdc_field

using 'SAPLZVBAK' '0001'.


using 'BDC_CURSOR'

'ZVBAK-VBELN(03)'.
perform bdc_field

using 'BDC_OKCODE'
'=BACK'.

perform bdc_dynpro
perform bdc_field

using 'SAPLZVBAK' '0001'.


using 'BDC_CURSOR'

'ZVBAK-VBELN(03)'.
perform bdc_field

using 'BDC_OKCODE'
'=BACK'.

perform bdc_dynpro
perform bdc_field

using 'SAPMSVMA' '0100'.


using 'BDC_OKCODE'

'/EBACK'.

perform bdc_field

using 'BDC_CURSOR'
'VIEWNAME'.

10) Now call the function bdc_insert and pass the bdcdata table to this function.
CALL FUNCTION 'BDC_INSERT'
EXPORTING
TCODE

= 'SM30'

* POST_LOCAL

= NOVBLOCAL

* PRINTING

= NOPRINT

* SIMUBATCH

=''

* CTUPARAMS

=''

TABLES
dynprotab

= BDCDATA

* EXCEPTIONS
* INTERNAL_ERROR
* NOT_OPEN

=1
=2

* QUEUE_ERROR

=3

* TCODE_INVALID

=4

* PRINTING_INVALID

=5

* POSTING_INVALID

=6

* OTHERS

=7

.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*

WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.
11) Now close the group.
CALL FUNCTION 'BDC_CLOSE_GROUP'
* EXCEPTIONS
* NOT_OPEN

=1

* QUEUE_ERROR
* OTHERS

=2

=3

.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*

WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.
12) Define the subroutines used in the recording here.
FORM BDC_DYNPRO USING PROGRAM DYNPRO.
CLEAR BDCDATA.
BDCDATA-PROGRAM = PROGRAM.
BDCDATA-DYNPRO = DYNPRO.
BDCDATA-DYNBEGIN = 'X'.
APPEND BDCDATA.
ENDFORM.
*----------------------------------------------------------------------*
*

Insert field

*----------------------------------------------------------------------*
FORM BDC_FIELD USING FNAM FVAL.

CLEAR BDCDATA.
BDCDATA-FNAM = FNAM.
BDCDATA-FVAL = FVAL.
APPEND BDCDATA.
ENDFORM.

You might also like