我的问题是这样的:
合同有不同的类别,每一个合同类别都有唯一的简称:
 合同类别         简称
 工程设计          建
 货物运输          运
 供用电            电我生成的合同格式为:东煤(Year)(简称)合字第(编号)号,例如东煤(2008)建合字第001号。
当合同类别选定后,要求合同编号子动生成。
假如合同类别为工程设计,而且库里面没有这个类别合同,则合同编号为:东煤(2008)建合字第001号。
如果选择了工程设计,库里面已经有了1份这个类别合同,则合同编号为:东煤(2008)建合字第002号。说说我的库,合同编号为一个字段,编号我也建立了一个字段。这个该怎么解决。

解决方案 »

  1.   

    function GetNo(strType: string): string;
    const
      CompanyName = '东煤';
    var
      strYear, strTmp: string;
    begin  
      Result := '';
      strYear = FormatDateTime('yyyy', Date);
      with TADODataSet.Create(nil) do
      begin
        Connection := YourADOConnection;
        CommandText := 'select 简称 from 合同类型表 where 合同类别 = ' + QuotedStr(strType);
        Active := True;
        if RecordCount = 0 then 
        begin
          //这是谁啊,乱传参数!!!
           Free;
          Exit;
        end;
        strTmp := Fields[0].Value;
        strTmp := CompanyName + '(' + strYear + ')' + strTmp + '合字第';    Active := False;
        CommandText := 'select Count(*) from 合同表 where 合同号 like ' + QuotedStr(strTmp + '%');
        Active := True;
        
        Result := Fields[0].AsString;
        Result := StringOfChar('0', 3-Length(Result)) + Result;
        Result := strTmp + Result + '号';
        Free;
      end;
    end;
      

  2.   

    上面的不对,忘了 + 1 了function GetNo(strType: string): string; 
    const 
      CompanyName = '东煤'; 
    var 
      strYear, strTmp: string; 
    begin  
      Result := ''; 
      strYear = FormatDateTime('yyyy', Date); 
      with TADODataSet.Create(nil) do 
      begin 
        Connection := YourADOConnection; 
        CommandText := 'select 简称 from 合同类型表 where 合同类别 = ' + QuotedStr(strType); 
        Active := True; 
        if RecordCount = 0 then 
        begin 
          //这是谁啊,乱传参数!!! 
          Free; 
          Exit; 
        end; 
        strTmp := Fields[0].Value; 
        strTmp := CompanyName + '(' + strYear + ')' + strTmp + '合字第';     Active := False; 
        CommandText := 'select Count(*) from 合同表 where 合同号 like ' + QuotedStr(strTmp + '%'); 
        Active := True; 
        
        Result := Fields[0].AsString; 
        Result := StringOfChar('0', 3-Length(Result)) + IntToStr(StrToInt(Result) + 1); 
        Result := strTmp + Result + '号'; 
        Free; 
      end; 
    end;
      

  3.   

    给你的建议:库中使用一个整数字段(N),记录当前存在的最大编号,这个整数初始值是N:=0,每创建一个合同时,它的编号就取N+1,同时也更新库中N的值为N+1,这样就很轻松了嘛.
      

  4.   

    我的代码:
    procedure TOtherForm.frDBEdit2Exit(Sender: TObject);
     //取得当前编号(sNO)的最大值并且加1
     function getMaxNO(smaxNO: String): String;
     var
      i,m: integer; 
     begin
      if smaxNO='' then 
      begin 
        smaxNO := '001';
        smaxNO := IntToStr(StrToInt(smaxNO)); 
      end else 
      begin 
        smaxNo := IntTostr(StrToInt(smaxNO)+1); 
      end;   for i:= 1 to 3-Length(smaxNO) do 
      begin 
        insert('0',smaxNO,i); 
      end; 
      Result:=smaxNO;
     end;
    var
     vDataSet,vvDataSet:TDataSet;
     vYear,vNO:Variant;
    begin
      inherited;
      vYear:=GetYear(frDate());
      if DB.SqlValueFmt('if exists(select Ht_NO from Other where Ht_NO like ''%s'') select 1 else   select 0',['%'+MDataSetSName.Value+'%'])=0 then  begin
       MDataSetsNO.Value:=getMaxNO('');
      end else begin
       vNO:=DB.SqlValueFmt('select top 1 sno from Other where Ht_NO like ''%s'' order by Ht_ID DESC',['%'+MDataSetSName.Value+'%']);
       MDataSetsNO.Value:=getMaxNO(vNO);
      end;
      MDataSetHt_NO.Value:=Format('东煤(%s)%s合字第%s号',[vYear,MDataSetSName.Value,MDataSetsNO.Value]);
    end;