Visual Basic Calling SAP – using BAPI Calls

5
3294

BAPI is a useful resource provided by SAP though which we can communicate with SAP also from external envirnment / sources.  The following code shows the example of how to call BAPI externally, although SAP authentication for the user is done.

The example is a program developed in Visual Basic which I developed in my learning phase. It gives an idea about the hierarchy that needs to be followed when calling BAPI.

The example consists of three BAPI calls namely, for PO, GRN and Sales Order creation. The code is also available for download, but may require a bit tweaking.

Create a new project in VB and add 3 normal forms and a MDI form(Main form from where child forms will be called). Three forms namely for PO Order, GRN, and Sales Order creation.

Also add SAP BAPI Control Control to the project to have relevant library get loded into project.

MDI Screen:

MDI Screen Code:

Private Sub mnuexit_Click()
Unload Me
End Sub
Private Sub mnugrn_Click()
Load grn
End Sub
Private Sub mnupurchase_Click()
Load porder
End Sub
Private Sub mnusales_Click()
Load salesorder
End Sub

Form PORDER:

PORDER Screen Code:

Dim BAPICTRL As Object
Dim BOORDER As Object
Dim OITEMSCH As Object
Dim OITEMIN As Object
Dim OHEADER As Object
Dim ORETURN As Object
Dim OCONN As Object
Private Sub Command1_Click()
Dim X As String
OHEADER.Value("DOC_TYPE") = Text1(0)
OHEADER.Value("PURCH_ORG") = Text1(1)
OHEADER.Value("PUR_GROUP") = Text1(2)
OHEADER.Value("VENDOR") = Text1(3)
OHEADER.Value("DOC_DATE") = Text1(4)
OHEADER.Value("CO_CODE") = Text1(14)
'OHEADER.Value("PURCH_NO") = Text1(5)
'OPARTNER.ROWS.Add
'OPARTNER.Value(1, "PARTN_ROLE") = Text1(6)
'OPARTNER.Value(1, "PARTN_NUMB") = Text1(7)
OITEMIN.ROWS.Add
OITEMIN.Value(1, "PO_ITEM") = Text1(8)
OITEMIN.Value(1, "PUR_MAT") = Text1(6)
OITEMIN.Value(1, "MATERIAL") = Text1(9)
OITEMIN.Value(1, "PLANT") = Text1(10)
OITEMIN.Value(1, "ACCTASSCAT") = " "
'OITEMIN.Value(1, "PLANT") = Text1(10)
OITEMSCH.ROWS.Add
OITEMSCH.Value(1, "PO_ITEM") = Text1(5)
OITEMSCH.Value(1, "DELIV_DATE") = Text1(12)
OITEMSCH.Value(1, "QUANTITY") = Text1(13)
'OITEMIN.Value(1, "MATERIAL") = Text1(9)
BOORDER.CreateFromData PoHeader:=OHEADER, PoItems:=OITEMIN, _
PoItemSchedules:=OITEMSCH, Return:=ORETURN
', _
'HEADERADDDATARELEVAN:=" ", SKIPITEMSWITHERROR:=" ", ITEMADDDATARELEVANT:=" "
'X = ORETURN.Value("message")
'If X = "" Then
'    MsgBox "Transaction Complete."
'Else
'    MsgBox X
'End If
 MsgBox (ORETURN.Columns("MESSAGE").Value(1))
End Sub
Private Sub Command2_Click()
OCONN.LOGOFF
End Sub
Private Sub Command3_Click()
Call Form_Load
End Sub
Private Sub Form_Load()
Text1(4) = Format(Now, "mm/dd/yyyy")
Text1(12) = Format(Now, "mm/dd/yyyy")
Set BAPICTRL = CreateObject("SAP.BAPI.1")
Set OCONN = BAPICTRL.Connection
OCONN.LOGON
Set BOORDER = BAPICTRL.GetSAPObject("PurchaseOrder")
Set OITEMSCH = BAPICTRL.DimAs(BOORDER, "CreateFromData", "PoItemSchedules")
Set OITEMIN = BAPICTRL.DimAs(BOORDER, "CreateFromData", "PoItems")
Set OHEADER = BAPICTRL.DimAs(BOORDER, "CreateFromData", "PoHeader")
Set ORETURN = BAPICTRL.DimAs(BOORDER, "CreateFromData", "RETURN")
End Sub
Private Sub Form_Unload(Cancel As Integer)
OCONN.LOGOFF
End Sub
Private Sub Text1_Change(Index As Integer)
End Sub

Form GRN:

GRN Screen Code:

