When developing database applications using Delphi, if you are using the EditMask property of a TField object to restrict the data that can be entered into a data field, when a user tries to leave a DBEdit component (and the data entered is NOT valid), an "Invalid input value. Use escape key to abandon changes" error will pop up.
Suppose you have a date(time) field in a database table, you drop a TDBEdit component on a form and set the EditMask to allow date value entry. For the sake of simplicity, let's say the mask is "DD/MM/YYYY" (day/month/year). When a user enters a date value in the field he MUST write the entire date, if he/she only provides the day part of a date and tries to leave the edit control the "Invalid input value. Use escape key to abandon changes" will be raised.
It would be much more friendly if we could auto-complete the date value, BEFORE the nasty error.
Unfortunately, handling the DBEdit's OnExit will not help, since the MaskedEdit validation is called before the OnExit event is raised. (Note: the TDBEdit inherits from TCustomMaskEdit).
To "fix" this behavior of the TMaskEdit component, you could create your own component, let's call it "TDBDateEdit", by inheriting from TDBEdit and overriding the ValidateEdit method.
Here's a sample solution:
~~~~~~~~~~~~~~~~~~~~~~~~~
unit DBDateEdit;
interface
uses
DateUtils, MaskUtils, DB, SysUtils, Classes, Controls, StdCtrls, Mask, DBCtrls;
type
TDBDateEdit = class(TDBEdit)
private
procedure DateComplete;
public
procedure ValidateEdit; override;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('delphi.about.com', [TDBDateEdit]) ;
end;
{ TDBDateEdit }
procedure TDBDateEdit.DateComplete;
begin
// implementation
end;
procedure TDBDateEdit.ValidateEdit;
var
pos : integer;
begin
if (IsMasked) then
if (Field.DataType = ftDateTime) OR (Field.DataType = ftDate) then
begin
if not Validate(Text, pos) then
begin
DateComplete;
end;
end;
inherited;
end;
end.
~~~~~~~~~~~~~~~~~~~~~~~~~
Delphi tips navigator:
» Logging Exceptions in Delphi Applicaions
« Moving the focus to the next edit control when MaxLength characters have been reached

