‎2016 Jul 04 10:35 AM
Hi to everyone,
I am facing an issue in extracting data from SAP to Excel using RFC_READ_TABLE in VBA.
The problem arises when I try to extract fields UEBTO from EKPO table.
I can get only *.0 values.
I would say it's an issue of data type but I am not able to overcome the problem...
Could you help me?
thanks in advance
Pietro
Hereafter the code:
Private Sub OdA()
m = ""
Sheets("principale di calcolo").Range("a3").Activate
Dim datamin_SAP As Variant
Dim datamax_SAP As Variant
datamin_SAP = Sheets("dati").Range("b1").Value
datamax_SAP = Sheets("dati").Range("b2").Value
Sheets("principale di calcolo").Select
Sheets("principale di calcolo").Range("a4").Activate
Dim attachpath1 As String
attachpath1 = Application.ActiveWorkbook.Path & "\Query1.CSV"
Set objfilesystemobject1 = CreateObject("Scripting.FileSystemObject")
Set filoutput1 = objfilesystemobject1.CreateTextFile(attachpath1, True)
Set RFC_READ_TABLE1 = funcControl1.Add("RFC_READ_TABLE")
Set strExport3 = RFC_READ_TABLE1.exports("QUERY_TABLE")
Set strExport4 = RFC_READ_TABLE1.exports("DELIMITER")
Set tbloptions1 = RFC_READ_TABLE1.tables("OPTIONS")
Set tblData1 = RFC_READ_TABLE1.tables("DATA")
Set tblFields1 = RFC_READ_TABLE1.tables("FIELDS")
strExport3.Value = "EKPO"
strExport4.Value = ";"
Do While IsEmpty(ActiveCell) = False
m = m & "WERKS LIKE '" & ActiveCell.Value & "' AND AEDAT GE '" & datamin_SAP & "'"
tbloptions1.Rows.Add
tbloptions1.Value(tbloptions1.RowCount, "TEXT") = m
m = " AND AEDAT LE '" & datamax_SAP & "'"
tbloptions1.Rows.Add
tbloptions1.Value(tbloptions1.RowCount, "TEXT") = m
m = " OR "
ActiveCell.Offset(1, 0).Activate
Loop
tblFields1.appendrow
tblFields1(1, "FIELDNAME") = "EBELN" 'numero ordine
tblFields1.appendrow
tblFields1(2, "FIELDNAME") = "EBELP" 'posizione
tblFields1.appendrow
tblFields1(3, "FIELDNAME") = "LOEKZ" 'indicatore cancellazione
tblFields1.appendrow
tblFields1(4, "FIELDNAME") = "AEDAT" 'data modifica posizione
tblFields1.appendrow
tblFields1(5, "FIELDNAME") = "TXZ01" 'testo breve
tblFields1.appendrow
tblFields1(6, "FIELDNAME") = "MATNR" 'materiale
tblFields1.appendrow
tblFields1(7, "FIELDNAME") = "BUKRS" 'societÃ
tblFields1.appendrow
tblFields1(8, "FIELDNAME") = "WERKS" 'divisione
tblFields1.appendrow
tblFields1(9, "FIELDNAME") = "LGORT" 'magazzino
tblFields1.appendrow
tblFields1(10, "FIELDNAME") = "BEDNR" 'numero fabbisogno
tblFields1.appendrow
tblFields1(11, "FIELDNAME") = "MATKL" 'gruppo merci
tblFields1.appendrow
tblFields1(12, "FIELDNAME") = "INFNR" 'info record
tblFields1.appendrow
tblFields1(13, "FIELDNAME") = "IDNLF" 'codice materiale presso il fonritore
tblFields1.appendrow
tblFields1(14, "FIELDNAME") = "KTMNG" 'qta prevista
tblFields1.appendrow
tblFields1(15, "FIELDNAME") = "MENGE" 'qta oda
tblFields1.appendrow
tblFields1(16, "FIELDNAME") = "MEINS" 'UdM Oda
tblFields1.appendrow
tblFields1(17, "FIELDNAME") = "BPRME" 'UdM prezzo OdA
tblFields1.appendrow
tblFields1(18, "FIELDNAME") = "NETPR" 'prezzo netto
tblFields1.appendrow
tblFields1(19, "FIELDNAME") = "PEINH" 'unità di prezzo
tblFields1.appendrow
tblFields1(20, "FIELDNAME") = "NETWR" 'valore netto OdA
tblFields1.appendrow
tblFields1(21, "FIELDNAME") = "BRTWR" 'valore lordo OdA
tblFields1.appendrow
tblFields1(22, "FIELDNAME") = "UEBTO" 'limite tolleranza per consegna in eccesso
tblFields1.appendrow
tblFields1(23, "FIELDNAME") = "UNTTO" 'limite tolleranza per consegna in difetto
tblFields1.appendrow
tblFields1(24, "FIELDNAME") = "ELIKZ" 'codice consegna finale
tblFields1.appendrow
tblFields1(25, "FIELDNAME") = "WEPOS" 'codice entrata merci
tblFields1.appendrow
tblFields1(26, "FIELDNAME") = "LABNR" 'numero conferma ordine
tblFields1.appendrow
tblFields1(27, "FIELDNAME") = "KONNR" 'numero contratto superiore
tblFields1.appendrow
tblFields1(28, "FIELDNAME") = "KTPNR" 'posizione contratto superiore
tblFields1.appendrow
tblFields1(29, "FIELDNAME") = "PLIFZ" 'tempo consegna plann in gg
tblFields1.appendrow
tblFields1(30, "FIELDNAME") = "RETPO" 'posizione resi
If RFC_READ_TABLE1.CALL = True Then
If tblData1.RowCount > 0 Then
For intRow = 1 To tblData1.RowCount
filoutput1.WriteLine tblData1(intRow, "WA")
Next
Else
MsgBox "No records returned"
End If
Else
MsgBox "ERROR CALLING SAP REMOTE FUNCTION CALL"
End If
Set objfilesystemobject1 = Nothing
Set filoutput1 = Nothing
Set RFC_READ_TABLE1 = Nothing
Set strExport3 = Nothing
Set strExport4 = Nothing
Set tbloptions1 = Nothing
Set tblData1 = Nothing
Set tblFields1 = Nothing
End Sub
‎2016 Jul 04 6:48 PM
That's a limit of this function module (don't forget it's not released for public use, as explained in several SAP notes): fields are returned in external representation, but the length is the internal length. So, if the table has a DEC field of 7 digits (4 bytes) with one after the decimal point, then the values will be returned on 4 characters. Any value greater than 9.9 (or lower than 9.9-) will be truncated and the first character will be replaced with * (11.1 would be rendered *.1)
No other solution than writing your own RFC-enabled function module.