unit U_PublicFunction;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ToolWin, StdCtrls, BtnEdit, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit,DBGrids, DB, cxDBData,
  cxGridLevel, cxClasses, cxControls, cxGridCustomView, ADODB,StrUtils,
  Midas,cxGridCustomTableView, cxGridTableView, cxGridDBTableView,cxTimeEdit,
    cxGrid,cxDBLookupComboBox,cxCalendar, cxCurrencyEdit,cxGridExportLink,
  ExtCtrls, Buttons,DBClient,FTComboBox,cxDropDownEdit,CXmemo;

  function GetMaxBh(ADOQueryTmp:TADOQuery;
                  var mMaxBh:string;
                  mBhType:string;
                  mFlag:string;
                  mStatus:string;
                  lsLen:integer=3):Boolean;
   procedure CreateAdoToCds(Ado1:TADOQuery; Cds1:TclientDataSet );
   procedure InitAdoToCds(Ado1:TADOQuery;Cds1:TclientDataSet);
implementation

function GetMaxBh(ADOQueryTmp:TADOQuery;
                  var mMaxBh:string;
                  mBhType:string;
                  mFlag:string;
                  mStatus:string;
                  lsLen:integer=3):Boolean;
begin
  try
    with ADOQueryTmp do
    begin
      Close;
      sql.Clear ;
      sql.Add(' exec Get_SY_MaxBH ');
      sql.Add(' @BHstr='+quotedstr(mFlag));
      sql.Add(',@BHType='+quotedStr(mBhType));
      sql.Add(',@Len='+inttostr(LsLen));
      sql.Add(',@status='+quotedStr(mStatus));
      Open;

      if RecordCount>0 then
      begin
        mMaxBh:=trim(fieldByName('MaxBH').AsString) ;
        if mMaxBh<>'' then
          result:=true
        else
          Result:=false;
      end
      else
      begin
        result:=false;
      end;

    end;
    if not Result then
      application.MessageBox(Pchar('无法生成自动编号('+mflag+')'),'提示信息',MB_ICONINFORMATION);

  Except
    result:=false;
    application.MessageBox(Pchar('无法生成自动编号('+mflag+')'),'提示信息',MB_ICONINFORMATION);
  end;
end;

procedure CreateAdoToCds(Ado1:TADOQuery; Cds1:TclientDataSet );
var
  i:integer;
  mfieldName:string;
  mSize:integer;
begin
  mfieldName:='';
  Cds1.FieldDefs.Clear;
  with Ado1 do
  begin
    for i:=0 to fieldCount-1 do     //
    begin
      if (Fields[i].DataType=ftString) and (Fields[i].Size=0) then
      begin
        msize:=1;
      end
      else
        msize:=Fields[i].Size;
        mfieldName:=trim(fields[i].FieldName);
        Cds1.FieldDefs.Add(mfieldName,Fields[i].DataType,msize);
    end;
  end;
  Cds1.FieldDefs.Add('flag',ftString,1);
  Cds1.FieldDefs.Add('index',ftInteger,0);
  Cds1.Close;
  Cds1.CreateDataSet;
end;
procedure InitAdoToCds(Ado1:TADOQuery;Cds1:TclientDataSet);
var
  i:integer;
  k:integer;
begin
  Ado1.DisableControls;
  Cds1.DisableControls;
  Ado1.first;
  K:=1;
  while not Ado1.Eof do
  begin
    with Cds1 do
    begin
      Append;
      for i:=0 to Ado1.FieldCount-1 do
      begin
        fields[i].value:=Ado1.Fields[i].Value ;
      end;
      fieldByName('flag').AsString :='1';
      fieldByName('index').value :=k;
      inc(k);
      Post;
    end;
    Ado1.Next;
  end;
  if not Cds1.IsEmpty then
  begin
    Cds1.First ;
  end;
  Ado1.EnableControls;
  Cds1.EnableControls;
end;

end.
