You are on page 1of 15

Getting Familiar With VB and RFC

10

The topic of this book is the collaboration of SAP R/3 with a PC programming language
suitable for web development, like Visual Basic. The classical situation you will find
yourself in when you start such a project, is that one part of your developers is quite
familiar with SAP R/3 and its ABAP programming language, while the other half knows
all about PC and Visual Basic but purely nothing about R/3.
We assume that you know that SAP is programmed in a third generation Basic style
programming language called ABAP and that you are able to guess how a BASIC program
works when we explain its flow to you.
The link between Visual Basic (or C++, Java, JavaScript) and R/3 ABAP is done via a bridging
technique called Remote Function Call generally referred to as RFC. RFC provides an
interface communication protocol that either enables ABAP to call programs running on a
remote server or let a remote computer call an ABAP library function from the distance.
All this talking will be about client server technologies. Remote program calls are the heart of
any client server technologies where the calling program is called the client and the program
or component that receives the call is named the server.
This book is designed around practical examples. Therefore, we will soon start designing
working pieces of code (called sniplets or servlets) to sharpen your fantasy how you can
connect to R/3 from Visual Basic. The example will connect as a Visual Basic Script to R/3 via
an ActiveX Logon component and make a call to R/3 s client table T000. performance

20

Chapter Contents

Overview
How To Start
Test The Installation
How to Call An RFC Function Module

Getting Familiar With VB and RFC

Overview
This book is about creating programs for the internet that are able to access and store data in
R/3 systems. This short summary should give you an idea what the necessary steps
and the technical philosophy behind the scenes are.
Browser clients that access ASP programs
30

Internet browser applications are typical client server applications. There is a client software
the web browser that requests a service from a server application the internet server.
1. An Internet web browser requests service from a web server application
2. The Web server stores Active Server Pages (ASP) which are HTML pages with Visual Basic
code and executes the program statement; as a result the ASP sends a valid HTML document
back to the calling browser
3. The ASP application may access other server application and components like a database or
specially designed DCOM and ActiveX components

ASP web server application is a variant of a COM component


40

An internet application is a special variant of a general Windows server application that always
returns an HTML document as a result.
4. The browser communicates with the web server application.
The web server application is responsible for interpreting the browser requests. If the web
server application has determined the service requested by the browser client

50

5. It calls another satellite application (application server or component) to determine


the requested result data and eventually sends back an appropriate HTML or XML
document as a response to the browser
This satellite application is usually a database application, i.e. a program that reads and
manipulates data in a database, e.g. an SQL Server or an ACCESS database. Here is where
R/3 appears on the scene. When working with SAP, we will always treat R/3 like any other
database server and
6. The satellite application may access R/3 like it would access any ODBC or SQL database
source

Different system, different terminology


60

The terminology and technology used above is the one of a Microsoft web server
environment. If you use other web servers, the principles will be analogous. Correspondences
may be:

2 of 15

Axel Angeli 12 March 2001

How to Call An RFC Function Module

Principles components to call R/3 from ASP

Axel Angeli 12 March 2001

3 of 15

Getting Familiar With VB and RFC


Windows

UNIX

Active Server Page


DCOM
Internet Information Server
Visual Basic

CGI, PERL, PSH


CORBA
Fasttrack, Apachee
PERL, C++

4 of 15

Axel Angeli 12 March 2001

How to Call An RFC Function Module

How To Start
There are essentially four big fields that need to be worked and harvested in order to create a
successful web application with SAP R/3 access. They are: 1. writing: Visual Basic
scripts, 2. writing ABAP functions, 3. executing VB Scripts with ASP and 4. linking VB
Scripts to R/3.

What To Learn
Step 1a: Learn to program in Visual Basic and in SAP R/3 ABAP
70

