BAPI/RFCwithDelphi(系列之一)--安装部件
1、安装sap客户端
安装SAPGui和SAPSDK
2、Delphi中安装ActiveX部件
2.1SAPLogonControl,SAPBapiControl安装
component-->ActiveXimport(importedcomponents:TSAPLogonControl,TSAPBapiControl)
2.2SAPremoteFunctioncallcontrol安装
project-->typelibraryimport(importedcomponents:TSAPFunctions,TFunction,TParameter,TExports,TImports,TStructure)系统会自动建立一个文件:TSAPFunctionsOCX_TLB.pas
3、注意点
函数的参数值(Functionvalues)
参数值的传入要和sap的定义的相同,如:数字的前导0不能删除,字符的大小写等.
事务提交(CommitWork)
有些BAPI需要显示的调用事务提交函数提交事务,具体请参考相关文档!
使用详见后续系列文章的样例代码。
BAPI/RFCwithDelphi(系列之二)--TSAPLogonControl使用(有对话框的登录sap的delphi源代码)
1、新建一个Form,并在form上添加下列控件ComponentFunction
SAPLogOnControl1SAPActiveX-Componenttologontothesystem
Button1Buttontostarttheprocedure
2、源代码如下
units_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;
procedureSAPLogonControl1Click(Sender:TObject);
procedureButton1Click(Sender:TObject);
private
{Private-Deklarationen}
public
{Public-Deklarationen}
end;
var
Form1:TForm1;
Connection:variant;
implementation
{$R*.DFM}
procedureTForm1.SAPLogonControl1Click(Sender:TObject);
begin
(*defineconnection*)
Connection:=SAPLogOnControl1.NewConnection;
(*startLogOn*)
ifConnection.LogOn(0,false)=truethen
begin
showmessage('LogonO.K.');
Button1.Enabled:=true;
end
else
begin
ShowMessage('ErroronLogon:-(((');
SAPLogonControl1.Enabled:=true;
end;
end;
procedureTForm1.Button1Click(Sender:TObject);
begin
(*cutconnection*)
Connection.LogOff;
ShowMessage('SystemLogOff...');
SAPLogonControl1.Enabled:=true;
Button1.enabled:=false;
end;
end.
BAPI/RFCwithDelphi(系列之三)--TSAPLogonControl使用(无对话框的登录sap的delphi源代码)
1、新建一个Form,并在form上添加下列控件ComponentFunction
SAPLogOnControl1SAPActiveX-Componenttologontothesystem
Button1Buttontostarttheprocedure
2、源代码如下
units_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;
procedureSAPLogonControl1Click(Sender:TObject);
procedureButton1Click(Sender:TObject);
private
{Private-Deklarationen}
public
{Public-Deklarationen}
end;
var
Form1:TForm1;
Connection:variant;
implementation
{$R*.DFM}
procedureTForm1.SAPLogonControl1Click(Sender:TObject);
begin
(*defineconnectionandit'sparameters*)
Connection:=SAPLogoncontrol1.newConnection;
(*InsomeGUI-versionstheusername*)
(*mustbewritteninuppercase!!!*)
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;
ifConnection.LogOn(0,true)=truethen
(*parameter"true":SilentLogOn*)
begin
ShowMessage('LogonO.K.');
Button1.Enabled:=true;
end
else
begin
ShowMessage('Erroronlogon:-(((');
SAPLogonControl1.Enabled:=true;
end;
end;
procedureTForm1.Button1Click(Sender:TObject);
begin
(*cutconnection*)
Connection.LogOff;
ShowMessage('SystemLogOff...');
SAPLogonControl1.Enabled:=true;
Button1.Enabled:=false;
end;
end.
BAPI/RFCwithDelphi(系列之四)--TSAPFunctions使用(有登录对话框的delphi源代码)
1、新建一个Form,并在form上添加下列控件
Componentfunction
SAPFunctions1SAPActiveX-componenttoconnectRFC/BAPI
GridStringgridtoshowthedata'sintheform
Button1Buttontostarttheprocedure
2、源代码如下(使用RFC_READ_TABLE函数读取成本中心)
unitlogon1;
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;
procedureButton1Click(Sender:TObject);
private
{}
public
{}
end;
var
Form1:TForm1;
Table,Funct:VARIANT;
implementation
{$R*.DFM}
procedureTForm1.Button1Click(Sender:TObject);
vartxt:string;
r:integer;
begin
(*definefunction*)
Funct:=SAPFunctions1.add('RFC_READ_TABLE');
(*tellthefunctionwhattableshouldberead*)
Funct.exports('QUERY_TABLE').value:='CSKT';
(*callthefunction*)
ifnotFunct.callthen
(*onerrorshowmessage*)
showMessage(Funct.exception)
elsebegin
(*selecttablewiththedata's*)
Table:=Funct.tables.item('DATA');
(*addjusttheStringGrid*)
grid.rowCount:=Table.rowcount+1;
grid.cells[0,0]:='Client';
grid.cells[1,0]:='CostNumber';
grid.cells[2,0]:='CostCenterDescription';
forr:=1togrid.rowCount-1dobegin
(*selectfirstdataset*)
txt:=Table.value(r,1);
(*BecausetheRCF-functionreturnsonlyone*)
(*stringwhitchcontainsalldata's,the*)
(*stringmustbecuttodifferentparts*)
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.
BAPI/RFCwithDelphi(系列之五)--TSAPFunctions和TSAPLogoncontrol使用(无登录对话框Delphi源代码)
1、新建一个Form,并在form上添加下列控件
ComponentFunction
SAPFunctions1SAPActiveX-componenttoconnectRFC/BAPI
SAPLogoncontrol1SAPActiveX-Componenttologontothesystem
GridStringgridtoshowthedata'sintheform
Button1Buttontostarttheprocedure
2、源代码如下(使用RFC_READ_TABLE函数读取成本中心)
unitlogon1;
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;
procedureButton2Click(Sender:TObject);
private
{Private-Deklarationen}
public
{Public-Deklarationen}
end;
var
Form1:TForm1;
Table,Funct,Connection:VARIANT;
implementation
{$R*.DFM}
procedureTForm1.Button2Click(Sender:TObject);
vartxt: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';
ifConnection.LogOn(0,true)=truethen
(*parameter"true"=SilentLogOn*)
begin
(*assigntheexistingconnectiontothe*)
(*componentSAPFunctions1*)
SAPFunctions1.Connection:=Connection;
Funct:=SAPFunctions1.add('RFC_READ_TABLE');
Funct.exports('QUERY_TABLE').value:='CSKT';
ifnotFunct.callthen
showMessage(Funct.exception)
elsebegin
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.';
forr:=1togrid.rowCount-1dobegin
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.
BAPI/RFCwithDelphi(系列之六)--TSAPFunctions使用BAPI创建PO(有登录对话框的delphi源代码)
1、新建一个Form,并在form上添加下列控件
ComponentFunction
SAPFunctions1SAPActiveX-componenttoconnectRFC/BAPI
Button1Buttontostarttheprocedure
Panel1notrelevant!
2、源代码如下(使用BAPI_PO_CREATE函数创建PO)
unitPO_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;
procedureButton1Click(Sender:TObject);
private
{Private-Deklarationen}
public
{Public-Deklarationen}
end;
var
Form1:TForm1;
Funct,
Header,
POItems,
Schedules,
ItemsRow,
SchedulesRow:Variant;
implementation
{$R*.DFM}
procedureTForm1.Button1Click(Sender:TObject);
varMLDText:String;
begin
Button1.Enabled:=false;
Panel1.Caption:='RFCistrunning,pleasewait.....';
(*definefunction*)
Funct:=sapFunctions1.add('BAPI_PO_CREATE');
(***definetables,usestructuresofthedictionary***)
(*tableforthepurcaseorderheader*)
Header:=funct.exports('PO_HEADER');
(*tableofthepurcaseorderitems*)
POItems:=funct.tables.item('PO_ITEMS');
(*tableoftheschedules*)
Schedules:=funct.tables.item('PO_ITEM_SCHEDULES');
(***fillingthePO_Header-table***)
(*purcasingdocumenttype*)
Header.Value[2]:='NB';
(*purcasingdocumentcategory*)
Header.Value[3]:='F';
(*purcasingorganisation*)
Header.Value[5]:='600';
(*purcasinggroup*)
Header.Value[6]:='610';
(*vendoraccountnumber,onnumericvaluesdon't*)
(*forgettheleadingzeroes!!!*)
Header.Value[8]:='0099000123';
(***fillingthePO_Items-table***)
(*addnewrowtothetable*)
ItemsRow:=POItems.rows.add;
(*itemnumberofpurcasingdocument*)
ItemsRow.Value[2]:='00010';
(*material-number,onnumericvaluesdon'tforget*)
(*theleadingzeros!!!*)
ItemsRow.Value[5]:='000000000000001161';
(*storagelocation*)
ItemsRow.Value[11]:='100';
(*plant*)
ItemsRow.Value[17]:='0001';
(*netpriceinpurcasingdocument,*)
(*indocumentcurrency*)
ItemsRow.Value[21]:='10,00';
(***fillingthePO_Items_Schedules-table***)
(*addnewrowtothetable*)
SchedulesRow:=Schedules.rows.add;
(*itemnumberofpurcasingdocument*)
SchedulesRow.Value[1]:='00010';
(*categoryofdeliverydate*)
SchedulesRow.Value[3]:='1';
(*itemdeliverydate*)
SchedulesRow.Value[4]:='30.05.2000';
(*scheduledquantity*)
SchedulesRow.Value[6]:='10';
(***callfunction***)
ifnotfunct.callthen
(*onerrorshowmessage*)
showMessage(funct.exception)
else
begin
(*shownumberofthepurcaseorder*)
MLDText:=funct.imports('PURCHASEORDER');
MessageDlg('purcaseorder'+MLDText+'created.',
MTInformation,[mbOK],0);
end;
end;
BAPI/RFCwithDelphi(系列之七)--TBAPIControl使用BUS1001显示物料(Delphi源代码)
1、新建一个Form,并在form上添加下列控件
ComponentFunction
SAPBAPIControl1SAPActiveX-ComponenttoconnecttoBAPI
Button1Buttontostarttheprocedure
Panel1Elementtodisplaythematerial-description
2、源代码如下(BUS1001显示物料)
unitUnit1;
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;
procedureButton1Click(Sender:TObject);
private
{Private-Deklarationen}
public
{Public-Deklarationen}
end;
var
Form1:TForm1;
Connection,MAT:Variant;
implementation
{$R*.DFM}
procedureTForm1.Button1Click(Sender:TObject);
begin
Connection:=SAPBapiControl1.Connection;
ifConnection.Logonthen
begin
ShowMessage('LogOn');
(*Calltheobjectwiththeneededparameters*)
MAT:=sapbapicontrol1.GetSAPObject('BUS1001','000000000000017550');
(*Displaymaterial-description*)
Panel1.Caption:=MAT.MATERIALDESCRIPTION;
end;
end;
end.
BAPI/RFCwithDelphi(系列之八)--TBAPIControl使用BUS2012建立PO(Delphi源代码)
1、新建一个Form,并在form上添加下列控件
ComponentFunction
SAPLogonControl1SAPActiveX-Componenttologontothesystem
SAPBAPIControl1SAPActiveX-ComponenttoconnecttoBAPI
Button1Buttontostarttheprocedure
Button2Buttontologon
Panel1-3Elementstodisplaymessages
2、源代码如下(BUS2012建立PO)
unitbest;
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;
procedureButton1Click(Sender:TObject);
procedureButton2Click(Sender:TObject);
private
{Private-Deklarationen}
public
{Public-Deklarationen}
end;
var
Form1:TForm1;
Connection,Mat,Header,Ret,Schedul,Item:Variant;
implementation
{$R*.DFM}
procedureTForm1.Button1Click(Sender:TObject);
begin
(*selectBusinessObject*)
Mat:=SAPBapiControl1.GetSAPObject('BUS2012');
(*definestructures*)
Header:=SAPBapiCcontrol1.dimAs(Mat,'CreateFromData','PoHeader');
Schedul:=SAPBapiCcontrol1.dimAs(Mat,'CreateFromData','PoItemSchedules');
Item:=SAPBapiCcontrol1.dimAs(Mat,'CreateFromData','PoItems');
Ret:=SAPBapiCcontrol1.dimAs(Mat,'CreateFromData','Return');
(*purchaseorderheaderdata'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';
(*dataforposition00010*)
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';
(*schedulesforposition00010*)
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';
(*dataforposition00020*)
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';
(*schedulesforposition00020*)
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';
(*callthemethodCreateFromData*)
Mat.CreateFromData(PoHeader:=Header,
SkipItemsWithError:='',
PoItems:=Item,
PoItemSchedules:=Schedul,
Return:=Ret);
(*ErrorsaresavedinthestructureRet*)
ifRet.RowCount>0then
begin
Panel1.Caption:=Ret.Value(1,'TYPE');
Panel2.Caption:=Ret.Value(1,'MESSAGE');
end
(*Ifthemethodwascalleswithouterrors,*)
(*displaythenumberofthepurchaseorder*)
elsePanel2.Caption:=Mat.PurchaseOrder;
end;
procedureTForm1.Button2Click(Sender:TObject);
begin
(*Logontothesystem*)
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;
ifConnection.LogOn(0,true)=Truethen
begin
ShowMessage('LogonO.K.');
Button1.Enabled:=true;
(*assigntheexistingconnectiontothe*)
(*componentSAPBapiControl1*)
SapBapiControl1.Connection:=Connection;
end
else
begin
ShowMessage('Erroronlogon:-(((');
end;
end;
end.
1、安装sap客户端
安装SAPGui和SAPSDK
2、Delphi中安装ActiveX部件
2.1SAPLogonControl,SAPBapiControl安装
component-->ActiveXimport(importedcomponents:TSAPLogonControl,TSAPBapiControl)
2.2SAPremoteFunctioncallcontrol安装
project-->typelibraryimport(importedcomponents:TSAPFunctions,TFunction,TParameter,TExports,TImports,TStructure)系统会自动建立一个文件:TSAPFunctionsOCX_TLB.pas
3、注意点
函数的参数值(Functionvalues)
参数值的传入要和sap的定义的相同,如:数字的前导0不能删除,字符的大小写等.
事务提交(CommitWork)
有些BAPI需要显示的调用事务提交函数提交事务,具体请参考相关文档!
使用详见后续系列文章的样例代码。
BAPI/RFCwithDelphi(系列之二)--TSAPLogonControl使用(有对话框的登录sap的delphi源代码)
1、新建一个Form,并在form上添加下列控件ComponentFunction
SAPLogOnControl1SAPActiveX-Componenttologontothesystem
Button1Buttontostarttheprocedure
2、源代码如下
units_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;
procedureSAPLogonControl1Click(Sender:TObject);
procedureButton1Click(Sender:TObject);
private
{Private-Deklarationen}
public
{Public-Deklarationen}
end;
var
Form1:TForm1;
Connection:variant;
implementation
{$R*.DFM}
procedureTForm1.SAPLogonControl1Click(Sender:TObject);
begin
(*defineconnection*)
Connection:=SAPLogOnControl1.NewConnection;
(*startLogOn*)
ifConnection.LogOn(0,false)=truethen
begin
showmessage('LogonO.K.');
Button1.Enabled:=true;
end
else
begin
ShowMessage('ErroronLogon:-(((');
SAPLogonControl1.Enabled:=true;
end;
end;
procedureTForm1.Button1Click(Sender:TObject);
begin
(*cutconnection*)
Connection.LogOff;
ShowMessage('SystemLogOff...');
SAPLogonControl1.Enabled:=true;
Button1.enabled:=false;
end;
end.
BAPI/RFCwithDelphi(系列之三)--TSAPLogonControl使用(无对话框的登录sap的delphi源代码)
1、新建一个Form,并在form上添加下列控件ComponentFunction
SAPLogOnControl1SAPActiveX-Componenttologontothesystem
Button1Buttontostarttheprocedure
2、源代码如下
units_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;
procedureSAPLogonControl1Click(Sender:TObject);
procedureButton1Click(Sender:TObject);
private
{Private-Deklarationen}
public
{Public-Deklarationen}
end;
var
Form1:TForm1;
Connection:variant;
implementation
{$R*.DFM}
procedureTForm1.SAPLogonControl1Click(Sender:TObject);
begin
(*defineconnectionandit'sparameters*)
Connection:=SAPLogoncontrol1.newConnection;
(*InsomeGUI-versionstheusername*)
(*mustbewritteninuppercase!!!*)
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;
ifConnection.LogOn(0,true)=truethen
(*parameter"true":SilentLogOn*)
begin
ShowMessage('LogonO.K.');
Button1.Enabled:=true;
end
else
begin
ShowMessage('Erroronlogon:-(((');
SAPLogonControl1.Enabled:=true;
end;
end;
procedureTForm1.Button1Click(Sender:TObject);
begin
(*cutconnection*)
Connection.LogOff;
ShowMessage('SystemLogOff...');
SAPLogonControl1.Enabled:=true;
Button1.Enabled:=false;
end;
end.
BAPI/RFCwithDelphi(系列之四)--TSAPFunctions使用(有登录对话框的delphi源代码)
1、新建一个Form,并在form上添加下列控件
Componentfunction
SAPFunctions1SAPActiveX-componenttoconnectRFC/BAPI
GridStringgridtoshowthedata'sintheform
Button1Buttontostarttheprocedure
2、源代码如下(使用RFC_READ_TABLE函数读取成本中心)
unitlogon1;
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;
procedureButton1Click(Sender:TObject);
private
{}
public
{}
end;
var
Form1:TForm1;
Table,Funct:VARIANT;
implementation
{$R*.DFM}
procedureTForm1.Button1Click(Sender:TObject);
vartxt:string;
r:integer;
begin
(*definefunction*)
Funct:=SAPFunctions1.add('RFC_READ_TABLE');
(*tellthefunctionwhattableshouldberead*)
Funct.exports('QUERY_TABLE').value:='CSKT';
(*callthefunction*)
ifnotFunct.callthen
(*onerrorshowmessage*)
showMessage(Funct.exception)
elsebegin
(*selecttablewiththedata's*)
Table:=Funct.tables.item('DATA');
(*addjusttheStringGrid*)
grid.rowCount:=Table.rowcount+1;
grid.cells[0,0]:='Client';
grid.cells[1,0]:='CostNumber';
grid.cells[2,0]:='CostCenterDescription';
forr:=1togrid.rowCount-1dobegin
(*selectfirstdataset*)
txt:=Table.value(r,1);
(*BecausetheRCF-functionreturnsonlyone*)
(*stringwhitchcontainsalldata's,the*)
(*stringmustbecuttodifferentparts*)
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.
BAPI/RFCwithDelphi(系列之五)--TSAPFunctions和TSAPLogoncontrol使用(无登录对话框Delphi源代码)
1、新建一个Form,并在form上添加下列控件
ComponentFunction
SAPFunctions1SAPActiveX-componenttoconnectRFC/BAPI
SAPLogoncontrol1SAPActiveX-Componenttologontothesystem
GridStringgridtoshowthedata'sintheform
Button1Buttontostarttheprocedure
2、源代码如下(使用RFC_READ_TABLE函数读取成本中心)
unitlogon1;
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;
procedureButton2Click(Sender:TObject);
private
{Private-Deklarationen}
public
{Public-Deklarationen}
end;
var
Form1:TForm1;
Table,Funct,Connection:VARIANT;
implementation
{$R*.DFM}
procedureTForm1.Button2Click(Sender:TObject);
vartxt: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';
ifConnection.LogOn(0,true)=truethen
(*parameter"true"=SilentLogOn*)
begin
(*assigntheexistingconnectiontothe*)
(*componentSAPFunctions1*)
SAPFunctions1.Connection:=Connection;
Funct:=SAPFunctions1.add('RFC_READ_TABLE');
Funct.exports('QUERY_TABLE').value:='CSKT';
ifnotFunct.callthen
showMessage(Funct.exception)
elsebegin
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.';
forr:=1togrid.rowCount-1dobegin
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.
BAPI/RFCwithDelphi(系列之六)--TSAPFunctions使用BAPI创建PO(有登录对话框的delphi源代码)
1、新建一个Form,并在form上添加下列控件
ComponentFunction
SAPFunctions1SAPActiveX-componenttoconnectRFC/BAPI
Button1Buttontostarttheprocedure
Panel1notrelevant!
2、源代码如下(使用BAPI_PO_CREATE函数创建PO)
unitPO_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;
procedureButton1Click(Sender:TObject);
private
{Private-Deklarationen}
public
{Public-Deklarationen}
end;
var
Form1:TForm1;
Funct,
Header,
POItems,
Schedules,
ItemsRow,
SchedulesRow:Variant;
implementation
{$R*.DFM}
procedureTForm1.Button1Click(Sender:TObject);
varMLDText:String;
begin
Button1.Enabled:=false;
Panel1.Caption:='RFCistrunning,pleasewait.....';
(*definefunction*)
Funct:=sapFunctions1.add('BAPI_PO_CREATE');
(***definetables,usestructuresofthedictionary***)
(*tableforthepurcaseorderheader*)
Header:=funct.exports('PO_HEADER');
(*tableofthepurcaseorderitems*)
POItems:=funct.tables.item('PO_ITEMS');
(*tableoftheschedules*)
Schedules:=funct.tables.item('PO_ITEM_SCHEDULES');
(***fillingthePO_Header-table***)
(*purcasingdocumenttype*)
Header.Value[2]:='NB';
(*purcasingdocumentcategory*)
Header.Value[3]:='F';
(*purcasingorganisation*)
Header.Value[5]:='600';
(*purcasinggroup*)
Header.Value[6]:='610';
(*vendoraccountnumber,onnumericvaluesdon't*)
(*forgettheleadingzeroes!!!*)
Header.Value[8]:='0099000123';
(***fillingthePO_Items-table***)
(*addnewrowtothetable*)
ItemsRow:=POItems.rows.add;
(*itemnumberofpurcasingdocument*)
ItemsRow.Value[2]:='00010';
(*material-number,onnumericvaluesdon'tforget*)
(*theleadingzeros!!!*)
ItemsRow.Value[5]:='000000000000001161';
(*storagelocation*)
ItemsRow.Value[11]:='100';
(*plant*)
ItemsRow.Value[17]:='0001';
(*netpriceinpurcasingdocument,*)
(*indocumentcurrency*)
ItemsRow.Value[21]:='10,00';
(***fillingthePO_Items_Schedules-table***)
(*addnewrowtothetable*)
SchedulesRow:=Schedules.rows.add;
(*itemnumberofpurcasingdocument*)
SchedulesRow.Value[1]:='00010';
(*categoryofdeliverydate*)
SchedulesRow.Value[3]:='1';
(*itemdeliverydate*)
SchedulesRow.Value[4]:='30.05.2000';
(*scheduledquantity*)
SchedulesRow.Value[6]:='10';
(***callfunction***)
ifnotfunct.callthen
(*onerrorshowmessage*)
showMessage(funct.exception)
else
begin
(*shownumberofthepurcaseorder*)
MLDText:=funct.imports('PURCHASEORDER');
MessageDlg('purcaseorder'+MLDText+'created.',
MTInformation,[mbOK],0);
end;
end;
BAPI/RFCwithDelphi(系列之七)--TBAPIControl使用BUS1001显示物料(Delphi源代码)
1、新建一个Form,并在form上添加下列控件
ComponentFunction
SAPBAPIControl1SAPActiveX-ComponenttoconnecttoBAPI
Button1Buttontostarttheprocedure
Panel1Elementtodisplaythematerial-description
2、源代码如下(BUS1001显示物料)
unitUnit1;
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;
procedureButton1Click(Sender:TObject);
private
{Private-Deklarationen}
public
{Public-Deklarationen}
end;
var
Form1:TForm1;
Connection,MAT:Variant;
implementation
{$R*.DFM}
procedureTForm1.Button1Click(Sender:TObject);
begin
Connection:=SAPBapiControl1.Connection;
ifConnection.Logonthen
begin
ShowMessage('LogOn');
(*Calltheobjectwiththeneededparameters*)
MAT:=sapbapicontrol1.GetSAPObject('BUS1001','000000000000017550');
(*Displaymaterial-description*)
Panel1.Caption:=MAT.MATERIALDESCRIPTION;
end;
end;
end.
BAPI/RFCwithDelphi(系列之八)--TBAPIControl使用BUS2012建立PO(Delphi源代码)
1、新建一个Form,并在form上添加下列控件
ComponentFunction
SAPLogonControl1SAPActiveX-Componenttologontothesystem
SAPBAPIControl1SAPActiveX-ComponenttoconnecttoBAPI
Button1Buttontostarttheprocedure
Button2Buttontologon
Panel1-3Elementstodisplaymessages
2、源代码如下(BUS2012建立PO)
unitbest;
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;
procedureButton1Click(Sender:TObject);
procedureButton2Click(Sender:TObject);
private
{Private-Deklarationen}
public
{Public-Deklarationen}
end;
var
Form1:TForm1;
Connection,Mat,Header,Ret,Schedul,Item:Variant;
implementation
{$R*.DFM}
procedureTForm1.Button1Click(Sender:TObject);
begin
(*selectBusinessObject*)
Mat:=SAPBapiControl1.GetSAPObject('BUS2012');
(*definestructures*)
Header:=SAPBapiCcontrol1.dimAs(Mat,'CreateFromData','PoHeader');
Schedul:=SAPBapiCcontrol1.dimAs(Mat,'CreateFromData','PoItemSchedules');
Item:=SAPBapiCcontrol1.dimAs(Mat,'CreateFromData','PoItems');
Ret:=SAPBapiCcontrol1.dimAs(Mat,'CreateFromData','Return');
(*purchaseorderheaderdata'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';
(*dataforposition00010*)
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';
(*schedulesforposition00010*)
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';
(*dataforposition00020*)
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';
(*schedulesforposition00020*)
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';
(*callthemethodCreateFromData*)
Mat.CreateFromData(PoHeader:=Header,
SkipItemsWithError:='',
PoItems:=Item,
PoItemSchedules:=Schedul,
Return:=Ret);
(*ErrorsaresavedinthestructureRet*)
ifRet.RowCount>0then
begin
Panel1.Caption:=Ret.Value(1,'TYPE');
Panel2.Caption:=Ret.Value(1,'MESSAGE');
end
(*Ifthemethodwascalleswithouterrors,*)
(*displaythenumberofthepurchaseorder*)
elsePanel2.Caption:=Mat.PurchaseOrder;
end;
procedureTForm1.Button2Click(Sender:TObject);
begin
(*Logontothesystem*)
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;
ifConnection.LogOn(0,true)=Truethen
begin
ShowMessage('LogonO.K.');
Button1.Enabled:=true;
(*assigntheexistingconnectiontothe*)
(*componentSAPBapiControl1*)
SapBapiControl1.Connection:=Connection;
end
else
begin
ShowMessage('Erroronlogon:-(((');
end;
end;
end.