用VB开发SAP接口程序
SAP R/3系統是業界最先進、最穩定的ERP系統,國際和國內大型企業采用該系統的比例遙遙領先于其它ERP系統的總和。SAP R/3內建了二次開發平臺,使用的開發語言叫做ABAP,是一種類似于COBOL的編程語言。ABAP在報表輸出方面功能相對較弱(只能按行打印在屏幕上或者導出到Excel中處理),所以日常工作中經常需要在外部開發程序。通過程序接口自動讀取SAP R/3的數據表(視圖),在外部進行處理和利用水晶報表等工具生成符合中國人習慣的報表樣式。
SAP R/3的接口方式主要有RFC、IDOC、BAPI三種,本文要介紹的是相對比較簡單的RFC(Romote Function Call,遠程函數調用)。SAP系統RFC調用的原理其實很簡單,有一些類似于三層構架的C/S系統,第三方的客戶程序通過接口調用SAP內部的標準或自定義函數,獲得函數返回的數據進行處理后顯示或打印。下面是RFC調用的模型:
本文主要不是討論SAP R/3函數的開發,因為使用SAP的公司一般都有專門的ABAP開發人員。大家如果有機會接觸ABAP平臺,可以通過事務代碼SE37進入ABAP開發平臺的"函數編制器"進行函數開發測試。
下面主要以VB為例介紹一下SAP接口RFC的開發思路。在SAP客戶端SAP Gui Client安裝的時候,注意選擇安裝附帶的SDK包(最保險是選擇完全安裝)。RFC接口程序開發,主要用的是"SAP.Functions"這個控件,通過控件在外部程序模擬SAP Gui Client的用戶登錄和函數調用,然后返回函數的值。請看下面的程序片段和重點注解(尤其是CmdAdd_Click()):
?
VB 代碼:
?
'---QUERY / SELECTALL / CREATE / EDIT / DELETE /
Private Sub CmdAdd_Click()
??? '添加記錄
??? On Error GoTo myer
Dim Functions As Object
????? Dim Connect As Object
??? Dim logname As String, password As String
??? Dim xi
??? Dim func As Object
??? Dim TT As Object
???
??? Set Functions = CreateObject("Sap.Functions.Unicode")?? ' 創建RFC的本地對象
??? Set Connect = Functions.Connection???????????????? ' 設置連接
?? Connect.ApplicationServer = "192.168.1.108"
??? Connect.Client = "811"
??? Connect.SystemNumber = "00"
??? Connect.Language = "ZH"
??? Connect.User = "LIANXI02"
??? Connect.password = "sap123"
??? If Not Connect.logon(0, True) Then????????????? ' 軟件登錄SAP并判斷
????? MsgBox "登錄SAP R/3失敗,請重新登錄!" + Err.Description, vbOKOnly + vbExclamation, "系統提示"
????? Exit Sub
??? Else ' 登錄SAP成功
??????
??????? Set func = Functions.Add("ZRFC_01")??? '--ZRFC_01 是SAP里的函數 se37
??????? Set TT = func.tables("ZTEMPTABLE")
??????? func.Exports("ACTION") = "CREATE"
??????? With TT
??????????? 'Next we set fields to obtain
??????????? 'Refresh table
??????????? TT.FreeTable
??????????? 'Then set values,類似與ADDNEW
??????????
??????????? 'xi是獲取當前表的行數,以便定位記錄行
??????????? '此處在賦值時,用字段名稱來表示列,也可以用數字來表示,比如TT(xi, 1) = "iii"
??????????? '注意:行和列都是從1開始,不是從0開始計數
??????????? TT.Rows.Add
??????????? xi = TT.rowcount
??????????? TT(xi, "MATNR") = "Z1001"
??????????? TT(xi, "MAKTX") = "單機頭"
??????????
??????????? TT.Rows.Add
??????????? xi = TT.rowcount
??????????? TT(xi, "MATNR") = "Z1002"
??????????? TT(xi, "MAKTX") = "包材1"
??????????
??????????? TT.Refresh
??????????
??????? End With
??????? If func.call = True Then
????????
????????? x = func.imports("RETURN")
????????? y = func.imports("ERRNUM")
????????
????????? Debug.Print "x:" & x
????????? Debug.Print "y:" & y
??????? Else
??????????? MsgBox "call failed!"
??????? End If
??? End If
??? Connect.LogOff
??? Exit Sub
myer:
??? MsgBox Err.Description, vbInformation, "Info"
End Sub
?
?
SAP 里的函數
?FUNCTION?ZRFC_01.
*"----------------------------------------------------------------------
*"*"Local?interface:
*"??IMPORTING
*"?????VALUE(ACTION)?TYPE??/1SAP1/CL_ACH0001
*"?????VALUE(WHEREUSERID)?TYPE??/GC1/DTE_MSG_TXT?OPTIONAL
*"??EXPORTING
*"?????VALUE(RETURN)?TYPE??/AIN/ACT_PUB_PARAMNAME
*"?????VALUE(ERRNUM)?TYPE??/AIN/ACTIVITY_COUNTER
*"??TABLES
*"??????ZTEMPTABLE?STRUCTURE??ZTEST2
*"----------------------------------------------------------------------
tables:ZTEST2.
DATA:
??WA_ZTEMPTABLE?LIKE?ZTEMPTABLE,
??TRANSACTION_ID?LIKE?ARFCTID,
??V_VAILD(1)?TYPE?C.
??V_VAILD?=?'X'.
*?OPEN?A?DATA?TRANSACTION
??CALL?FUNCTION?'TRANSACTION_BEGIN'
????IMPORTING
??????TRANSACTION_ID?=?TRANSACTION_ID.
??CASE?ACTION?.
*按照關鍵字ZUSERID查詢
?????WHEN?'QUERY'.
??????SELECT?*?FROM?ZTEST2?INTO?TABLE?ZTEMPTABLE?where?MATNR?=?WHEREUSERID.
????????IF?SY-SUBRC?=?0.
????????ELSE.
??????????V_VAILD?=?''.
??????????ERRNUM?=?SY-SUBRC.
????????ENDIF.
*選擇所有的記錄
????WHEN?'SELECTALL'.
??????SELECT?*?FROM?ZTEST2?INTO?TABLE?ZTEMPTABLE.
????????IF?SY-SUBRC?=?0.
????????ELSE.
??????????V_VAILD?=?''.
??????????ERRNUM?=?SY-SUBRC.
????????ENDIF.
*插入記錄
????WHEN?'CREATE'.
??????LOOP?AT?ZTEMPTABLE?INTO?WA_ZTEMPTABLE.
????????INSERT?ZTEST2?FROM?WA_ZTEMPTABLE.
????????IF?SY-SUBRC?=?0.
????????ELSE.
??????????V_VAILD?=?''.
??????????ERRNUM?=?SY-SUBRC.
????????ENDIF.
??????ENDLOOP.
*修改記錄
????WHEN?'EDIT'.
??????LOOP?AT?ZTEMPTABLE?INTO?WA_ZTEMPTABLE.
????????UPDATE?ZTEST2?FROM?WA_ZTEMPTABLE.
????????IF?SY-SUBRC?=?0.
????????ELSE.
??????????V_VAILD?=?''.
??????????ERRNUM?=?SY-SUBRC.
????????ENDIF.
??????ENDLOOP.
*刪除記錄
????WHEN?'DELETE'.
??????LOOP?AT?ZTEMPTABLE?INTO?WA_ZTEMPTABLE.
????????DELETE?ZTEST2?FROM?WA_ZTEMPTABLE.
????????IF?SY-SUBRC?=?0.
????????ELSE.
??????????V_VAILD?=?''.
??????????ERRNUM?=?SY-SUBRC.
????????ENDIF.
??????ENDLOOP.
????WHEN?OTHERS.
??ENDCASE.
*如果V_VALID為X,則表示成功,否則表示失敗,并返回RETURN錯誤代碼
??IF?V_VAILD?=?'X'.
????CALL?FUNCTION?'TRANSACTION_END'
??????EXPORTING
????????TRANSACTION_ID?=?TRANSACTION_ID.
????RETURN?=?''.
??ELSE.
????CALL?FUNCTION?'TRANSACTION_ABORT'
??????EXPORTING
????????TRANSACTION_ID?=?TRANSACTION_ID.
????RETURN?=?'THE?CURRENT?ACTION?IS?FAILURE!'.
??ENDIF.
ENDFUNCTION.
?
說明: ZTEST2表是我自己定義的,包含了這三個字段:MANDT / MATNR /? MAKTX
你也可以換成你自己寫的表或用SAP里的表。
總結
以上是生活随笔為你收集整理的用VB开发SAP接口程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SAP ERP差异来源和差异处理
- 下一篇: VB连接SAP实例