Create SAP - VB.Net Connection using SAP ActiveX

<< Click to Display Table of Contents >>

Navigation:  All About SAP Interface > SAP r/3 and VB.Net >

Create SAP - VB.Net Connection using SAP ActiveX

Objective

Create SAP Logon

Create SAP Logon with input dialog

Calling and Using RFC/BAPIs from SAP R/3

Support

Download Source Code

Contributor

Albertus Reinandang ( reinandang@yahoo.com )

 

Requirement

All step is already tested using this requirement

1.SAP GUI 6.40 Patch Level 0-21 or SAP GUI 7.10 Patch Level 0 (download)

2.Microsoft Visual Studio 2005

3.Windows 2000 or XP

In SAP system, there are roles that we must pass through SAP Logon to access SAP feature. SAP Logon need a special codes to be a standard SAP Logon system, which is:

Client (R/3 System, client number from logon)

> A client in the r/3 system that you want to connect to

ApplicationServer (Name of Application Server )

> The name of the host that you want to connect to

SystemNumber ( System Number server SAP )

> Specify the system number (e.g. 00) of the SAP System that you want to connect to

System ( System ID)

> Specify the system ID (e.g. OSS) of the SAP System that you want to connect to

User ( User Logon Name )

Password ( User Password )

Language ( System Language )

If you not completely all the condition of that code above, you can’t using/accessing a feature in SAP GUI ActiveX, especially “SAP Functions” .

 

Installed Component

1.Dotnet Framwork 2.0

2.SAP ActiveX ( SAPLogonControl and SAPFunctions )

 

____________________________________________________________________________

 

Create SAP Logon

 

SAPLogonControl Concept and Format:

 … Declare a New Connection …

 … Fill Connection Variable …

 … login …

 

