[文件] Delphi 透過 RFC 連結 SAP ERP

  1.連線

sapconnection          :=
SAPLogonControl1 .NewConnection ;

 
sapconnection.User     :=
AnsiUpperCase(‘userid
‘);

 
sapconnection.system   := ‘TES’
;// server 簡稱 ‘TES’;

 
sapconnection.client   :=
‘105’  ;//  client id ‘105’;

 
sapconnection.applicationserver := ‘192.168.1.27’ ;//ip;

 
sapconnection.systemnumber:= ’00’ ; 
//’00’;

 
sapconnection.password := ‘passwd
‘ ;

 
sapconnection.language := ‘zf’ ;// ‘ZH’;

 
sapconnection.CodePage := ‘8300’;

 

2. CALL RFC

 
SAPFunctions1.RemoveAll;

 
funct :=SAPFunctions1.add(‘Z_PP_PROD’);  // function 名稱

 
funct.EXports(‘I_WERKS’).VALUE:= ‘1000’;  // 輸入的參數

 
funct.EXports(‘I_PRODNO’).VALUE:= ‘4500000001’;  // 輸入的參數2

 
funct.EXports(‘I_PRODITM’).VALUE:= ‘00010’;

 

  IF
NOT funct.CALL THEN

   
ShowMessage(‘
呼叫error’)

 
ELSE BEGIN

  
try

   
table :=funct.imports.Item(‘E_ZPPS009’); //連結成功 返回的 Table 

  
except

    
ShowMessage(‘error’)

  
end;

 

  // 可直接使用 TABLE  的值

   
StringGrid1 .Cells [1,1]:= 
TABLE.Value(‘WERKS’) ; 

   
StringGrid1 .Cells [2,1]:= 
TABLE.Value(‘PRODNO’);

   
StringGrid1 .Cells [3,1]:= 
TABLE.Value(‘PRODITM’);

   
StringGrid1 .Cells [4,1]:= 
TABLE.Value(‘LIFNR’);

   
StringGrid1 .Cells [5,1]:= 
TABLE.Value(‘NAME1’);

   
StringGrid1 .Cells [6,1]:= 
TABLE.Value(‘MATNR’);

 

 

 

1. 如何將ACTIVEX控件註冊到DELPHI, 以便利用SAP這些控件;

2. 利用SAP自動的FUNCTION:BAPI_PO_CREATE ,測試如何通過DELPHI去調用這個函數,創建採購訂單

例子很簡單,但是通過這個例子可以知道SAP於其它開發工具交互的一種方式(RFC:遠程調用),整個例子不需要在SAP做什麼配置(原以爲需要在DBCO中建立

連接,看來那個跟此沒有關係),開始做下,按此步驟: 1。安裝sap客戶端,安裝SAPGui和SAP SDK (如果你是SAP使用者,估計這部都做過了,沒有安裝在DELPHI中找不到相應的控件的)

2. Delphi中安裝ActiveX部件

   2.1 SAPLogonControl, SAPBapiControl 安裝

        component –> ActiveX import   (imported components: TSAPLogonControl, TSAPBapiControl)

     Delphi 連接SAP

   2.2 SAP remote Function call control安裝

        project –> type library import (imported components: TSAPFunctions, TFunction, TParameter, TExports, TImports, TStructure)

     Delphi 連接SAP

     系統會自動建立一個文件:TSAPFunctionsOCX_TLB.pas 

   做完這兩步驟後,可以在DELPHI中看到控件如下:

3.開始寫DELPHI代碼,測試連接

    3.1 新建一個DELPHI PROJECT,將SAPLogonControl1控件和SAPFunctions1控件拖到界面上;

3.2 再放兩個按鈕:一個是建立SAP連接,一個是調用SAP FUNCTION

     Connection :variant;(全局變量)

3.3 建立SAP連接:

       Connection                    := SAPLogoncontrol1.newConnection;

    Connection.User               := Ansiuppercase(‘wangnz’);

    Connection.System             := ‘IDS’;

    Connection.Client             := ‘800’;

    Connection.ApplicationServer := ‘192.168.5.112’;

    Connection.SystemNumber       := ’00’;

    Connection.Password           := ‘321’;

    //Connection.Language           := ‘DE’ ; //注意這個語言的,填DE還登錄不上

    SAPLogonControl1.Enabled      := false;

    if Connection.LogOn(0,true) = True then

    begin

    ShowMessage(‘Logon O.K.’);

    btn_CreatePO.Enabled:= true;

    SapBapiControl1.Connection:=Connection;

    sapFunctions1.Connection := Connection;

    end

    else

    begin

    ShowMessage(‘Error on logon :-(((‘);

    end;

    3.4 調用SAP FUNCTION:BAPI_PO_CREATE

     var MLDText : String;

    Funct,Header,POItems,Schedules,ItemsRow,

     SchedulesRow: Variant;

   begin

    (* define function *)

    Funct := sapFunctions1.add(‘BAPI_PO_CREATE’);

   (*** define tables, use structures of the dictionary ***)

    (* table for the purcaseorder header *)

    Header := funct.exports(‘PO_HEADER’);

    (* table of the purcaseorder items *)

    POItems := funct.tables.item(‘PO_ITEMS’);

    (* table of the schedules *)

    Schedules := funct.tables.item(‘PO_ITEM_SCHEDULES’);

   (*** filling the PO_Header-table ***)

    (* purcasing document type *)

    Header.Value[2] := ‘NB’ ;

    (* purcasing document category *)

    Header.Value[3] := ‘F’ ;

    Header.Value[5] := ‘1000’ ; //公司代碼

    (* purcasing organisation 採購組織 *)

    Header.Value[5] := ‘1000’ ;

    (* purcasing group *)

    Header.Value[6] := ‘026’ ;

    (* forget the leading zeroes!!!                     *)

    Header.Value[8] := ‘111’;   //供應商

   (*** filling the PO_Items-table ***)

    (* add new row to the table *)

    ItemsRow := POItems.rows.add;

    (* item number of purcasing document *)

    ItemsRow.Value[2]:=’00010′;

    (* material-number, on numeric values don’t forget *)

    (* the leading zeros !!!                            *)

    ItemsRow.Value[5]:=’100-210′;

    (* storage location *)

    ItemsRow.Value[11]:=’0001′;

    (* plant *)

    ItemsRow.Value[17]:=’1000′;

    (* netprice in purcasing document, *)

    (* in document currency              *)

    ItemsRow.Value[21]:=’10000′;

   (*** filling the PO_Items_Schedules-table ***)

    (* add new row to the table *)

    SchedulesRow := Schedules.rows.add;

    (* item number of purcasing document *)

    SchedulesRow.Value[1]:=’00010′;

    (* category of delivery date *)

    SchedulesRow.Value[3]:=’1′;

    (* item delivery date *)

    SchedulesRow.Value[4]:=’20000523′;

    (* scheduled quantity *)

    SchedulesRow.Value[6]:=’10’;

   (*** call function ***)

    if not funct.call then

    (* on error show message *)

    showMessage(funct.exception)

    else

    begin

    (* show number of the purcaseorder *)

    MLDText:= funct.imports(‘PURCHASEORDER’);

    MessageDlg(‘purcaseorder ‘+MLDText+’ created.’,MTInformation,[mbOK],0);

    end;

   end;

如此就將SAP連接起來,具體的實現SAP的功能都在SAP SE37中寫功能而已; 

 

分類未分類

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料