4

i would like to be able to ensure that if a table (in this case adotHours) enters Edit or insert mode and the user clicks the save button but accidentally posts the same value under the Hours column as was already entered, a message appears Please enter another value, otherwise if the user enters a different value, another piece of code is used.

I have tried the following method but it doesn't work,- whatever the user enters the message Please enter another value appears.

procedure TfrmLabour.Button6Click(Sender: TObject);
var 
  i,j, t: String;
begin
  Edit1.Text := adotHours['Hours'];
  j :=  Edit1.Text;
  adotHours.Post;
  Edit2.Text := adotHours['Hours'];
  t := Edit2.Text;
  if t = j then 
    showmessage ('Please enter another value')
  else begin.....
end;

while i know it may not be the most elegant code, my thinking was that once the value had been posted to adotHours Hours through the connected DBgrid, if it was a different value to before t would become the new value and therefore adotHours['Hours'] would be different and allow the else begin. Suggestions?

3 Answers 3

7

Try checking the entered value against the OldValue property.

adotHours.FieldByName('Hours').OldValue
1
  • 2
    And check OldValue within beforepost event, much easier and less code than OP with all of the extra assignments etc... Mar 27, 2012 at 22:04
2

adotHours.FieldByName('Hours').NewValue will be Unassigned if the field value is not modified. In that case, OldValue will contain the (unmodified) value.

0

You could use something like this:

if adotHours.FieldByName('ItemName').OldValue = 
  adotHours.FieldByName('ItemName').Value then
  Showmessage(Please enter another value');

But as for me, it would be better to put this code in event adotHours.BeforePost and call the abort procedure if values are the same - in other case, when user changes value and clicks on other row on grid - it cause Post in dataset, and you may miss your check in button click.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.