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

BiancaAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Mahdi78Connect With a Mentor Commented:
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
 
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
 
BiancaAuthor 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
All Courses

From novice to tech pro — start learning today.