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

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.

posted on 2007-03-26 11:55  毛小娃  阅读(256)  评论(0编辑  收藏  举报

导航