Recent

Author Topic: TSQLQuery with lookup field in TMemDataset  (Read 728 times)

dpap

  • New Member
  • *
  • Posts: 10
TSQLQuery with lookup field in TMemDataset
« on: January 11, 2025, 10:59:18 am »
I have a TSQLquery that has a field that corresponds to a record of a TMemDataset. How can I set a lookup field for this field?

Thaddy

  • Hero Member
  • *****
  • Posts: 16299
  • Censorship about opinions does not belong here.
Re: TSQLQuery with lookup field in TMemDataset
« Reply #1 on: January 11, 2025, 01:42:54 pm »
Don't use TMemDataset, use TBufDataset. That can use lookups easily. TMemDataset has way less feautures than TBufDataset, which can perform the same tasks as a memory only dataset.
If I smell bad code it usually is bad code and that includes my own code.

dpap

  • New Member
  • *
  • Posts: 10
Re: TSQLQuery with lookup field in TMemDataset
« Reply #2 on: January 11, 2025, 05:44:59 pm »
Let have:
Q : table in a SQL database, that keeps the persons data in the fields Name:string(50), Town:integer,....
M : a table that keeps the Towns data in the fields ID:integer, TownName:string(30),....
Now I want to present a person's TownName where Q.Town=M.ID
If both tables were in the same(or not) database, this would be achived with the statement SELECT Q.Name,Q.Town,M.nameOfTown FROM Q LEFT JOIN M ON Q.town=M.ID
but what can I do if the table M is an inmemory table (TBufDataset)?

cdbc

  • Hero Member
  • *****
  • Posts: 1746
    • http://www.cdbc.dk
Re: TSQLQuery with lookup field in TMemDataset
« Reply #3 on: January 11, 2025, 07:26:05 pm »
Hi
Quote
but what can I do if the table M is an inmemory table (TBufDataset)?
I'd use M.Locate(Q.Town..)...
...Time for you to go sniffing in the sources, it's all there  :D
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

dpap

  • New Member
  • *
  • Posts: 10
Re: TSQLQuery with lookup field in TMemDataset
« Reply #4 on: January 11, 2025, 08:42:18 pm »
Finally I make the lookup tables permanent in the database and now I can use the JOIN command. I didn't found other way.

egsuh

  • Hero Member
  • *****
  • Posts: 1519
Re: TSQLQuery with lookup field in TMemDataset
« Reply #5 on: January 12, 2025, 02:55:52 pm »
Simply idea.


1. first open the SQLquery with one additional null field, like

    SELECT Q.Name, Q.Town, null as nameOfTown FROM Q

2. Add event handler to SQLQuery, probably AfterOpen or AfterScroll

      if M.Locate('NameOfTown', [Q.FieldByName('Town').AsInteger]) then begin
         Q.Edit;
         Q.FiledByName('NameOfTown').AsString := Q.FieldByName('NameOfTown').AsString;
         Q.Post;
      end; 

Haven't tested whether this will work.

dpap

  • New Member
  • *
  • Posts: 10
Re: TSQLQuery with lookup field in TMemDataset
« Reply #6 on: January 12, 2025, 06:56:57 pm »
YES! It works  :)With this trick now I can add lookup and calculated fields. Many Thanks!

PS. But I have a major problem as I am newby In lazarus AND SQL coming from Delphi where I handn't such problems. If you please see my post https://forum.lazarus.freepascal.org/index.php/topic,69838.0.html and Give your help!

 

TinyPortal © 2005-2018