使用VB通过RFC连接并处理SAP数据例子

使用VB建立SAP的PO数据

例1:

:Option Explicit

Dim functionCtrl As Object
Dim sapConnection As Object
Dim theFunc As Object
Dim objStruct

Private Sub Command1_Click()
If ConnectToSAP = False Then
MsgBox "Connect Error"
Exit Sub
End If

'Set theFunc = functionCtrl.Add("RFC_READ_TABLE")
Set theFunc = functionCtrl.Add("BAPI_PO_GETDETAIL")


If theFunc Is Nothing Then
MsgBox "Creating function module object failed"
Exit Sub
End If

theFunc.Exports("PURCHASEORDER") = "4500014788"

If theFunc.Call = False Then
MsgBox "Function call failed"
Exit Sub
End If

Set objStruct = theFunc.tables("PO_ITEMS")
objStruct.Value(2, "MATERIAL") = "MYITEMNUMBER"

Dim i As Integer
For i = 1 To objStruct.RowCount()
MsgBox objStruct.Value(i, "MATERIAL")
Next i

End Sub

Private Function ConnectToSAP() As Boolean
Set functionCtrl = CreateObject("SAP.Functions")
Set sapConnection = functionCtrl.Connection

sapConnection.client = "800"
sapConnection.user = "USERNAME"
sapConnection.Password = "PASSWORD"
sapConnection.language = "EN"

If sapConnection.Logon(0, False) = False Then
ConnectToSAP = False
Else
ConnectToSAP = True
End If
End Function

[@more@]

例2:

Option Explicit
Dim functionCtrl As Object
Dim sapConnection As Object
Dim theFunc As Object
Dim PoNumber

Private Sub Command1_Click()

Set functionCtrl = CreateObject("SAP.Functions")
Set sapConnection = functionCtrl.Connection
sapConnection.Client = "500"
sapConnection.user = "USERNAME"
sapConnection.password = "PASSWORD"
sapConnection.Language = "EN"

If sapConnection.logon(0, False) True Then
MsgBox "No connection to R/3 System"
Exit Sub 'End program
End If
Set theFunc = functionCtrl.Add("BAPI_PO_CREATE")


Dim poheader As Object
Dim poitems As Object
Dim poitemschedule As Object
Dim retMess As Object
Dim returnFunc As Boolean
Dim startzeil As Integer
Dim endcol As Integer
Dim the_name As String

Set poheader = theFunc.exports.Item("PO_HEADER")
Set poitems = theFunc.tables.Item("PO_ITEMS")
Set poitemschedule = theFunc.tables.Item("PO_ITEM_SCHEDULES")

poheader.Value("VENDOR") = Text1.Text

poheader.Value("PURCH_ORG") = Text2.Text
poheader.Value("PUR_GROUP") = Text3.Text
poheader.Value("DOC_TYPE") = Text4.Text

poitems.Rows.Add
poitems.Value(1, "PUR_MAT") = Text5.Text
poitems.Value(1, "PLANT") = Text6.Text
poitems.Value(1, "NET_PRICE") = Text7.Text


poitemschedule.Rows.Add
poitemschedule.Value(1, "DELIV_DATE") = Text8.Text
poitemschedule.Value(1, "QUANTITY") = Text9.Text

returnFunc = theFunc.call
PoNumber = theFunc.imports("PURCHASEORDER")
Set retMess = theFunc.tables.Item("RETURN")
If retMess Is Nothing Then

MsgBox retMess.Value(1, "MESSAGE")
Else
MsgBox "Purchase Order No : " & PoNumber & "Created"
End If

End Sub

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/9437124/viewspace-1010260/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/9437124/viewspace-1010260/

你可能感兴趣的:(使用VB通过RFC连接并处理SAP数据例子)