var
  ID:String;
  i:integer;
  Month,Day:string;
begin
 while not datamodule2.tvquery.Eof do
 begin
  ID:=Trim(datamodule2.tvquery.FieldByName('id_card').AsString);
  if (Length(ID)<>15) And (Length(ID)<>18)  then
    begin
       tvquery.Next;
       //Exit;
    end;
 //判断年月日的正确性
  if Length(ID)=15 then
    begin
      Month:=Copy(ID,9,2);
      Day:=Copy(ID,11,2);
    end
  else if Length(ID)=18 then
    begin
      Month:=Copy(ID,11,2);
      Day:=Copy(ID,13,2);
    end;
  if not (((StrToInt(Month))>=01) and ((StrToInt(Month))<=12)) then
    begin
      tvquery.Next;
    end;
  if not (((StrToInt(Day))>=01) and ((StrToInt(Day))<=31)) then
    begin
      tvquery.Next;
    end;
  if (StrToInt(Copy(ID,15,1)) Mod 2)=0 then
  begin
  tvquery.Edit;
  tvquery.FieldByName('sex').AsString:='女';
  tvquery.post;
  tvquery.Next;
  end
  else
  begin
  tvquery.Edit;
  tvquery.FieldByName('sex').AsString:='男';
  tvquery.post;
  tvquery.Next;
  end;
  if (StrToInt(Copy(ID,17,1)) Mod 2)=0 then
  begin
  tvquery.Edit;
  tvquery.FieldByName('sex').AsString:='女';
  tvquery.post;
  tvquery.Next;
  end
  else
  begin
  tvquery.Edit;
  tvquery.FieldByName('sex').AsString:='男';
  tvquery.post;
  tvquery.Next;
  end ;
 end;
我是想执行这段代码先检查身份证号码是否正确,校验位数,然后看日期是否合理,然后提取18位身份证的倒数第二位,或者15位身份证的最后一位看性别,把性别赋值给sex字段.但是我现在这段程序的问题是遇到空值的时候就出错.

解决方案 »

  1.   

    if (Length(ID)<>15) And (Length(ID)<>18)  then
        begin
           tvquery.Next;
           //在此處加入continue即可
           //Exit;
        end;
      

  2.   

    你的代码,错的地方很多  if datamodule2.tvquery.FieldByName('id_card').isNull then
        Continue;  ID:=Trim(datamodule2.tvquery.FieldByName('id_card').AsString);
    然后,if not 的判断,都要加一行:
    tvquery.Next;
    continue; // 加这行后面 if 的那些判断
    都不能有 tvquery.Next;  这行
    在最后一个 end; 前,加 tvquery.Next;
      

  3.   

    你的代码错在,当是15位的时候判断完了,还要继续去if (StrToInt(Copy(ID,17,1)) Mod 2)=0 then判断,当然这里为空了,所以StrToInt就会出错,按照楼上的说法是正确的,加上continue
      

  4.   

    谢谢aiirii,高手真的是位高手.问题解决了.
      

  5.   

    var
      ID:String;
      i:integer;
      Month,Day:string;
    begin
     while not datamodule2.tvquery.Eof do
     begin
      ID:=Trim(datamodule2.tvquery.FieldByName('id_card').AsString);
      if (Length(ID)<>15) And (Length(ID)<>18)  then
        begin
           tvquery.Next;
           //Exit;
    end
    else
    begin
    //判断年月日的正确性
      if Length(ID)=15 then
        begin
          Month:=Copy(ID,9,2);
          Day:=Copy(ID,11,2);
        end
      else if Length(ID)=18 then
        begin
          Month:=Copy(ID,11,2);
          Day:=Copy(ID,13,2);
        end;
      if not (((StrToInt(Month))>=01) and ((StrToInt(Month))<=12)) then
        begin
          tvquery.Next;
    end
    else
    begin
    if not (((StrToInt(Day))>=01) and ((StrToInt(Day))<=31)) then
        begin
          tvquery.Next;
    end
    else
    begin
    if (StrToInt(Copy(ID,15,1)) Mod 2)=0 then
      begin
      tvquery.Edit;
      tvquery.FieldByName('sex').AsString:='女';
      tvquery.post;
      tvquery.Next;
      end
      else
      begin
      tvquery.Edit;
      tvquery.FieldByName('sex').AsString:='男';
      tvquery.post;
      tvquery.Next;
      end;
      if (StrToInt(Copy(ID,17,1)) Mod 2)=0 then
      begin
      tvquery.Edit;
      tvquery.FieldByName('sex').AsString:='女';
      tvquery.post;
      tvquery.Next;
      end
      else
      begin
      tvquery.Edit;
      tvquery.FieldByName('sex').AsString:='男';
      tvquery.post;
      tvquery.Next;
    end ;
    end;
      
    end;
    end;
      
     
     end;
    我是想执行这段代码先检查身份证号码是否正确,校验位数,然后看日期是否合理,然后提取18位身份证的倒数第二位,或者15位身份证的最后一位看性别,把性别赋值给sex字段.但是我现在这段程序的问题是遇到空值的时候就出错.