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

你可能感兴趣的:(DAO,vb)