Ask the Expert

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

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

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

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: