Recent

Author Topic: [SOLVED] IBX 2.4.3, How Get Old Value of a field  (Read 4672 times)

incendio

  • Sr. Member
  • ****
  • Posts: 291
[SOLVED] IBX 2.4.3, How Get Old Value of a field
« on: February 22, 2022, 05:17:06 am »
Hi guys,

I am using IBX 2.4.3 on Lazarus 2.0.12 on Windows.

I have a TIBDataset object Dt2 with CachedUpdates Enabled.

I tried to get old value data with this codes
Code: Pascal  [Select][+][-]
  1. procedure TFrm.GrdDtKeyDown(Sender: TObject; var Key: Word;
  2.   Shift: TShiftState);
  3. begin
  4.   if Key = 13 then
  5.   begin
  6.     if GrdDt.SelectedField.Name ='Dt2CD' then
  7.     begin
  8.       GrdDt.SelectedField  := Dt2QTY;
  9.       ShowMessage(Dt2CD.OldValue);
  10.       ShowMessage(Dt2CD.NewValue);
  11.     end;
  12.   end;
  13. end;
  14.  

The value of Dt2CD.OldValue always the same with the value of Dt2CD.NewValue.

Is this normal? If this is normal, how to get the old value of a field?
« Last Edit: February 24, 2022, 04:31:32 am by incendio »

tonyw

  • Sr. Member
  • ****
  • Posts: 325
    • MWA Software
Re: IBX 2.4.3, How Get Old Value of a field
« Reply #1 on: February 22, 2022, 03:45:50 pm »
OldValue = NewValue unless the record is in the edit state and you have modified the field value, in which case, Old Value is the field value prior to the edit and NewValue is what you just set it to.

Cached updates should have no impact on this. Once you have posted a change - cached or otherwise, the oldvalue is no longer accessible. The only exception is if you call CancelUpdates (before ApplyUpdates)  in which case all pending cached updates are reversed.

incendio

  • Sr. Member
  • ****
  • Posts: 291
Re: IBX 2.4.3, How Get Old Value of a field
« Reply #2 on: February 23, 2022, 03:53:24 am »
So OldValue only valid in edit mode.

How about in insert mode, is it possible to catch the old value?

If user input data on the field and edit the data again, before user posted, can we catch the old value?

The sequence could be like these :
No   OldValue   NewValue
==  ======  ======
1      empty      abc
2      abc          cde
3      cde          efg

Zvoni

  • Hero Member
  • *****
  • Posts: 2691
Re: IBX 2.4.3, How Get Old Value of a field
« Reply #3 on: February 23, 2022, 08:00:37 am »
Use a Trigger to write the OldValues in its own table, and process them from there?
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

incendio

  • Sr. Member
  • ****
  • Posts: 291
Re: IBX 2.4.3, How Get Old Value of a field
« Reply #4 on: February 23, 2022, 08:20:50 am »
Using trigger and save old value in its own table require lots of process, I rather use other method if this Old Value not applicable in insert mode.

Zvoni

  • Hero Member
  • *****
  • Posts: 2691
Re: IBX 2.4.3, How Get Old Value of a field
« Reply #5 on: February 23, 2022, 08:29:48 am »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

incendio

  • Sr. Member
  • ****
  • Posts: 291
Re: IBX 2.4.3, How Get Old Value of a field
« Reply #6 on: February 23, 2022, 09:24:40 am »
I am afraid it's not the solution.

I just want to search id from a database based on the code input by users.

For example, after users input a code : abc then pressed ENTER, the app will search the code abc in the database.

Problem is, when users just pressed ENTER without changing the code, app will always search to database.

If app can catch the old value and compare to new value, it doesn't need to search database if no change in the value.

rvk

  • Hero Member
  • *****
  • Posts: 6387
Re: IBX 2.4.3, How Get Old Value of a field
« Reply #7 on: February 23, 2022, 09:44:00 am »
I just want to search id from a database based on the code input by users.
Wait... where does the user enter that code?
Is that a database TDBEdit? In that case you are CHANGING the database. You are not searching for a record.

If you just want the user to search for something you should use a TEdit. And that has nothing to do with a database and there is also no OldValue.

Also... then you search that record and find it and locate it (so the current record is that found record) then when the user presses enter again you can just check if the current record holds that same value as the TEdit value.

Or am I missing something? If so, please explain a bit more about which components you are using and how they interact with the user.

incendio

  • Sr. Member
  • ****
  • Posts: 291
Re: IBX 2.4.3, How Get Old Value of a field
« Reply #8 on: February 23, 2022, 10:01:52 am »
Users input the code in a tdbgrid with has a dataset, but this inputted code is not really change the value of the table, because this code is not saved in the table.

The code is not searched in the same table, but search in different table.

Here are the sample of tables structure :
T_Detail (id int, id_gd int) -> this is the table for grid's dataset.

M_Gd (id int, cd varchar(20)) -> this is the table to search the code inputted by users.


rvk

  • Hero Member
  • *****
  • Posts: 6387
Re: IBX 2.4.3, How Get Old Value of a field
« Reply #9 on: February 23, 2022, 10:24:52 am »
Users input the code in a tdbgrid with has a dataset, but this inputted code is not really change the value of the table, because this code is not saved in the table.
Mmm. Then why is that a dataset if the user-input isn't going to be saved?

Or is the T_Detail a list of possible values in M_Gd and the user SELECTS one of the records in T_Detail???
In that case it's a whole other story.

You have a few possible coices.
1) You can save the value in OnEnter and check it each time. When you do a search you can reset the old value to the just searched text. That would be easiest to implement in your current code.
2) If the T_Detail is a set of possible key values of M_Gd (in that case it's a really strange name because normaly it's the other way around) then you can make a Master-Detail relation with 2 DBGrids. In that case, when you scroll though DB1, the records matching in DB2 will be displayed automatically. Although in that case DB2 will only show that record(s) and not all other records.
3) You can create the master-detail element yourself by using DB1.OnScroll and automatically search/locate the correct record in DB2. In that case the searching is instant (during scrolling of DB1). That could be slow depending on you datasets (so you need to figure out if that's what you want).

But thinking of it... what exactly is the problem at this moment? If you have typed a value in the first grid, and you searched it in the second... you can just check if you are already on that record, can't you?

Code: Pascal  [Select][+][-]
  1. procedure TFrm.GrdDtKeyDown(Sender: TObject; var Key: Word;
  2.   Shift: TShiftState);
  3. begin
  4.   if Key = 13 then
  5.   begin
  6.     if GrdDt.SelectedField.Name ='Dt2CD' then
  7.     begin
  8.       GrdDt.SelectedField  := Dt2QTY;
  9.       if Dt2CD.NewValue <> VALUE_FROM_DB2_RECORD then
  10.       begin
  11.          YOU NEED TO SEARCH
  12.       end;
  13.     end;
  14.   end;
  15. end;


dseligo

  • Hero Member
  • *****
  • Posts: 1371
Re: IBX 2.4.3, How Get Old Value of a field
« Reply #10 on: February 23, 2022, 10:51:12 am »
Users input the code in a tdbgrid with has a dataset, but this inputted code is not really change the value of the table, because this code is not saved in the table.

But if inputted code doesn't change value, then it isn't inserted either thus you can't have old and new values. Even if it would been possible at insert time.

incendio

  • Sr. Member
  • ****
  • Posts: 291
Re: IBX 2.4.3, How Get Old Value of a field
« Reply #11 on: February 23, 2022, 10:58:29 am »
Users input the code in a tdbgrid with has a dataset, but this inputted code is not really change the value of the table, because this code is not saved in the table.
Mmm. Then why is that a dataset if the user-input isn't going to be saved?
Don't need to save the value of the code. Only its id need to be saved.

When cursor is in the first column of the grid, users input the code and pressed ENTER to move cursor to the second column.

After ENTER pressed, app search the value of the code from table M_Gd, if found, app input the id of table M_Gd into field id_gd in table T_Detail.

The problem is, without doing comparison of old value and new value, when cursor on the first column of the grid and users pressed ENTER without changing the value of the code, app will always search the code into table M_Gd.

Of course, it's possible to save the old value of code into different table, but since TIBDataset already have property OldValue, probably I could use it without a need to use different table.

rvk

  • Hero Member
  • *****
  • Posts: 6387
Re: IBX 2.4.3, How Get Old Value of a field
« Reply #12 on: February 23, 2022, 11:58:35 am »
The problem is, without doing comparison of old value and new value, when cursor on the first column of the grid and users pressed ENTER without changing the value of the code, app will always search the code into table M_Gd.
There are multiple ways to detect if it's changed.

But in what kind of field is the user typing? In a cell of a TDBGrid? In that case your dataset will change to State Editing. If the user does not change the code, the State should still be Browsing. If you would save the code then it would all become much easier. Is the user typing in the field for id or is there a separate code column?

It's all a bit confusing. Could you post some screenshots and explain how the user interface works. (I'm starting to think it's all a bit complicated from a users point of view and that there are easier ways to accomplish what you want.)

incendio

  • Sr. Member
  • ****
  • Posts: 291
Re: IBX 2.4.3, How Get Old Value of a field
« Reply #13 on: February 23, 2022, 12:02:21 pm »
I will post a screen shot later, right now I not working on the computer.

tonyw

  • Sr. Member
  • ****
  • Posts: 325
    • MWA Software
Re: IBX 2.4.3, How Get Old Value of a field
« Reply #14 on: February 23, 2022, 12:19:05 pm »
So OldValue only valid in edit mode.

How about in insert mode, is it possible to catch the old value?

If user input data on the field and edit the data again, before user posted, can we catch the old value?

In "insert mode" no. There is no old value to return. Only after you post and then change to the editing state is a potential old value available.

The TField OldValue/Newvalue is really no more than the same functionality that you get with a database update trigger - and probably better done by an update trigger. I guess that it dates back to Delphi 1 and support of simple paradox databases.

 

TinyPortal © 2005-2018