How to post a string on the text property of a datetime field?

I have a datetime field that I want to show 'Not Applicable' when the user presses the ESC key. Code is below: I keep exceptioning out when trying to put the string to the text property...

procedure TfrmAccountingDataEntry.edPreBillSentKeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  inherited;
    if Key = VK_ESCAPE then begin
    dmAccounting.qryAccounting.Edit;
    edPreBillSent.DataBinding.Field.Value := StrToDateTime('1/1/1900');
    edPreBillSent.Text := 'Not Applicable';
end;
end;

Open in new window

Norm-alNetwork EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RDWilson2Commented:
What type of field is edPreBillSent?
I am assuming that it is a TEdit field.  In that case, you might want to use the On Key Pressed event instead of On Key Down . . . you may be, in effect, trying to modify something in the moddle of trying to modify it.  In other words, the event hasn't finished firing as you are trying to change the TEdit field's contents.
0
Mahdi78Commented:
try this


private
    { Private declarations }
    procedure MyProcedure(Sender: TField; var Text: string;
      DisplayText: Boolean);
  public
    { Public declarations }
  end;

var
  frmAccountingDataEntry: TfrmAccountingDataEntry;

implementation

{$R *.dfm}

procedure TfrmAccountingDataEntry.MyProcedure(Sender: TField; var Text: string;
  DisplayText: Boolean);
begin
if Sender.AsDateTime = StrToDate('1/1/1900') then
   Text := 'Not Applicable' else Text := Sender.AsString;
end;


procedure TfrmAccountingDataEntry.FormCreate(Sender: TObject);
begin
(Table1.FieldByName('MyField') as TField).OnGetText := MyProcedure;
end;

procedure TfrmAccountingDataEntry.edPreBillSentKeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  inherited;
    if Key = VK_ESCAPE then begin
    dmAccounting.qryAccounting.Edit;
    edPreBillSent.DataBinding.Field.Value := StrToDateTime('1/1/1900');
end;

Open in new window

0
RDWilson2Commented:
Mahdi78,
your code has at least one compile error in it. There is no end associated with the begin of the if statement.

procedure TfrmAccountingDataEntry.edPreBillSentKeyDown(Sender: TObject;
var Key: Word; Shift: TShiftState);
begin
inherited;
if Key = VK_ESCAPE then begin
dmAccounting.qryAccounting.Edit;
edPreBillSent.DataBinding.Field.Value := StrToDateTime('1/1/1900');
end;

0
Mahdi78Commented:
Ok, then will be


private
    { Private declarations }
    procedure MyProcedure(Sender: TField; var Text: string;
      DisplayText: Boolean);
  public
    { Public declarations }
  end;

var
  frmAccountingDataEntry: TfrmAccountingDataEntry;

implementation

{$R *.dfm}

procedure TfrmAccountingDataEntry.MyProcedure(Sender: TField; var Text: string;
  DisplayText: Boolean);
begin
if Sender.AsDateTime = StrToDate('1/1/1900') then
   Text := 'Not Applicable' else Text := Sender.AsString;
end;


procedure TfrmAccountingDataEntry.FormCreate(Sender: TObject);
begin
(Table1.FieldByName('MyField') as TField).OnGetText := MyProcedure;
end;

procedure TfrmAccountingDataEntry.edPreBillSentKeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  inherited;
    if Key = VK_ESCAPE then begin
    dmAccounting.qryAccounting.Edit;
    edPreBillSent.DataBinding.Field.Value := StrToDateTime('1/1/1900');
end;
end;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Norm-alNetwork EngineerAuthor Commented:
The edPreBillSent is a datetime field. I keep getting an exception of incompatible types string and datetime... but I can't even post the text as "Not Applicable"??
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.