You will write all your server side programming in Visual Basic. This will be your web
application . The application will receive requests from the browser session through the ASP
pages and decides how to react on the request, e.g. read or update databases, write log files,
send out emails, short: anything that can be programmed.
Step 1b: Learn to program in SAP R/3 ABAP

Programming in ABAP will be the other end of the tunnel. R/3 will be the power server which
shall handle the requests, process most of the business logic and pipe phenomenal amount of
data through the web.
Step 2: Access R/3 from Visual Basic via the RFC library

You that you know how to program on both ends you should learn how you can call the stubs
you developed in R/3 from Visual Basic. This will be mainly done through SAP's RFC OCX
library which is shipped routinely with the latest SAPGUI CDROM.
Step 3: Make use of ASP pages to call the Visual Basic application from a browser
80

Until now you would have written your applications without any relationship to the web. In fact,
accessing your application through a browser is simply another shim stacked on top of your
application which does nothing else than to accept browser requests, translate them into
application calls and make sure that the application result data is directed corrected to the
requesting browser session.

Alternative Development Environments


Alternative programming languages

Although we will not care fore alternatives in this document, you still have alternatives to use.
You can program in C++ or SmallTalk, Delphi, Java or any other programming language to
your liking, as long as the language is compatible to Microsoft's DCOM standard. This is
generally true for all modern programming packages to run under Windows and for most UNIX
environments as well. However, you would have to check this out for your platform.
Alternative CGI access
90

Instead of accessing your application via ASP you may use CGI scripting or Server side
JavaScripting. You may even use Java, though I heavily discourage you from doing so for
reason of performance, compatibility and security. Many industry companies strictly prohibit
running Java applets on their company work station through the internet, this making your
application incompatible. Programming Java on the server end is an option, but this belongs to
the decision on the programming language discussed in the previous paragraph.
Recommendation and why we chose those tools

100

However, if you ask for a recommendation, be assured that we chose the elements (VB, ASP,
DCOM and NT) very consciously. We are convinced that this will result in faster and more
reliable development simply because of the enormous amount of tools and information
available for this platform and language combination which is definitely not found in other
environments. To make it simple: You can address your VB and ASP questions in any high
school class and will get proper, immediate and qualified response. Thus you do not have to
go through expensive head hunters to find your experts, you will find them on the street.

Axel Angeli 12 March 2001

5 of 15

Getting Familiar With VB and RFC

Technologies required for successful R/3 web applications

Elementary Tools and Know-How for


Web Development

You should be acquainted with the shown


technologies to develop successful R/3 web
collaboration applications..

Visual Basic

C++, JAVA

Alternatives:
PERL, JAVA, PHP
Visual Basic

+ HTML

=
Active Server Page

ASP
Alternatives:
MS Access, Oracle

SQL
VB Recordsets

DLL/OCX-Programming
+
MS Transaction Server

=
COM+

R/3-RFC
Via RFCSDK OCX Controls
(DCOM Connector)

ABAP IV
R/3-Repository
R/3 Customizing
21 May 2001

6 of 15

R/3 and Internet - (c) 2001 Logos! Informatik GmbH All rights reserved

17

Axel Angeli 12 March 2001

How to Call An RFC Function Module

Test The Installation


Most frustration with RFC comes from a bad system set-up. RFC should be enabled if you
successfully installed the full SAPGUI of any valid release since version 3.1. Although
we describe the installations later in detail we add this chapter to stress the
importance of the correct installation and to tell you the easiest way to test your
application using either EXCEL or a Visual Basic Script.
Install RFC files with SAPGUI
110

The easiest way to enable RFC functionality on R/3 is to install the complete SAPGUI, mainly
the RFC Software Development Kit. If you are not sure whether the RFCSDK is installed,
search for the following files your Windows Program Files directory: wdtlog.ocx,
wdtfuncs.ocx, wdtaocx.ocx and wdtx.ocx and for LIBRFC32.DLL in your windows SYSTEM
directory. (Note: if you have a local language version of Windows, the folder Program Files
may be translated into your language like Programme in German or Programmes in
French)
Minimal VBA code to test RFC from EXCEL

