1 Calling RFC Functions and Bapis From Windows™: Sub Routines
1 Calling RFC Functions and Bapis From Windows™: Sub Routines
1 Calling RFC Functions and Bapis From Windows™: Sub Routines
This chapter summarises in brief how to call the DCOM ActiveX components that are provided
by SAP to access R/3 function modules from a Windows platform. We will demonstrate how to
call the standard RFC function module RFC_READ_TABLE.
Reading data from an arbitrary R/3 table with RFC_READ_TABLE
Here you can see the basic example how to call R/3 RFC functions from Visual Basic. This example
will read the data from table T000 as also shown in the previous chapter, where we discussed the
RFC function RFC_READ_TABLE. If you analysed this example and understood how it works, I see no
reason why you should not be able to create any other application, that links to R/3.
Apart from the logon data and the name of the table we need nothing else from R/3
SAP provides a set of interface OCX control and DLLs. They were written and compiled in Visual
Basic, so the OCX are technically compatible with your Visual Basic. If there are still problems with
compatibility, you better take it up with Microsoft, not with SAP. SAP is seen by Visual Basic as an
object like any other object, e.g. the ADO-object, DAO-object or the FileSystem-Object. You need to
know the methods and the meaning of the properties, but you do not need to know anything about R/3
to be able to use R/3 as an intelligent database server.
R/3 is a stored procedure database server
If you are completely ignorant about R/3 then you should regard R/3 as a transparent database server
and the function modules as stored procedures of this database system.
DIM RFC_READ_TABLE
DIM eQUERY_TAB
DIM TOPTIONS
DIM TDATA
DIM TFIELDS
'************************************************************
' Main Program
'************************************************************
'-----------------------------------------------------------call Main
'------------------------------------------------------------
Sub Routines
The program is neatly split in handy, easily digestible sub routines.
Login to R/3 via RFC with the logon Active/X control component
This is a little VB Script example that demonstrates how you can call an RFC function module in R/3.
The function used is function RFC_READ_TABLE which takes the name of a table as a parameter and
returns its contents. As an option you can pass a Visual Basic recordset to contain a simple SQL
WHERE-clause, which is added to the SQL statement issued by RFC_READ_TABLE.
The example is the basic template for every R/3 access
Sub R3Logon()
If you fully understand how this example works then you should be able to write any program you want
to connect to R/3.
Specifying the Login properties
Visual Basic code to read data from table T000 via RFC
Declarations
Declare an R/3 Logon OCX component
DIM LogonControl
Declare an R/3 Connection object to become a member of the Logon OCX component
DIM conn
DIM funcControl
DIM TableFactoryCtrl
The login properties are the same as are found in the R/3 Logon Panel. Only the application server
name and the system number are mandatory. If the other parameters are missing you will be
prompted for them. Of course, if you run the login from a web server a dialogue is desirable.
conn.ApplicationServer = "r3dev" ' IP or DNS-Name of the R/3 application server
conn.System = "00"
' System ID of the instance, usually 00
conn.Client = "100"
' opt. Client number to logon to
conn.Language = "EN"
' opt. Your login language
conn.User = ""
' opt. Your user id
conn.Password = ""
' opt. Your password
The rest
End Sub
Main Program
Sub R3RFC_READ_TABLE(pQueryTab)
'-----------------------------------------------------------' Call the R/3 RFC function RFC_READ_TABLE
'------------------------------------------------------------
Main() procedure
Set pointers to local variables for the import and export parameters
Import, export and tables parameters of an R/3 function module are referenced with a Visual Basic
object pointer. The function collection which we created above with the funcControl.Add method
provides appropriate methods, Exports, Imports and Tables which create a correct parameter object
and return a reference to this object.
Set eQUERY_TAB = RFC_READ_TABLE.Exports("QUERY_TABLE")
Set TOPTIONS = RFC_READ_TABLE.Tables("OPTIONS") '
Set TDATA
= RFC_READ_TABLE.Tables("DATA") '
Set TFIELDS = RFC_READ_TABLE.Tables("FIELDS") '
Once the parameter object have been created, you can assign a value to the objects value property or
read the value property respectively.
eQUERY_TAB.Value = pQueryTab ' pQueryTab is the R/3 name of the table
TOPTIONS.AppendRow ' new item line
TOPTIONS(1,"TEXT") = "MANDT EQ '000'"
The above code have been the most thrilling part but then we need to put them together in a Main
procedure.
Sub Main()
The number at the end of the object class name lets you specify the version number, so that you could
have several versions of the same class registered in Windows registry simultaneously. If you do not
specify the version there should be a default version registered in the registry, if the developer made a
proper effort to do so.
Set LogonControl = CreateObject("SAP.LogonControl.1")
The SAP table factory is an object that returns a special variant of a Visual Basic recordset along with
appropriate methods to manipulate the table factory recordset.
Set TableFactoryCtrl = CreateObject("SAP.TableFactory.1")
call R3Logon
Once the parameter values have been set, you can call the function with the CALL-property. The
property returns TRUE or FALSE according to whether the call has been successful or not.
If RFC_READ_TABLE.Call = True Then
funcControl.Connection = conn
call R3RFC_READ_TABLE("T000")
Output the result
When the RFC call has been successful you can output the result data or process them appropriately.
For our demo we will display the first row of the returned recordset (= RFC table parameter) by means
of the VBS message box.
If TDATA.RowCount > 0 Then
MsgBox "Call to RFC_READ_TABLE successful! Data found"
MsgBox TDATA(1, "WA")
Else
MsgBox "Call to RFC_READ_TABLE successful! No data found"
End If
Else
MsgBox "Call to RFC_READ_TABLE failed!"
End If
conn.Logoff
MsgBox " Logged off from R/3! "
End Sub
Depending on the runtime environment you use, there are different ways to call the whole procedure.
If you stored the coding as Visual Basic Script in a separate file with extension .VBS you have to add
an explicit call to your main routine in the file.
Call Main()
ASP
Call Main() from an ASP page
You can call that from an ASP page that would simply look similar to the following. (assume that the
coding above is stored to a file called RfcReadTable.VBS.
<HTML>
<HEAD>
<#include RfcReadTable.vbs >
</HEAD>
<BODY><%>Call Main()<%></BODY>
Example 1:
'************************************************************
' Declarations
'************************************************************
DIM LogonControl 'As SAPLogonCtrl.SAPLogonControl
DIM conn 'As SAPLogonCtrl.Connection
DIM funcControl 'As SAPFunctionsOCX.SAPFunctions
DIM TableFactoryCtrl 'As SAPTableFactoryCtrl.SAPTableFactory
'-----------------------------------------------------------' Pointer to functions
'-----------------------------------------------------------DIM RFC_READ_TABLE
'-----------------------------------------------------------' Pointers to function parameters
'-----------------------------------------------------------DIM eQUERY_TAB
DIM TOPTIONS
DIM TDATA
DIM TFIELDS
'************************************************************
' Main Program
'************************************************************
call Main
'************************************************************
' Subroutines
'************************************************************
Sub Main()
Set LogonControl = CreateObject("SAP.LogonControl.1")
Set funcControl = CreateObject("SAP.Functions")
Set TableFactoryCtrl = CreateObject("SAP.TableFactory.1")
call R3Logon
funcControl.Connection = conn
call R3RFC_READ_TABLE("T000")
conn.Logoff
MsgBox " Logged off from R/3! "
End Sub
Sub R3Logon()
Set conn = LogonControl.NewConnection
'-----------------------------------------------------------' ** Set here your system data. They are also found in the R/3 Logon Panel
' Only the app server and the system number is mandatory. If the other params
' are missing you will be prompted for
'-----------------------------------------------------------conn.ApplicationServer = "r3dev" ' IP or DNS-Name of the R/3 application server
conn.System = "00"
' System ID of the instance, usually 00
conn.Client = "100"
' opt. Client number to logon to
conn.Language = "EN"
conn.User = ""
conn.Password = ""
Sub R3RFC_READ_TABLE(pQueryTab)
'-----------------------------------------------------------' Add the R/3 RFC function RFC_READ_TABLE to the collection
'-----------------------------------------------------------Set RFC_READ_TABLE = funcControl.Add("RFC_READ_TABLE")
'-----------------------------------------------------------' Create objects for each parameter
'-----------------------------------------------------------Set eQUERY_TAB = RFC_READ_TABLE.Exports("QUERY_TABLE")
Set TOPTIONS = RFC_READ_TABLE.Tables("OPTIONS") '
Set TDATA
= RFC_READ_TABLE.Tables("DATA") '
Set TFIELDS = RFC_READ_TABLE.Tables("FIELDS") '
eQUERY_TAB.Value = pQueryTab ' pQueryTab is the R/3 name of the table
TOPTIONS.AppendRow ' new item line
TOPTIONS(1,"TEXT") = "MANDT EQ '000'"
If RFC_READ_TABLE.Call = True Then
If TDATA.RowCount > 0 Then
MsgBox "Call to RFC_READ_TABLE successful! Data found"
MsgBox TDATA(1, "WA")
Else
MsgBox "Call to RFC_READ_TABLE successful! No data found"
End If
Else
MsgBox "Call to RFC_READ_TABLE failed!"
End If
End Sub
ASP Application
(HTML + VB Script
Code.)
COM
DCOM Components
SAP Logon Control SAP.LogonControl.1
SAP Function Library SAP.Functions
SAP BAPI Control SAP.Bapi.1
RFC
RFC
BAPI
ABAP
ABAP
R/3 Database