Recent

Author Topic: Move DBGrid.InplaceEditor.EditText contents to a SQLQuery field.  (Read 5077 times)

iru

  • Sr. Member
  • ****
  • Posts: 321
Gentlefolk,

I asked this question a very long time ago and received an answer. Somehow in moving things from system to system the answer has been lost.

In a DBGrid I accumulate data in a cell and then on the <enter> I need to check the input for non numeric data, syntax, etc and convert the input into a standard format, for instance 41523 has to be converted to 4:15.23.

I can do all this by accessing the data in

  procedure TFM.DBGrid1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
    TStringCellEditor(tDBGrid(Sender).InplaceEditor).EditText

The question is how do I write the contents of EditText to a field in the current record of the underlying SQLQuery?

From long memory it was something in the area of procedure

   TFM.SQLQueryHeatBeforeInsert(DataSet: TDataSet)

But all efforts around that part of the code have resulted in the original non parsed input and/or the parsed version in the next record of the SQLQuery.

An help appreciated, Ian.

LacaK

  • Hero Member
  • *****
  • Posts: 691
Re: Move DBGrid.InplaceEditor.EditText contents to a SQLQuery field.
« Reply #1 on: March 30, 2015, 08:16:57 pm »
I would use event of TField: http://www.freepascal.org/docs-html/fcl/db/tfield.onsettext.html
http://www.freepascal.org/docs-html/fcl/db/tfieldsettextevent.html
It is called also when DB-aware control is writing data to their related field.
So you can catch "aText" parse it and write to Sender.AsString what you want.

iru

  • Sr. Member
  • ****
  • Posts: 321
Re: Move DBGrid.InplaceEditor.EditText contents to a SQLQuery field.
« Reply #2 on: March 31, 2015, 12:11:11 pm »
Lacak,

Thank you for your response, from reading your  prose and the links I understand what needs to be done.

The trouble is that  I am struggling to implement a solution.

I have implemented a tField event handler like "procedure SQLQueryHeatFieldsEvent(Sender: TField; const AText: String);"
And I would presume that I would need to point the fields.fields[3].OnSetEvent to something like @SQLQueryHeatFieldsEvent;

Nothing even compiles clean so I am obviously on multiple wrong tracks. Do you (or anone) have a small example?

Thanks, Ian

LacaK

  • Hero Member
  • *****
  • Posts: 691
Re: Move DBGrid.InplaceEditor.EditText contents to a SQLQuery field.
« Reply #3 on: March 31, 2015, 09:31:32 pm »
For example I have in datamodule in class definition in private section:
  procedure QueryField1SetText(Sender: TField; const Text: String);

Then in another place (in Query.AfterOpen) in same datamodule:
  DataSet.FieldByName('Field1').OnSetText := @QueryField1SetText;

Then:
  procedure TDM.QueryField1SetText(Sender: TField; const Text: String);
  var s: string;
  begin
    // do something with Text and f.e. store result in local variable s, then
    Sender.AsString:=s;
  end;

iru

  • Sr. Member
  • ****
  • Posts: 321
Re: Move DBGrid.InplaceEditor.EditText contents to a SQLQuery field.
« Reply #4 on: April 01, 2015, 12:00:52 am »
Lacak,

Thank you for the example. It is implemented and test data is written to the dataset.

The changes that this code makes to the program flow is interesting....

In my original code

  I use procedure TFM.DBGrid1KeyUp to process each keyboard input character.
  When an <enter> is input this is detected and the previous digits are parsed, modified, etc.

With the  TFM.QueryFieldPerfSetText(Sender: TField; const AText: String) implemented the program flow changes to

  On each digit input TFM.DBGrid1KeyUp is invoked.
  On the <enter> TFM.QueryFieldPerfSetText is invoked.
  TFM.DBGrid1KeyUp is NOT invoked.

Therefore I have to re-think lots of logic and code......

Thanks once again, Ian.

BTW: I have been involved in communications and computers for many years but am still amazed by the Internet and its uses.
I am in Melbourne Australia and put up the request for an example before I went to bed last night. Up early this morning and there is your response from Slovakia, simply marvellous.

iru

  • Sr. Member
  • ****
  • Posts: 321
Re: Move DBGrid.InplaceEditor.EditText contents to a SQLQuery field.
« Reply #5 on: April 01, 2015, 03:22:28 am »
Lacak and all,

I have had a look at the data input process and the new logic flow created by the use of the field based "TDM.QueryField1SetText(Sender: TField; const Text: String);"

The original data input process is as follows:

  Input some numeric digits.
  On the <enter> check for any errors.
  If no errors reformat the data and write it to the dataset. Move the Dataset to the next entry (and the DBGrid to the next row).
  If errors put up a status message and maintain the Dataset and the DBGrid in the same entry/row so the user can correct the error.

The use of the field with the <enter> passing control to "TDM.QueryField1SetText(Sender: TField; const Text: String" is very late in the process and I appear to have lost control of positioning the entry/row in case of an error.

Any ideas?  Ian.

iru

  • Sr. Member
  • ****
  • Posts: 321
Re: Move DBGrid.InplaceEditor.EditText contents to a SQLQuery field.
« Reply #6 on: April 01, 2015, 08:09:57 am »
Lacak, et al,

Some more research shows that the DBGrid events OnKeyDown and OnKeyPress fire on any data character as it is input.

On an <enter> OnKeyDown  fires just before "TFM.QueryFieldPerfSetText(Sender: TField; const AText: String);" .

So it looks like I may be able to control the checking and correction of errors in a cell on DBGrid before things are written to the underlying dataset.

Ian.

LacaK

  • Hero Member
  • *****
  • Posts: 691
Re: Move DBGrid.InplaceEditor.EditText contents to a SQLQuery field.
« Reply #7 on: April 01, 2015, 11:47:55 am »
Some more research shows that the DBGrid events OnKeyDown and OnKeyPress fire on any data character as it is input.

On an <enter> OnKeyDown  fires just before "TFM.QueryFieldPerfSetText(Sender: TField; const AText: String);" .

Yes it is expected.

So it looks like I may be able to control the checking and correction of errors in a cell on DBGrid before things are written to the underlying dataset.

TDataSet and TDBGrid are two independent components. TDBGrid (or other data-aware component like TDBEdit) "sends" updates to TDataSet. So it is up to TDBGrid when posts updates to underlying TDataSet / TField.
As you explore, when key is pressed and cursor/focus moves from one cell to another DBGrid posts updates ... an OnKeyUp is fired after this.

iru

  • Sr. Member
  • ****
  • Posts: 321
Re: Move DBGrid.InplaceEditor.EditText contents to a SQLQuery field.
« Reply #8 on: April 01, 2015, 09:53:49 pm »
Lacak et al,

Looked at my code and cut the parsing/formatting code from the DBGrid.OnKeyUp event and pasted it to the DBGrid.OnKeyDown.

Some basic testing show that:
   The parsed/formatted good text is presented in the display in the correct cell.
   If there are no errors, the 'active' cell' of the DBGridis on the next row.
   If there are errors the 'active' cell is the same cell as the data was entered.

Data is not saved to the underlying FB database, I think I know why.

Looking good, a lot more testing required....

Thanks once again. Ian.

Thanks

 

TinyPortal © 2005-2018