//* 日期字段校验 *//
procedure MyDateSetText(Sender: TField; const Text: string);
procedure TFrmBsSheet.MyDateSetText(Sender: TField; const Text: string);
var
tmp_Date: TDateTime;
iYear: word;
iMonth: Word;
iDay: Word;
iLen: integer;
tmp_Text: string;
begin
tmp_Text := Trim(Text);
if tmp_Text = '' then
begin
Sender.Value := Null;
Exit;
end;
try
iLen := Length(Trim(tmp_Text));
//格式转换,比如输入0901,090105,20080102
case iLen of
4: tmp_Text := '20' + Copy(Trim(tmp_Text), 1, 2) + '-' + Copy(Trim(tmp_Text), 3, 2) + '-01';
6: tmp_Text := '20' + Copy(Trim(tmp_Text), 1, 2) + '-' + Copy(Trim(tmp_Text), 3, 2) + '-' + Copy(Trim(tmp_Text), 5, 2);
8: tmp_Text := Copy(Trim(tmp_Text), 1, 4) + '-' + Copy(Trim(tmp_Text), 5, 2) + '-' + Copy(Trim(tmp_Text), 7, 2);
end;
tmp_Date := StrToDate(tmp_Text);
DecodeDate(tmp_Date, iYear, iMonth, iDay);
Sender.Value := EncodeDate(iYear, iMonth, iDay);
except
ShowMessage('错误日期');
end;
end;
调用
ADO_Master.FieldByName('日期').OnSetText := MyDateSetText;