Visual Basic code for a BAPI function

Visual Basic code for a BAPI function

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

    Requires Free Membership to View

    When you register, you will start receiving targeted emails from my award-winning team of editorial writers. Our goal is to keep you informed on the hottest topics and biggest challenges faced by SAP professionals today.

    Hannah Smalltree, Editorial Director

    By submitting your registration information to SearchSAP.com you agree to receive email communications from TechTarget and TechTarget partners. We encourage you to read our Privacy Policy which contains important disclosures about how we collect and use your registration and other information. If you reside outside of the United States, by submitting this registration information you consent to having your personal data transferred to and processed in the United States. Your use of SearchSAP.com is governed by our Terms of Use. You may contact us at webmaster@TechTarget.com.

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