Import Tables Directly Into Access From SAP Using RFCs
Import Tables Directly Into Access From SAP Using RFCs
One way to get the contents of a table is to export it to a text file and then import it
into Access. This doesn't work so well and tends to be a cumbersome manual
process comprised of several step each prone to error.
Pulling the data into Access using Remote Function Calls (RFCs) into the SAP system is very easy.
It requires only a user account with appropriate permissions and the libraries that are installed along with
the SAPGui client.
RFC calls from Access can be included in a Macro in the event that the data must be the most
recent available. A good example is obsolescence determination that requires current data extracted
from a legacy SAP system.
The RFC_READ_TABLE function accepts as parameters the name of the SAP table, the name of
the table to be created (or replaced) in Access and a comma-delimited string containing the
names of the fields to be extracted. In this example the fields MATNR, LVORM, MSTAE, MSTAV,
ERSDA and MTART are pulled from the SAP MARA table into the Access tblMARA table.
As Object
As Object
As Object
As Object
'**********************************************
'Create Server object and Setup the connection
'**********************************************
nTotalSeconds = 0
Set R3 = CreateObject("SAP.Functions")
R3.Connection.System = [TempVars]![SAP_System]
R3.Connection.SystemNumber = [TempVars]![SAP_SystemNumber]
R3.Connection.client = [TempVars]![SAP_Client]
R3.Connection.User = [TempVars]![SAP_User]
R3.Connection.Password = [TempVars]![SAP_Password]
R3.Connection.language = "EN"
R3.Connection.ApplicationServer = [TempVars]![SAP_ApplicationServer]
'*****************************************************
'Call RFC function RFC_READ_TABLE
'*****************************************************
Set MyFunc = R3.Add("RFC_READ_TABLE")
QUERY_TABLE.Value = strSAPTable
DELIMITER.Value = vbTab
NO_DATA = ""
j=0
strFieldnames = Split(strFields, ",")
For Each vField In strFieldnames
j=j+1
FIELDS.Rows.Add
FIELDS.Value(j, "FIELDNAME") = vField
Next
RetVal = SysCmd(acSysCmdSetStatus, "Extracting " & j & " fields from table " &
strSAPTable & " in " & strSAP_System & " . . . ")
Result = MyFunc.CALL
'*******************************************
'Quit the SAP Application
'*******************************************
R3.Connection.LOGOFF
nTotalColumns = FIELDS.ROWCOUNT
'Check for existence of TargetTable
nCounter = 0
Do While nCounter < dbs.TableDefs.Count
If dbs.TableDefs(nCounter).Name = strTableName Then
'Delete TargetTable--must start from scratch
dbs.TableDefs.Delete (strTableName)
End If
nCounter = nCounter + 1
Loop
iField = 1
nRowCount = FIELDS.ROWCOUNT
For iField = 1 To nRowCount
nFieldData(iField, 1) = FIELDS(iField, "OFFSET")
nFieldData(iField, 2) = FIELDS(iField, "LENGTH")
Next
For iRow = 1 To DATA.ROWCOUNT
nTotalRecords = nTotalRecords + 1
If Second(Now()) <> nCurSec Then ' And nCurRec <> rs.RecordCount Then
nCurSec = Second(Now())
nTotalSeconds = nTotalSeconds + 1
nSecondsLeft = Int(((nTotalSeconds / iRow) * DATA.ROWCOUNT) * ((DATA.ROWCOUNT
- iRow) / DATA.ROWCOUNT))
RetVal = SysCmd(acSysCmdRemoveMeter)
RetVal = SysCmd(acSysCmdInitMeter, "Importing " & strTableName & " from SAP, " &
nSecondsLeft & " seconds remaining. [" & nTotalRecords & " of " & DATA.ROWCOUNT & "]",
DATA.ROWCOUNT)
RetVal = SysCmd(acSysCmdUpdateMeter, iRow)
RetVal = DoEvents()
End If
rs.AddNew
For iField = 1 To nRowCount
rs.FIELDS(iField - 1).Value = Trim(Mid(strRow, nFieldData(iField, 1) + 1,
nFieldData(iField, 2)))
Next
rs.Update
Next
rs.Close
Close #2
Set dbs = Nothing
Set rs = Nothing
RetVal = SysCmd(acSysCmdRemoveMeter)
End Function