最近在研究vb串連SAP的例子, 終於可以正常登入SAP通過RFC讀取SAP中的資料。
下面是具體的代碼:
vb6.0寫法:
'定義公開變數
Public Connect As Object
Public Functions As Object
'登入SAP
Private Sub Command1_Click()
'建立ocx對象
Set Functions = CreateObject("Sap.Functions.unicode")
'Sap.Functions.unicode 不加unicode的話無法正常顯示中文。這和登入時Language的設定沒有關係
'是因為字元集的原因。
'設定串連資訊
Set Connect = Functions.Connection
Connect.ApplicationServer = "172.18.95.173"
Connect.Client = "169"
Connect.Language = "ZH"
Connect.User = "CRMDEV69"
Connect.Password = "654321"
Connect.SystemNumber = 7
Connect.System = "CD2"
If Not Connect.Logon(0, True) Then
MsgBox "登入失敗"
Command1.SetFocus
Else
Command1.Enabled = False
MsgBox "登入成功"
End If
End Sub
'調用RFC的寫法
Private Sub Command2_Click()
Dim GetCustomers As Object
Dim Customers As Object
Dim i As Integer
'所要調用的RFC名稱
Set GetCustomers = Functions.Add("ZCSMS_GET_HRINFO")
'傳遞的輸入參數名稱並賦值
GetCustomers.Exports("BEGDAFROM") = ""
GetCustomers.Exports("BEGDATO") = ""
GetCustomers.Exports("MILL") = "7960"
GetCustomers.Exports("NUMBERFROM") = "0061500001"
GetCustomers.Exports("NUMBERTO") = "0061500080"
'執行後返回的Table,相當於二維數組
Set Customers = GetCustomers.Tables("THR")
'簡單的MsgBox彈出以查看值
If GetCustomers.Call Then
For i = 1 To Customers.RowCount
MsgBox Customers(i, "MILL")
MsgBox Customers(i, "PERNR")
MsgBox Customers(i, "NAME1")
MsgBox Customers(i, "STEXT")
Next i
Else
MsgBox " 函數調用失敗" + GetCustomers.exception
End If
End Sub