VB To Connect and Get Data From RFC

How to let programs written in VB interface to SAP through RFC? 

By : Worached

Below is source in VB to connect and get data from RFC.

Sub GetData()

'Declare the objects and variables
'
Dim functionCtrl As Object             'Function Control (Collective object)
Dim sapConnection As Object         'Connection object
Dim theFunc As Object                 'Function object

Set functionCtrl = CreateObject("SAP.Functions")
Set sapConnection = functionCtrl.Connection

       sapConnection.User = <user name>
       sapConnection.Password = <password>
       sapConnection.System = "00"
       sapConnection.ApplicationServer = "107.10.xx.xx"     'TSP
       sapConnection.SystemNumber = "00"
       sapConnection.Client = "100"
       sapConnection.Language = "EN"
       sapConnection.codepage = "8600"

 ' logon to SAP without presenting the logonscreen
   'If sapConnection.Logon(0, False) <> True Then
    If sapConnection.Logon(0, True) <> True Then
        MsgBox "No connection to R/3!"
        Exit Sub                                           'End program
    End If
Set theFunc = functionCtrl.Add("RFC_CIS_DIV")

Dim ITAB As Object
Dim returnFunc As Boolean
Dim startzeil As Integer
Dim endcol As Integer
Dim the_name As String
startzeil = 3

        theFunc.exports("MATNR") = "*"

'Call function
        returnFunc = theFunc.Call
        die_exception = theFunc.Exception

        If returnFunc <> True Then
            GoTo ConnectionClose
        End If

    Dim DB As DAO.Database, RS As DAO.Recordset
    Set DB = CurrentDb
    Set RS = DB.OpenRecordset("0Master_CIS_DIV")

    Do Until RS.EOF
        RS.Delete
        RS.MoveNext

    Loop
    Set ITAB = theFunc.tables.Item("MARA_T")

            For Each it_row In ITAB.rows
                RS.AddNew
                RS("material") = it_row("matnr")
                RS("mtype") = it_row("mtart")
                RS("mgroup") = it_row("matkl")
                RS("div") = it_row("spart")
                RS.Update

            Next it_row

ConnectionClose:
'Close_Connection
 functionCtrl.Connection.logoff
Set sapConnection = Nothing
Set functionCtrl = Nothing
MsgBox "Material master detail with Div. has been import from SAP(MARA)", vbInformation, "Finish!"

End Sub

Get help for your ABAP problems
Do you have a ABAP Question?

ABAP Books
ABAP Certification, BAPI, Java, Web Programming, Smart Forms, Sapscripts Reference Books

More ABAP Tips

Best regards,
SAP Basis, ABAP Programming and Other IMG Stuff
http://www.erpgreat.com

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.  The site www.erpgreat.com is in no way affiliated with SAP AG. 
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk. 
 The content on this site may not be reproduced or redistributed without the express written permission of 
www.erpgreat.com or the content authors.