120

Below you will find the minimal code you need to see if your system is set up to execute RFC
calls to R/3. The code in the procedure Sub Main() will create a new RFC logon object and
then popup the SAPGUI Logon Panel to ask for the missing logon data and to confirm logon.
You may enter the code in EXCEL (or MS WinWord or any other application that support VBA)
by creating a new Macro.

Axel Angeli 12 March 2001

7 of 15

Getting Familiar With VB and RFC

VBA Test Program to See If RFC Controls Are Correctly Installed


Declarations

Dim LogonControl
Dim conn
Dim funcControl As New SAPFunctionsOCX.SAPFunctions
Dim TableFactoryCtrl
Dim RFC_READ_TABLE
Sub Main()
'-----------------------------------------------------------' Main is the principle Entry to the program pool
'-----------------------------------------------------------Set LogonControl = CreateObject("SAP.LogonControl.1")
Set funcControl = CreateObject("SAP.Functions")
Set TableFactoryCtrl = CreateObject("SAP.TableFactory.1")
Set conn = LogonControl.NewConnection

Set here your system data. If some or all parameters are missing you will be prompted for in a popup
conn.ApplicationServer = "192.68.2.22" ' IP of the R/3 application server
conn.System = "MICKY"
' System ID of the instance, usually 00
conn.SystemNumber = "00"
' System (database) ID of the instance
conn.Client = "100"
' opt. Client number to logon to
conn.Language = "EN"
' opt. Your login language
conn.User = "MICKYMOUSE"
' opt. Your user id
conn.Password = "pazzwurd"
' opt. Your password
retcd = conn.Logon(0, False) '0=new instance; false=do not suppress dialog
If retcd <> True Then
MsgBox " Cannot log on! "
MsgBox retcd
' Stop
Else
' MsgBox " Logon OK."
End If
conn.Logof
MsgBox " Logged of from R/3! "
End Sub

Alternatively you can add the code in a text file with extension .VBS

If you do not want to test from EXCEL, you can create the code in a simple text file with
extension .vbs. Then the program will be treated as a Visual Basic Script. Mind the explicit
CALL MAIN() statement which is the only difference to the VBA code.
Additional code line for Visual Basic Script

If you want to test the code from VB Script, youll have to add an explicit call to the main
procedure.
Call Main()

8 of 15

Axel Angeli 12 March 2001

How to Call An RFC Function Module

How to Call An RFC Function Module


130

This chapter summarizes 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 analyzed 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 to know from R/3

140

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 quarrel 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.
Example that reads data from an R/3 table

150

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 on 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 /3 access

If you fully understood, how this example works, then you should be able to write any program
you want to connect to R/3.

Axel Angeli 12 March 2001

9 of 15

Getting Familiar With VB and RFC

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

Declare a pointer to the R/3 RFC function repository


DIM funcControl

Declare a record set pointer to pass an SQL WHERE-clause


DIM TableFactoryCtrl

Declare a pointer to the actual R/3 RFC function module


DIM RFC_READ_TABLE

Declare a pointer to every parameter to the function module


DIM
DIM
DIM
DIM

eQUERY_TAB
TOPTIONS
TDATA
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
160
Sub R3Logon()

Create a new connection with method NewConnection


Set conn = LogonControl.NewConnection

Specifying the Login properties

The login properties are the same which may be 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. Of course, if you will run the login from a web server a
dialogue is desirable.

10 of 15

Axel Angeli 12 March 2001

How to Call An RFC Function Module


10
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

Calling Logon method

retcd = conn.Logon(0, False)

Checking if logon has been successful


If retcd <> True Then
MsgBox " Cannot log on! "
MsgBox retcd
Stop
else
MsgBox " Logon OK."
End If
End Sub

Calling an RFC Function Module


