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

VBA BAPI_MATERIAL_GET_ALL, get

Former Member
0 Likes
2,838

Hello experts,

I need to run the procedure of the function module BAPI_MATERIAL_GET_ALL from VBA (Visual Basic Application).

My code works successfully, I get data.

    Dim SapConnection As Object
    
    Dim objSAPReadTable As Object
    
    Set SapConnection = CreateObject("SAP.Functions.Unicode")


    SapConnection.Connection.ApplicationServer = "My server"
    SapConnection.Connection.System = "My System"
    SapConnection.Connection.Client = "My client"
    SapConnection.Connection.User = ""
    SapConnection.Connection.Password = ""
    SapConnection.Connection.Language = "EN"
    SapConnection.Connection.SystemNumber = "My SystemNumber"
    
    If SapConnection.Connection.Logon(0, False) <> True Then
        MsgBox "Error! Logon!"
    End If
    


    If SapConnection.Connection.IsConnected = 1 Then


        Set objSAPReadTable = SapConnection.Add("BAPI_MATERIAL_GET_ALL")
                
        Dim MT As Object
        Dim CompCode As Object
        Dim DataFromSap As Object


        Set MT = objSAPReadTable.Exports("MATERIAL")
        Set CompCode = objSAPReadTable.Exports("COMP_CODE")
        Set DataFromSap = objSAPReadTable.Imports("CLIENTDATA")


        MT.Value = "My material"
        CompCode.Value = "My company code"


        Result = objSAPReadTable.CALL
        
		' Get data 
		' DataFromSap("MATERIAL")
		' DataFromSap("MATL_TYPE")
		' DataFromSap("LAST_CHNGE")
        
        
    End If

I get the string data and date type successfully.

The problem is that I cannot get the fields of the structure with data type "packed bcd", for example "SHELF_LIFE", "MINREMLIFE" and other.

Thank you all for the help.

7 REPLIES 7
Read only

KatiNonhebel
Advisor
Advisor
0 Likes
2,286

Welcome and thanks for visiting SAP Community to get answers to your questions. Check out our tutorial to get started in SAP Community: https://developers.sap.com/tutorials/community-start.html By adding a picture to your profile you encourage readers to respond: https://www.youtube.com/watch?v=46bt1juWUUM

Read only

Sandra_Rossi
Active Contributor
0 Likes
2,286

"SHELF_LIFE" and "MINREMLIFE" of parameter "CLIENTDATA"

Why can't you get them? What error do you have? What is your code?

Read only

TuncayKaraca
Active Contributor
2,286

Hi Ivan,

SHELF_LIFE and MINREMLIFE are DEC (Decimal) types in SAP. If you are using VBA data type Object for the variables and getting the error. I would recommend using another data types -for example Decimal-.

Reference:

Thanks,
Tuncay

Read only

0 Likes
2,286

Tuncay! Thank you so much. But how to do it?

I've tried

Dim ShelfLife As Variant
ShelfLife = CDec(DataFromSap("SHELF_LIFE"))

result = <Type mismatch>

I think this is due to the fact that first the CLIENTDATA is converted to VBA Object

But I don't know how to get it bypassing the structure, I tried

Dim ShelfLife As Variant
ShelfLife = CDec(objSAPFuncReadTable.Imports("CLIENTDATA\\SHELF_LIFE"))

This does not work. =((((((

Read only

0 Likes
2,286

Ivan,

I had commented here yesterday but somehow it seems disappeared! 😞 Anyway,

Set DataFromSap = objSAPReadTable.Imports("CLIENTDATA")

seems correct. CLIENTDATA is export parameter in the BAPI as structure type. The structure has fields names including "SHELF_LIFE" and "MINREMLIFE" and structures are like tables with one row entry. So your variable DataFromSap should be object and table like. Then DataFromSap variable/table might need to be read for required field.

References:

VBA & RFC_READ_TABLE mit GUI750 SP8

Import tables directly into Access from SAP using RFCs

Read only

Former Member
0 Likes
2,286

Sandra Rossi!

I know that these parameters refer to CLIENTDATA! My code I provided in the question.

I get these parameters like this

DataFromSap("SHELF_LIFE")

DataFromSap("MINREMLIFE")

As a result, I get empty strings.

But, if I get parameter MATERIAL:

DataFromSap("MATERIAL")

As a result, I get normal material number!

Read only

TuncayKaraca
Active Contributor
0 Likes
2,286

Ivan,

CLIENTDATA is an Export parameter in BAPI_MATERIAL_GET_ALL - on your coding it's import parameter. CLIENTDATA is a structure which means 1 record of like a table row. It could return 1 row data with many fields including "SHELF_LIFE", "MINREMLIFE".

So this seems correct.

Set DataFromSap = objSAPReadTable.Imports("CLIENTDATA")

DataFromSap variable seems an Object type as table. Then it seems fields of DataFromSap should be accessible.

References:

SAP Function BAPI Scripting - Calling RFC FM from Excel

Import tables directly into Access from SAP using RFCs