‎2021 Feb 16 9:04 AM
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.
‎2021 Feb 16 9:19 AM
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
‎2021 Feb 16 1:39 PM
"SHELF_LIFE" and "MINREMLIFE" of parameter "CLIENTDATA"
Why can't you get them? What error do you have? What is your code?
‎2021 Feb 17 2:13 AM
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
‎2021 Feb 18 9:08 AM
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. =((((((
‎2021 Feb 18 5:14 PM
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:
‎2021 Feb 17 10:18 AM
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!
‎2021 Feb 17 10:36 PM
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: