Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Call BAPI in VBA

Former Member
0 Likes
1,795

Hi everyone,

I've been trying for many hours to get a BAPI call working in VBA. Can someone take a look at the code below and tell me what might be wrong? Thanks!

- The code doesn't crash or return an error. It just says that there are 0 rows in the table.

- I've managed to retrieve data from SAP with another BAPI. So in terms of references and connecting to SAP it should be OK

- When I go to SE37, press execute, manually enter the company code, customer and keydate, and execute agian; I do get 115 items as a result.


Sub BAPIgetAccsList()

Application.ScreenUpdating = False

   

Dim oSAP As Object

Dim objGetList As Object

Dim objGetListTbl As Object

Dim dRowsTable As Double

Set oSAP = CreateObject("SAP.Functions")

Set wsG = ThisWorkbook.Worksheets("GUI")

'------------------Connect---------------

oSAP.Connection.hostName = CNT_STR_APPLN_SRVR

oSAP.Connection.System = CNT_STR_SYSTEM

oSAP.Connection.SystemNumber = CNT_STR_SYS_NUM

oSAP.Connection.client = CNT_STR_CLIENT

oSAP.Connection.Language = CNT_STR_LOGON_LANG

oSAP.Connection.user = CNT_STR_USR

oSAP.Connection.Password = CNT_STR_PWD

If oSAP.Connection.Logon(1, True) = False Then

    MsgBox "Could not connect to SAP using provided details", vbCritical, "BAPI connection error"

    End

End If

'-------------------------------------------------------------------------------

Set objGetList = oSAP.Add("BAPI_AR_ACC_GETOPENITEMS")

objGetList.exports("COMPANYCODE") = "1000"

objGetList.exports("CUSTOMER") = "1000"

objGetList.exports("KEYDATE") = "10.06.2016"

objBAPIcall = objGetList.call

If objBAPIcall = True Then

    Set objGetListTbl = objGetList.tables("LINEITEMS")

           

    dRowsTable = objGetListTbl.RowCount

    If tblR <> 0 Then

        For r = 1 To tblR

            mdR = mdR + 1

            rsltRng.Offset(mdR, 1) = objGetListTbl(r, 1)

            rsltRng.Offset(mdR, 2) = objGetListTbl(r, 3)

            rsltRng.Offset(mdR, 3) = objGetListTbl(r, 4)

        Next r

    End If

End If

   

Set objGetList = Nothing

Set objGetListTbl = Nothing

Set objGetDtls = Nothing

Set objGetDtlsImp = Nothing

oSAP.Connection.Logoff

End Sub

1 ACCEPTED SOLUTION
Read only

Juwin
Active Contributor
0 Likes
937

Shouldn't the date be in YYYYMMDD format? Also, customer should be 10 characters, padded with zeros. Hence, pass customer as 0000001000. Can you try that way please?

Thanks,

Juwin

Shouldn't the date be in YYYYMMDD format? Also, customer should be 10 characters, padded with zeros. Hence, pass customer as 0000001000. Can you try that way please?

Thanks,

Juwin

2 REPLIES 2
Read only

Juwin
Active Contributor
0 Likes
938

Shouldn't the date be in YYYYMMDD format? Also, customer should be 10 characters, padded with zeros. Hence, pass customer as 0000001000. Can you try that way please?

Thanks,

Juwin

Read only

Sandra_Rossi
Active Contributor
0 Likes
937

To complement Juwin's answer, add a breakpoint at the beginning of the BAPI when you run it via SE37, to make sure which exact values are to be passed to the importing parameters ; you would see what explained Juwin.