起步软件技术论坛-X3

 找回密码
 立即注册
搜索
查看: 240|回复: 7

[分享][转载]SAP接口--BAPI方式**

[复制链接]
发表于 2007-10-19 16:52:34 | 显示全部楼层 |阅读模式
http://blog.csdn.net/CompassButt ... /03/26/1541285.aspx
===========================
1、安装sap客户端
安装SAPGui和SAP SDK

2、Delphi中安装ActiveX部件
2.1 SAPLogonControl, SAPBapiControl 安装
component --> ActiveX import   (imported components: TSAPLogonControl, TSAPBapiControl)
2.2 SAP remote Function call control安装
project --> type library import (imported components: TSAPFunctions, TFunction, TParameter, TExports, TImports, TStructure) 系统会自动建立一个文件:TSAPFunctionsOCX_TLB.pas

3、注意点

函数的参数值(Function values)
参数值的传入要和sap的定义的相同,如:数字的前导0不能删除,字符的大小写等.

事务提交(CommitWork)
有些BAPI需要显示的调用事务提交函数提交事务,具体请参考相关文档!

使用详见后续系列文章的样例代码。
回复

使用道具 举报

 楼主| 发表于 2007-10-19 16:55:01 | 显示全部楼层

BAPI / RFC with Delphi(系列之二)--TSAPLogonControl使用(有对话框的登录sap的delphi

http://blog.csdn.net/CompassButt ... /03/26/1541288.aspx
================================
1、新建一个Form,并在form上添加下列控件Component Function
SAPLogOnControl1  SAP ActiveX-Component to logon to the system
Button1                     Button to start the procedure


2、源代码如下
unit s_logon;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, OleCtrls, SAPLogonCtrl_TLB, StdCtrls,Grids ;

type
  TForm1 = class(TForm)
  SAPLogonControl1: TSAPLogonControl;
  Panel1: TPanel;
  StaticText1: TStaticText;
  Button1: TButton;
  procedure SAPLogonControl1Click(Sender: TObject);
  procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;

var
Form1: TForm1;
Connection :variant;

implementation

{$R *.DFM}

procedure TForm1.SAPLogonControl1Click(Sender: TObject);
begin

  (* define connection *)
  Connection:= SAPLogOnControl1.NewConnection;

  (* start LogOn *)
  if Connection.LogOn(0,false) = true then

  begin
    showmessage('Logon O.K.');
    Button1.Enabled:= true;
  end
  else
  begin
    ShowMessage('Error on Logon :-(((');
    SAPLogonControl1.Enabled:=true;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin

  (* cut connection *)
  Connection.LogOff;

  ShowMessage('System LogOff...');
  SAPLogonControl1.Enabled:=true;
  Button1.enabled :=false;
end;
end.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-10-19 16:56:13 | 显示全部楼层

BAPI / RFC with Delphi(系列之三)--TSAPLogonControl使用(无对话框的登录sap的delphi

http://blog.csdn.net/CompassButt ... /03/26/1541291.aspx
============================
1、新建一个Form,并在form上添加下列控件Component Function
SAPLogOnControl1 SAP ActiveX-Component to logon to the system
Button1 Button to start the procedure


2、源代码如下


unit s_logon;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, OleCtrls, SAPLogonCtrl_TLB, StdCtrls,Grids ;

type
  TForm1 = class(TForm)
  SAPLogonControl1: TSAPLogonControl;
  Panel1: TPanel;
  Edit1: TEdit;
  Edit2: TEdit;
  Label1: TLabel;
  Label2: TLabel;
  StaticText1: TStaticText;
  Button1: TButton;
  procedure SAPLogonControl1Click(Sender: TObject);
  procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;

var
Form1: TForm1;
Connection :variant;

implementation

{$R *.DFM}

procedure TForm1.SAPLogonControl1Click(Sender: TObject);
begin

  (* define connection and it's parameters *)
  Connection := SAPLogoncontrol1.newConnection;

  (* In some GUI-versions the username *)
  (* must be written in uppercase !!!  *)
  Connection.User := AnsiUpperCase(Edit1.text);

  Connection.System            := 'IDS';
  Connection.Client            := '800';
  Connection.ApplicationServer := 'SAPIDES';
  Connection.SystemNumber      := '00';
  Connection.Password          := Edit2.text;
  Connection.Language          := 'DE' ;
  SAPLogonControl1.Enabled     := false;

  if Connection.LogOn(0,true) = true then
  (* parameter "true" : SilentLogOn *)

  begin
    ShowMessage('Logon O.K.');
    Button1.Enabled:= true;
  end
  else
  begin
    ShowMessage('Error on logon :-(((');
    SAPLogonControl1.Enabled:=true;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin

  (* cut connection *)
  Connection.LogOff;

  ShowMessage('System LogOff...');
  SAPLogonControl1.Enabled:=true;
  Button1.Enabled :=false;
end;
end.




wdtfuncs   SAP data type not supportederror on line:Set i_TABNAME = Func.Exports("TABNAME")
Here is my VB code:

Option ExplicitPublic Functions As SAPFunctionsOCX.SAPFunctionsPrivate LogonControl As SAPLogonCtrl.SAPLogonControlPrivate R3Connection As SAPLogonCtrl.ConnectionPrivate TableFactory As SAPTableFactoryDim Func As SAPFunctionsOCX.FunctionPublic i_TABNAME  As SAPFunctionsOCX.ParameterPublic i_FIELD  As SAPFunctionsOCX.ParameterPublic i_LANGU  As SAPFunctionsOCX.ParameterPublic strLabel  As SAPFunctionsOCX.Parameter'Public tENTRIES  As SAPTableFactoryCtrl.TablePublic Sub Main()    Dim ix As Integer    Dim retcd As Boolean    Dim SilentLogon As Boolean    Set LogonControl = CreateObject("SAP.LogonControl.1")    Set Functions = CreateObject("SAP.Functions")    Set TableFactory = CreateObject("SAP.TableFactory.1")    Set R3Connection = LogonControl.NewConnection    R3Connection.client = "000"    R3Connection.ApplicationServer = "A1KGNB09"    R3Connection.language = "DE"    R3Connection.User = "BCUSER"    R3Connection.Password = "minisap"    R3Connection.System = "A1 SAP 620 09"    R3Connection.SystemID = "000"    R3Connection.SystemNumber = "00"    R3Connection.UseSAPLogonIni = False    SilentLogon = False       retcd = R3Connection.Logon(0, SilentLogon)    If retcd <> True Then MsgBox "Logon failed": Exit Sub       Functions.Connection = R3Connection    Set Func = Functions.Add("DDIF_FIELDLABEL_GET")    Set i_TABNAME = Func.Exports("TABNAME")    Set i_FIELD = Func.Exports("FIELDNAME")    Set i_LANGU = Func.Exports("LANGU")    Set strLabel = Func.Imports("LABEL")    i_TABNAME.Value = "USR02"    i_FIELD.Value = "BNAME"    i_LANGU.Value = "DE"    Func.Call      MsgBox strLabel'    Debug.Print eNUMBER_OF_ENTRIES'    For ix = 1 To tENTRIES.RowCount'        Debug.Print tENTRIES(ix, 1)'    Next    R3Connection.LogoffEnd Sub
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-10-19 16:57:36 | 显示全部楼层

BAPI / RFC with Delphi(系列之四)--TSAPFunctions使用(有登录对话框的delphi源代码)

http://blog.csdn.net/CompassButt ... /03/26/1541316.aspx
================================
1、新建一个Form,并在form上添加下列控件
Component function
SAPFunctions1 SAP ActiveX-component to connect RFC/BAPI
Grid Stringgrid to show the data's in the form
Button1 Button to start the procedure


2、源代码如下(使用RFC_READ_TABLE函数读取成本中心)
unit logon1;

interface

uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,ExtCtrls, OleCtrls, StdCtrls, SAPFunctionsOCX_TLB, Grids;

type
TForm1 = class(TForm)
  SAPFunctions1: TSAPFunctions;
  Button1: TButton;
  Grid: TStringGrid;
  procedure Button1Click(Sender: TObject);
private
{ }
public
{ }
end;

var
Form1 : TForm1 ;
Table,Funct : VARIANT ;

implementation
{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var txt : string  ;
    r   : integer ;
begin

  (* define function *)
  Funct := SAPFunctions1.add('RFC_READ_TABLE');

  (* tell the function what table should be read *)
  Funct.exports('QUERY_TABLE').value := 'CSKT';

  (* call the function *)
  if not Funct.call then

    (* on error show message *)
    showMessage(Funct.exception)

  else begin

    (* select table with the data's *)
    Table := Funct.tables.item('DATA');

    (* addjust the StringGrid *)
    grid.rowCount := Table.rowcount + 1;
    grid.cells[0,0] := 'Client';
    grid.cells[1,0] := 'Cost Number';
    grid.cells[2,0] := 'CostCenter Description';
    for r := 1 to grid.rowCount -1 do begin

      (* select first dataset *)
      txt := Table.value(r,1);

      (* Because the RCF-function returns only one    *)
      (* string whitch contains all data's, the       *)
      (* string must be cut to different parts        *)
      grid.cells[0,r] := copy(txt,0,3);   (* Client                *)
      grid.cells[1,r] := copy(txt,9,10);  (* CostCent-number     *)
      grid.cells[2,r] := copy(txt,27,20); (* CostCent-description*)
    end;
    grid.visible := True;
  end;
end;
end.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-10-19 17:00:53 | 显示全部楼层

BAPI / RFC with Delphi(系列之五)--TSAPFunctions和TSAPLogoncontrol使用(无登录对话

http://blog.csdn.net/CompassButt ... /03/26/1541327.aspx
===============================
1、新建一个Form,并在form上添加下列控件
Component Function
SAPFunctions1 SAP ActiveX-component to connect RFC/BAPI
SAPLogoncontrol1 SAP ActiveX-Component to logon to the system
Grid Stringgrid to show the data's in the form
Button1 Button to start the procedure


2、源代码如下(使用RFC_READ_TABLE函数读取成本中心)
unit logon1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, OleCtrls, StdCtrls, SAPFunctionsOCX_TLB,
Grids, SAPLogonCtrl_TLB;

type
  TForm1 = class(TForm)
  SAPFunctions1: TSAPFunctions;
  Button2: TButton;
  Grid: TStringGrid;
  Edit1: TEdit;
  Edit2: TEdit;
  Label1: TLabel;
  Label2: TLabel;
  SAPLogonControl1: TSAPLogonControl;
procedure Button2Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;

var
Form1 : TForm1 ;
Table,Funct, Connection : VARIANT ;

implementation

{$R *.DFM}


procedure TForm1.Button2Click(Sender: TObject);
var txt : string ;
r : integer ;

begin
  Connection                  := SAPLogoncontrol1.newConnection;
  Connection.User             := AnsiUpperCase(Edit1.text);
  Connection.System           := 'IDS';
  Connection.Client           := '800';
  Connection.ApplicationServer:= 'SAPIDES';
  Connection.SystemNumber     := '00';
  Connection.Password         := Edit2.text;
  Connection.Language         := 'DE' ;

  if Connection.LogOn(0,true) = true then
  (* parameter "true" = SilentLogOn *)

  begin

    (* assign the existing connection to the *)
    (* component SAPFunctions1                *)
    SAPFunctions1.Connection := Connection;

    Funct := SAPFunctions1.add('RFC_READ_TABLE');
    Funct.exports('QUERY_TABLE').value := 'CSKT';
    if not Funct.call then
      showMessage(Funct.exception)
    else begin
      Table := Funct.tables.item('DATA');
      grid.rowCount := Table.rowcount + 1;
      grid.cells[0,0] := 'Client';
      grid.cells[1,0] := 'CostCent-No';
      grid.cells[2,0] := 'CostCent-Des.';
      for r := 1 to grid.rowCount -1 do begin
        txt := Table.value(r,1);
        grid.cells[0,r] := copy(txt,0,3);
        grid.cells[1,r] := copy(txt,9,10);
        grid.cells[2,r] := copy(txt,27,20);
      end;
      grid.visible := True;
    end;
  end;
end;
end.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-10-19 17:17:17 | 显示全部楼层

BAPI / RFC with Delphi(系列之六)--TSAPFunctions使用BAPI创建PO(有登录对话框的delp

http://blog.csdn.net/CompassButt ... /03/26/1541340.aspx
=============================================================
1、新建一个Form,并在form上添加下列控件
Component Function

SAPFunctions1 SAP ActiveX-component to connect RFC/BAPI
Button1 Button to start the procedure
Panel1 not relevant!


2、源代码如下(使用BAPI_PO_CREATE函数创建PO)


unit PO_Create;

interface

uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,StdCtrls, OleCtrls, SAPFunctionsOCX_TLB, ExtCtrls;

type
  TForm1 = class(TForm)
  SAPFunctions1: TSAPFunctions;
  Button1: TButton;
  Panel1: TPanel;
  procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;

var
Form1: TForm1;
Funct,
Header,
POItems,
Schedules,
ItemsRow,
SchedulesRow: Variant;

implementation
{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var MLDText : String;
begin
  Button1.Enabled:=false;
  Panel1.Caption := 'RFC ist running, please wait.....';
  (* 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' ;

  (* purcasing organisation *)
  Header.Value[5] := '600' ;

  (* purcasing group *)
  Header.Value[6] := '610' ;

  (* vendor account number, on numeric values don't *)
  (* forget the leading zeroes!!!                     *)
  Header.Value[8] := '0099000123';

(*** 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]:='000000000000001161';

  (* storage location *)
  ItemsRow.Value[11]:='100';

  (* plant *)
  ItemsRow.Value[17]:='0001';

  (* netprice in purcasing document, *)
  (* in document currency              *)
  ItemsRow.Value[21]:='10,00';

(*** 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]:='30.05.2000';

  (* 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;
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-10-19 17:18:03 | 显示全部楼层

BAPI / RFC with Delphi(系列之七)--TBAPIControl使用BUS1001显示物料(Delphi源代码)

http://blog.csdn.net/CompassButt ... /03/26/1541424.aspx
=======================================================
1、新建一个Form,并在form上添加下列控件
Component Function
SAPBAPIControl1 SAP ActiveX-Component to connect to BAPI
Button1 Button to start the procedure
Panel1 Element to display the material-description


2、源代码如下(BUS1001显示物料)


unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, OleCtrls, SAPBAPIControlLib_TLB, ExtCtrls;

type
  TForm1 = class(TForm)
  SAPBAPIControl1: TSAPBAPIControl;
  Button1: TButton;
  Panel1: TPanel;
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;

var
Form1: TForm1;
Connection,MAT : Variant;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
  Connection:= SAPBapiControl1.Connection;
  if Connection.Logon then
  begin
    ShowMessage('LogOn');

    (* Call the object with the needed parameters *)
    MAT:= sapbapicontrol1.GetSAPObject('BUS1001','000000000000017550');

    (* Display material-description *)
    Panel1.Caption:=MAT.MATERIALDESCRIPTION;
  end;
end;

end.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-10-19 17:19:33 | 显示全部楼层

BAPI / RFC with Delphi(系列之八)--TBAPIControl使用BUS2012建立PO(Delphi源代码)

http://blog.csdn.net/CompassButt ... /03/26/1541437.aspx
=========================================================
1、新建一个Form,并在form上添加下列控件
Component Function
SAPLogonControl1 SAP ActiveX-Component to logon to the system

SAPBAPIControl1 SAP ActiveX-Component to connect to BAPI
Button1 Button to start the procedure
Button2 Button to logon
Panel1-3 Elements to display messages


2、源代码如下(BUS2012建立PO)


unit best;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, OleCtrls, SAPBAPIControlLib_TLB, ExtCtrls, Grids,
SAPLogonCtrl_TLB;

type
TForm1 = class(TForm)
  SAPBAPIControl1: TSAPBAPIControl;
  Button1: TButton;
  Panel1: TPanel;
  Panel2: TPanel;
  Panel3: TPanel;
  Button2: TButton;
  SAPLogonControl1: TSAPLogonControl;
  Edit1: TEdit;
  Edit2: TEdit;
  Label1: TLabel;
  Label2: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;

var
Form1: TForm1;
Connection,Mat,Header,Ret,Schedul,Item : Variant;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin

  (* select BusinessObject *)
  Mat:= SAPBapiControl1.GetSAPObject('BUS2012');

  (* define structures *)
  Header := SAPBapiCcontrol1.dimAs (Mat,'CreateFromData','PoHeader');
  Schedul:= SAPBapiCcontrol1.dimAs (Mat,'CreateFromData','PoItemSchedules');
  Item   := SAPBapiCcontrol1.dimAs (Mat,'CreateFromData','PoItems');
  Ret    := SAPBapiCcontrol1.dimAs (Mat,'CreateFromData','Return');

  (* purchaseorder header data's *)
  Header.value ('DOC_TYPE') := 'NB';
  Header.value ('DOC_CAT')  := 'F';
  Header.value ('PURCH_ORG'):= '10';
  Header.value ('PUR_GROUP'):= '10';
  Header.value ('VENDOR')   := '0010000999';

  (* data for position 00010 *)
  Item.Rows.Add;
  Item.Value (1,'PO_ITEM')   := '00010';
  Item.Value (1,'PUR_MAT')   := '000000000000000017';
  Item.Value (1,'STORE_LOC') := '100';
  Item.Value (1,'PLANT')     := '1000';
  Item.Value (1,'NET_PRICE') := '10,00';

  (* schedules for position 00010 *)
  Schedul.Rows.Add;
  Schedul.Value (1,'PO_ITEM')    := '00010';
  Schedul.Value (1,'DEL_DATCAT') := '1';
  Schedul.Value (1,'DELIV_DATE') := '20.09.2000';
  Schedul.Value (1,'QUANTITY')   := '10';

  (* data for position 00020 *)
  Item.Rows.Add;
  Item.value (2,'PO_ITEM')   := '00020';
  Item.value (2,'PUR_MAT')   := '000000000000001161';
  Item.value (2,'STORE_LOC') := '100';
  Item.value (2,'PLANT')     := '1000';
  Item.value (2,'NET_PRICE') := '10,00';

  (* schedules for position 00020 *)
  Schedul.Rows.Add;
  Schedul.Value (2,'PO_ITEM')    := '00020';
  Schedul.Value (2,'DEL_DATCAT') := '1';
  Schedul.Value (2,'DELIV_DATE') := '20.09.2000';
  Schedul.Value (2,'QUANTITY')   := '10';

  (* call the method CreateFromData *)
  Mat.CreateFromData (PoHeader           := Header,
                      SkipItemsWithError := ' ',
                      PoItems            := Item,
                      PoItemSchedules    := Schedul,
                      Return             := Ret);

  (* Errors are saved in the structure Ret *)
  if Ret.RowCount > 0 then
  begin
    Panel1.Caption:= Ret.Value (1,'TYPE');
    Panel2.Caption:= Ret.Value (1,'MESSAGE');
  end

  (* If the method was calles without errors, *)
  (* display the number of the purchaseorder  *)
  else Panel2.Caption:= Mat.PurchaseOrder;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin

  (* Logon to the system *)
  Connection                    := SAPLogoncontrol1.newConnection;
  Connection.User               := Ansiuppercase(Edit1.text);
  Connection.System             := 'IDS';
  Connection.Client             := '800';
  Connection.ApplicationServer  := 'SAPIDES';
  Connection.SystemNumber       := '00';
  Connection.Password           := Edit2.text;
  Connection.Language           := 'DE' ;
  SAPLogonControl1.Enabled      := false;

  if Connection.LogOn(0,true) = True then
  begin
    ShowMessage('Logon O.K.');
    Button1.Enabled:= true;

    (* assign the existing connection to the *)
    (* component SAPBapiControl1              *)
    SapBapiControl1.Connection:=Connection;
  end
  else
  begin
    ShowMessage('Error on logon :-(((');
  end;
end;
end.
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Justep Inc.

GMT+8, 2025-6-28 21:55 , Processed in 0.042119 second(s), 15 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表