170

Calling an RFC function module can be done via the funcControl Active/X control and an
open RFC connection, whoosh has been created in the logon step. The code will set pointers
to local variables that hold the import and export parameters to the function and then the
function call is executed. R/3 table parameters will be represented as Visual Basic recordsets.
The recordsets will be automatically typed by the RFC call.
Sub R3RFC_READ_TABLE(pQueryTab)
'-----------------------------------------------------------' Call the R/3 RFC function RFC_READ_TABLE
'------------------------------------------------------------

Create a new collection object for the function module


Set RFC_READ_TABLE = funcControl.Add("RFC_READ_TABLE")

Set pointers to local variables for the import and export parameters

180

Import, export and tables parameters of an R/3 function module are referenced with an 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
Set
Set
Set

eQUERY_TAB = RFC_READ_TABLE.Exports("QUERY_TABLE")
TOPTIONS = RFC_READ_TABLE.Tables("OPTIONS") '
TDATA
= RFC_READ_TABLE.Tables("DATA") '
TFIELDS = RFC_READ_TABLE.Tables("FIELDS") '

Reading and writing parameter values

Once the parameter object have been created, you can assign a value to the objects value
property or read the value property respectively.

Axel Angeli 12 March 2001

11 of 15

Getting Familiar With VB and RFC


eQUERY_TAB.Value = pQueryTab ' pQueryTab is the R/3 name of the table
TOPTIONS.AppendRow ' new item line
TOPTIONS(1,"TEXT") = "MANDT EQ '000'"

Calling the RFC function

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

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.

190

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

The rest
End Sub

Main Program
Main() procedure

The above code have been the most thrilling part but then we need to put them together in a
Main procedure.
Sub Main()

Create an instance of the SAP.LogonControl class (version 1)

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")
200

Create an instance of the SAP.Functions collection


Set funcControl = CreateObject("SAP.Functions")

Create an instance of the SAP.TableFactory

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.

12 of 15

Axel Angeli 12 March 2001

How to Call An RFC Function Module


Set TableFactoryCtrl = CreateObject("SAP.TableFactory.1")

Call the logon part


call R3Logon

Assign the connection to our collection


funcControl.Connection = conn

Make the RFC call


call R3RFC_READ_TABLE("T000")

Log off the connection


conn.Logof
MsgBox " Logged of from R/3! "
End Sub

Start the Program


Call Main()

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.

210

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>

Axel Angeli 12 March 2001

13 of 15

Getting Familiar With VB and RFC

The coding to call an RFC function RFC_READ_TABLE as a whole


'************************************************************
' 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
'-----------------------------------------------------------eQUERY_TAB
PrinciplesDIM
components
to call R/3 from ASP
DIM TOPTIONS
DIM TDATA
DIM TFIELDS

Calling R/3 From ASP

'************************************************************
' 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.Logof
MsgBox " Logged of from R/3! "
End Sub

ASP Application
(HTML + VB Script
Code.)
COM

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"
' opt. Your login language
conn.User = ""
' opt. Your user id
conn.Password = ""
' opt. Your password

Proxy Interface Class


This will be our self-defined the subroutine pool
for the calls to R/3
COM

DCOM Components
SAP Logon Control SAP.LogonControl.1
SAP Function Library SAP.Functions
SAP BAPI Control SAP.Bapi.1

retcd = conn.Logon(0, False)


If retcd <> True Then
MsgBox " Cannot log on! "
MsgBox retcd
Stop
else
MsgBox " Logon OK."
End If
End Sub

RFC Function Modules

14 of 15

R/3 Database

BAPI
ABAP

ABAP

'-----------------------------------------------------------' Create objects for each parameter

RFC

RFC

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")

Axel Angeli 12 March 2001

How to Call An RFC Function Module


'-----------------------------------------------------------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

Axel Angeli 12 March 2001

15 of 15

You might also like