Dim BAPICTRL As Object
Dim BOORDER As Object
Dim OCODE As Object
Dim OITEMIN As Object
Dim OHEADER As Object
Dim ORETURN As Object
Dim OCONN As Object
Private Sub Command1_Click()
Dim X As String
OHEADER.Value("PSTNG_DATE") = Text1(0)
OHEADER.Value("DOC_DATE") = Text1(1)
OHEADER.Value("REF_DOC_NO") = Text1(2)
OHEADER.Value("BILL_OF_LADING") = Text1(3)
OHEADER.Value("HEADER_TXT") = Text1(4)
'OHEADER.Value("CO_CODE") = Text1(14)
'OHEADER.Value("PURCH_NO") = Text1(5)
OCODE.Value("GM_CODE") = Text1(8)
OITEMIN.ROWS.Add
OITEMIN.Value(1, "MATERIAL") = Text1(5)
OITEMIN.Value(1, "PLANT") = Text1(12)
OITEMIN.Value(1, "STGE_LOC") = Text1(13)
OITEMIN.Value(1, "MOVE_TYPE") = Text1(6)
OITEMIN.Value(1, "VENDOR") = Text1(7)
OITEMIN.Value(1, "PO_NUMBER") = Text1(9)
OITEMIN.Value(1, "ENTRY_QNT") = "10"
OITEMIN.Value(1, "MVT_IND") = "B"
OITEMIN.Value(1, "PO_ITEM") = "1"
OITEMIN.Value(1, "ITEM_TEXT") = "-/-/-/nav"
BOORDER.CreateFromData GoodsmvtHeader:=OHEADER, GoodsmvtItem:=OITEMIN, _
GoodsmvtCode:=OCODE, Return:=ORETURN
', _
'HEADERADDDATARELEVAN:=" ", SKIPITEMSWITHERROR:=" ", ITEMADDDATARELEVANT:=" "
'X = ORETURN.Value("message")
'If X = "" Then
'    MsgBox "Transaction Complete."
'Else
'    MsgBox X
'End If
MsgBox (ORETURN.Columns("MESSAGE").Value(1))
End Sub
Private Sub Command2_Click()
OCONN.LOGOFF
End Sub
Private Sub Command3_Click()
Call Form_Load
End Sub
Private Sub Form_Load()
Text1(0) = Format(Now, "mm/dd/yyyy")
Text1(1) = Format(Now, "mm/dd/yyyy")
'Text1(2).SetFocus
Set BAPICTRL = CreateObject("SAP.BAPI.1")
Set OCONN = BAPICTRL.Connection
OCONN.LOGON
Set BOORDER = BAPICTRL.GetSAPObject("GoodsMovement")
Set OCODE = BAPICTRL.DimAs(BOORDER, "CreateFromData", "GoodsmvtCode")
Set OITEMIN = BAPICTRL.DimAs(BOORDER, "CreateFromData", "GoodsmvtItem")
Set OHEADER = BAPICTRL.DimAs(BOORDER, "CreateFromData", "GoodsmvtHeader")
Set ORETURN = BAPICTRL.DimAs(BOORDER, "CreateFromData", "RETURN")
End Sub
Private Sub Form_Unload(Cancel As Integer)
OCONN.LOGOFF
End Sub

Form SALESORDER:

SALESORDER Screen Code:

Dim BAPICTRL As Object
Dim BOORDER As Object
Dim OPARTNER As Object
Dim OITEMIN As Object
Dim OHEADER As Object
Dim ORETURN As Object
Dim OCONN As Object
Private Sub Command1_Click()
Dim X As String
OHEADER.Value("DOC_TYPE") = Text1(0)
OHEADER.Value("SALES_ORG") = Text1(1)
OHEADER.Value("DISTR_CHAN") = Text1(2)
OHEADER.Value("DIVISION") = Text1(3)
OHEADER.Value("PRICE_DATE") = Text1(4)
OHEADER.Value("PURCH_NO") = Text1(5)
OPARTNER.ROWS.Add
OPARTNER.Value(1, "PARTN_ROLE") = Text1(6)
OPARTNER.Value(1, "PARTN_NUMB") = Text1(7)
OITEMIN.ROWS.Add
OITEMIN.Value(1, "REQ_QTY") = Text1(8)
OITEMIN.Value(1, "MATERIAL") = Text1(9)
OITEMIN.Value(1, "COND_VALUE") = Text1(10)
BOORDER.CreateFromData orderheaderin:=OHEADER, orderitemsin:=OITEMIN, _
orderpartners:=OPARTNER, Return:=ORETURN
X = ORETURN.Value("message")
If X = "" Then
    MsgBox "Transaction Complete."
Else
    MsgBox X
End If
'MsgBox (ORETURN.Columns("MESSAGE").Value(1))
End Sub
Private Sub Command2_Click()
OCONN.LOGOFF
End Sub
Private Sub Command3_Click()
Call Form_Load
End Sub
Private Sub Form_Load()
Text1(4) = Format(Now, "mm/dd/yyyy")
Set BAPICTRL = CreateObject("SAP.BAPI.1")
Set OCONN = BAPICTRL.Connection
OCONN.LOGON

Set BOORDER = BAPICTRL.GetSAPObject("SalesOrder")
Set OPARTNER = BAPICTRL.DimAs(BOORDER, "CreateFromData", "orderpartners")
Set OITEMIN = BAPICTRL.DimAs(BOORDER, "CreateFromData", "orderitemsin")
Set OHEADER = BAPICTRL.DimAs(BOORDER, "CreateFromData", "orderheaderin")
End Sub
Private Sub Form_Unload(Cancel As Integer)
OCONN.LOGOFF
End Sub

Project Source Code: Download Link.

5 COMMENTS

  1. can you please provide mapping of VB with bapi .. and how to call bapi i mean your complete work of this scenarion it will be really helpful thanking you in advance

  2. Thanks for this helpful topic. Can you please provide me the abap code for PurchaseOrder, GoodsMovement, SalesOrder.
    With Bast Regards.

LEAVE A REPLY

Please enter your comment!
Please enter your name here