Skip to Content
0
Former Member
Apr 15, 2008 at 12:09 PM

SET PARAMETERS WHEN CALLING RFC FROM VBA

726 Views

Hi All,

I am using the following VBA code to call table information from SAP.

Sub GetTable()

'Logon

Dim sapConn As Object 'Declare variant

Set sapConn = CreateObject("SAP.Functions") 'Create ActiveX object

If sapConn.Connection.Logon(0, False) <> True Then 'Try Logon

MsgBox "Cannot Log on to SAP"

End If

'Define function

Dim objRfcFunc As Object

Set objRfcFunc = sapConn.Add("RFC_READ_TABLE")

Dim objQueryTab, objRowCount As Object

Set objQueryTab = objRfcFunc.Exports("QUERY_TABLE")

objQueryTab.Value = "LQUA"

Set objRowCount = objRfcFunc.Exports("ROWCOUNT")

objRowCount.Value = "100"

Dim objOptTab, objFldTab, objDatTab As Object

Set objOptTab = objRfcFunc.Tables("OPTIONS")

Set objFldTab = objRfcFunc.Tables("FIELDS")

Set objDatTab = objRfcFunc.Tables("DATA")

'First we set the condition

'Refresh table

objOptTab.FreeTable

'Next we set fields to obtain

'Refresh table

objFldTab.FreeTable

'Then set values

objFldTab.Rows.Add

objFldTab(objFldTab.RowCount, "FIELDNAME") = "MANDT"

objFldTab.Rows.Add

objFldTab(objFldTab.RowCount, "FIELDNAME") = "LGNUM"

objFldTab.Rows.Add

objFldTab(objFldTab.RowCount, "FIELDNAME") = "MATNR"

objFldTab.Rows.Add

objFldTab(objFldTab.RowCount, "FIELDNAME") = "WERKS"

objFldTab.Rows.Add

objFldTab(objFldTab.RowCount, "FIELDNAME") = "LGTYP"

objFldTab.Rows.Add

objFldTab(objFldTab.RowCount, "FIELDNAME") = "LGPLA"

objFldTab.Rows.Add

objFldTab(objFldTab.RowCount, "FIELDNAME") = "GESME"

objFldTab.Rows.Add

objFldTab(objFldTab.RowCount, "FIELDNAME") = "VERME"

objFldTab.Rows.Add

objFldTab(objFldTab.RowCount, "FIELDNAME") = "MEINS"

If objRfcFunc.Call = False Then

MsgBox objRfcFunc.Exception

End If

Dim objDatRec As Object

Dim objFldRec As Object

For Each objDatRec In objDatTab.Rows

For Each objFldRec In objFldTab.Rows

Cells(objDatRec.Index, objFldRec.Index) = _

Mid(objDatRec("WA"), objFldRec("OFFSET") + 1, objFldRec("LENGTH"))

Next

Next

End Sub

This code will download all the information in the table but I want to be able to filter the information I am calling.

Does anyone know how I could set parameters in the code (e.g. only rows would be downloaded when fieldname MANDT = 900)?

Thanks