Q

Visual Basic code for a BAPI function

Our expert provides code for BAPI functions to use in Visual Basic.

Can I write a code in Visual Basic to use a BAPI function? For example, BABI_MATERIAL_GETALL ?
Thank you.

Sure you can. The following code calls RFC_RAD_TABLE_ENTRIES from VBA:

' Example calling BAPI RFC_GET_TABLE_ENTRIES
Option Explicit
Public Functions As SAPFunctionsOCX.SAPFunctions
Private LogonControl As SAPLogonCtrl.SAPLogonControl
Private R3Connection As SAPLogonCtrl.Connection
Dim Func As SAPFunctionsOCX.Function
Public iTABLE_NAME  As SAPFunctionsOCX.Parameter
Public eNUMBER_OF_ENTRIES  As SAPFunctionsOCX.Parameter
Public tENTRIES  As SAPTableFactoryCtrl.Table

Private Sub Main()
    Dim ix As Integer
    Dim retcd As Boolean
    Dim SilentLogon As Boolean
    Set LogonControl = CreateObject("SAP.LogonControl.1")
    Set Functions = CreateObject("SAP.Functions")
    Set TableFactory = CreateObject("SAP.TableFactory.1")
    Set R3Connection = LogonControl.NewConnection
    R3Connection.Client = "000"
    R3Connection.ApplicationServer = "192.168.69.111"
    R3Connection.Language = "EN"
    R3Connection.User = "DEVELOPER"
    R3Connection.Password = "19920607"
    R3Connection.System = "WAS"
    R3Connection.SystemID = "$WebAS"
    R3Connection.SystemNumber = "18"
    R3Connection.UseSAPLogonIni = False
    SilentLogon = True
    
    retcd = R3Connection.Logon(0, SilentLogon)
    If retcd <> True Then MsgBox "Logon failed": Exit Sub
    Functions.Connection = R3Connection
    
    Set Func = Functions.Add("RFC_GET_TABLE_ENTRIES")
    Set iTABLE_NAME = Func.Exports("TABLE_NAME")
    Set eNUMBER_OF_ENTRIES = Func.Imports("NUMBER_OF_ENTRIES")
    Set tENTRIES = Func.Tables("ENTRIES")
    iTABLE_NAME.Value = "TCURR"
    Func.Call
    Debug.Print eNUMBER_OF_ENTRIES
    For ix = 1 To tENTRIES.RowCount
        Debug.Print tENTRIES(ix, 1)
    Next
    R3Connection.logoff
End Sub

To learn more have a look in the mySAP section of logosworld.com or read the R/3 Guide to EDI, IDocs, ALE and Interfaces. Here is also a short document that explains the code in more detail.

This was first published in July 2006

Dig deeper on SAP ABAP

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchManufacturingERP

SearchOracle

SearchDataManagement

SearchAWS

SearchBusinessAnalytics

SearchCRM

SearchContentManagement

SearchFinancialApplications

Close