Example:

 

    Dim l_SAPLogonControl As New SAPLogonCtrl.SAPLogonControl

    Dim l_conn As SAPLogonCtrl.Connection

    l_conn = l_SAPLogonControl.NewConnection

 

    If l_conn.Logon(0, FalseThen

      MsgBox("Connect to SAP.")

    End If

 

>> … LogOn(0,false) … if you set to False then Dialog Login Menu will be appear.

 

Note : Connect to SAP” = it’s mean ‘You have Succeed Login to SAP !!’

The following Example show how to used SAP Login Control:

 

No

Description

 

1

Difficulty

Easy ( For beginner )

2

Microsoft Visual Basic .Net

Forms.Button

 

Procedure:

1.Run Microsoft Visual Basic .Net

2.Create new Project > Visual Studio > Windows > Windows Application

3.Add SAP ActiveX

4.Add one “ Button ” Component to a Form

vb60005

 

No

Component

Properties

Value

1

Button

Name

BLogon

 

 

Caption

&R/3 Logon

 

 

Others

Default

 

5.Add OnClik event on SAPLogonControl1 component with code bellow :

  Private Sub BLogon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BLogon.Click

    Dim l_SAPLogonControl As New SAPLogonCtrl.SAPLogonControl

    Dim l_conn As SAPLogonCtrl.Connection

    l_conn = l_SAPLogonControl.NewConnection

 

    If l_conn.Logon(0, FalseThen

      MsgBox("Connect to SAP.")

    End If

 

  End Sub

 

6.Press “ Run/Start “ button for testing your code.

 

____________________________________________________________________________

 

Create SAP Logon with input dialog

 

No

Description

 

1

Difficulty

Easy ( For beginner)

2

Microsoft Visual Basic .Net

Forms.Button

 

 

Forms.TextBox

Forms.Label

 

Procedure:

1.Run Microsoft Visual Basic .Net

2.Create new Project > Visual Studio > Windows > Windows Application

3. Add SAP ActiveX

4.Add and place it the others component like a picture bellow :

vbnet0004

 

 

No

Commponent

Properties

Value

1

Button

Name

BLogon

 

 

Caption

&R/3 Logon

 

 

Others

Default

2

TextBox

Name

eApplicationServer

 

 

Text

Null (Empty)

3

TextBox

Name

eSystemNumber

 

 

Text

Null (Empty)

4

TextBox

Name

eClient

 

 

Text

Null (Empty)

5

TextBox

Name

eSystem

 

 

Text

Null (Empty)

6

TextBox

Name

eUsername

 

 

Text

Null (Empty)

7

TextBox

Name

ePassword

 

 

Text

Null (Empty)

8

TextBox

Name

eLanguage

 

 

Text

Null (Empty)

 

5.Add OnClick event at SAPLogonControl1 component with code bellow :

  Private Sub BLogon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BLogon.Click

    Dim l_SAPLogonControl As Object 'New SAPLogonCtrl.SAPLogonControl

    Dim l_conn As Object 'SAPLogonCtrl.Connection

 

    l_SAPLogonControl = CreateObject("SAP.LogonControl.1")

    l_conn = l_SAPLogonControl.NewConnection

 

    l_conn.Client = eClient.Text

    l_conn.ApplicationServer = eApplicationServer.Text

    l_conn.SystemNumber = eSystemNumber.Text

    l_conn.System = eSystem.Text

    l_conn.User = eUsername.Text

    l_conn.Password = ePassword.Text

    l_conn.Language = eLanguage.Text

 

    If l_conn.Logon(0, TrueThen

      MsgBox("Connect to SAP.")

    End If

 

  End Sub

 

6.Press “ Run/Start “ button for testing your code.

 

____________________________________________________________________________

 

Calling and Using RFC/BAPIs from SAP R/3

 

BAPIs (Business Application Program Interfaces) is SAP business objects held in the Business Object Repository (BOR) encapsulate their data and processes. External access to the data and processes is only possible using specific methods.

RFC (Remote Function Call) is interfaces provided for communication between SAP Systems and with external partners.

 

The BAPIs and RFC in the R/3 System are currently implemented as function modules which are created and managed in the Function Builder (SE37). Not all SAP user can used a Function Builder. Only user who have an access key from SAP can used all the feature in Function Builder. You can get Access key from http://service.sap.com.

 

In RFC or BAPI desain there are 5 variable type, which is Import, Export, Changing, Tables and Exceptions. The variable which used generally is Import, Export and Tables.

delphi0022

 

Import Variable in SAPFunctions is declare as a functionexports “, while the Export variable is declare as a functionimports “.

 

SAPFunctions Concept and Format: 

 … SAPFunction Connection …

 … Calling SAPFunctions …

         … Fill (upload) Import/Tables variable in SAP …

         … Call (download) Export/Tables variable in SAP …

 … Close Connection …

 

 

The following Example show how to call RFC :

 

No

Description

 

1

Difficulty

Middle

2

Microsoft VB .Net Component

Forms.Button

 

 

Forms.TextBox

 

 

Forms.Label

 

 

Forms.Panel

 

 

Forms.DataGridView

3

RFC names

/CRYSTAL/GET_USER_LIST

 

Procedure :

1.Run Microsoft Visual Basic .Net

2.Create new Project > Visual Studio > Windows > Windows Application

3.Add SAP ActiveX

4.Add and place it the others component like a picture bellow :

vbnet0005

 

No

Commponent

Properties

Value

1

Button

Name

BLogon

 

 

Caption

&R/3 Logon

 

 

Others

Default

2

TextBox

Name

eApplicationServer

 

 

Text

Null (Empty)

3

TextBox

Name

eSystemNumber

 

 

Text

Null (Empty)

4

TextBox

Name

eClient

 

 

Text

Null (Empty)

5

TextBox

Name

eSystem

 

 

Text

Null (Empty)

6

TextBox

Name

eUsername

 

 

Text

Null (Empty)

7

TextBox

Name

ePssword

 

 

Text

Null (Empty)

8

TextBox

Name

eLanguage

 

 

Text

Null (Empty)

10

Frame

Name

Frame1

 

 

Caption

SAP Logon

10

TextBox

Name

DisplayTableData

 

 

Text

Null (Empty)

 

 

MultiLine

True

 

 

ScrollBars

3 - Both

 

6.Add OnLoad event at Form

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    DataGridView1.Columns.Add("No""No")

    DataGridView1.Columns.Add("MANDT""MANDT")

    DataGridView1.Columns.Add("BNAME""BNAME")

    DataGridView1.Columns.Add("SUBSYSTEM""SUBSYSTEM")

    DataGridView1.Columns.Add("AGR_NAME""AGR_NAME")

    DataGridView1.Columns.Add("FROM_DAT""FROM_DAT")

    DataGridView1.Columns.Add("TO_DAT""TO_DAT")

    DataGridView1.Columns.Add("ORG_FLAG""ORG_FLAG")

 

  End Sub

 

7.Add OnClick event at BLogon button with this code bellow :

  Private Sub BLogon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BLogon.Click

    Dim l_SAPLogonControl As Object 'New SAPLogonCtrl.SAPLogonControl

    Dim l_conn As Object 'SAPLogonCtrl.Connection

    Dim l_SAPFunction As New SAPFunctionsOCX.SAPFunctions

 

    Dim l_Function As SAPFunctionsOCX.Function

    Dim l_Table As SAPTableFactoryCtrl.Table

    Dim i As Integer

 

    l_SAPLogonControl = CreateObject("SAP.LogonControl.1")

    l_conn = l_SAPLogonControl.NewConnection

 

    l_conn.Client = eClient.Text

    l_conn.ApplicationServer = eApplicationServer.Text

    l_conn.SystemNumber = eSystemNumber.Text

    l_conn.System = eSystem.Text

    l_conn.User = eUsername.Text

    l_conn.Password = ePassword.Text

    l_conn.Language = eLanguage.Text

 

    If l_conn.Logon(0, TrueThen

      'Declare RFC Conncetion

      l_SAPFunction = CreateObject("SAP.Functions")

      l_SAPFunction.Connection = l_conn

      'Call RFC or BAPI

      l_Function = l_SAPFunction.Add("/CRYSTAL/GET_USER_LIST")

      'If RFC or BAPI doesn't need any export variable (in SAP is Imports)

      'l_Function.exports(< variable name for Import in SAP >).Value = < value >

      'Example:

      'l_Function.exports("PERNR").Value = Edit1.Text

      If Not l_Function.Call Then

        ' IF RFC or BAPI failed to call

        MsgBox(l_Function.Exception)

      Else

        'Called (Download) table variable from RFC or BAPI

        l_Table = l_Function.Tables.Item("USERACTGRP")

        ' If RFC or BAPI doesn't need any import variable (in SAP is Export)

 

        '< value > = l_Function.imports(<variable name for Export in SAP >).value

        'Example:

        'Edit2.Text = l_Function.imports('PERNR').value

 

        For i = 0 To l_Table.RowCount - 1

          'Get value from table

          'l_Table.Value(< data to >, < Field Name > )

          DataGridView1.Rows.Add()

          DataGridView1.Rows(i).Cells(0).Value = i + 1

          DataGridView1.Rows(i).Cells(1).Value = l_Table.Value(i + 1, "MANDT")

          DataGridView1.Rows(i).Cells(2).Value = l_Table.Value(i + 1, "BNAME")

          DataGridView1.Rows(i).Cells(3).Value = l_Table.Value(i + 1, "SUBSYSTEM")

          DataGridView1.Rows(i).Cells(4).Value = l_Table.Value(i + 1, "AGR_NAME")

          DataGridView1.Rows(i).Cells(5).Value = l_Table.Value(i + 1, "FROM_DAT")

          DataGridView1.Rows(i).Cells(6).Value = l_Table.Value(i + 1, "TO_DAT")

          DataGridView1.Rows(i).Cells(7).Value = l_Table.Value(i + 1, "ORG_FLAG")

 

        Next i

      End If

      'Close SAP Conncetion

      l_conn.Logoff()

    End If

 

  End Sub

 

7.Press “ Run/Start “ button for testing your code.