[Solved]Dbgrid add new records from query without loss of old data lazarus / 16-Databases ---------------------------------------------------------------------- doniuzas · 2015-02-03 14:23 hello, please help. i have this structure zconnection1->zquery1->datasource1->dbgrid1 How i can add additional data from query on button click without loss old data? I know i need add some like a memodataset or bufdataset or jvmemorydata, but how whole code? my query code on button click is: zquery1.sql.clear; zquery1.Params.CreateParam(ftstring,'someparam',ptunknown); zquery1.params.ParamValues['someparam']:=dbtext1.field.asstring; zquery1.sql.add('select title from table where x= :someparam'); zquery1.active:=true; thank you in advance for your answers. ---------------------------------------------------------------------- doniuzas · 2015-02-03 17:52 no answers? ---------------------------------------------------------------------- fpcoder · 2015-02-03 18:27 doniuzas, the DBGrid doesn't show anything except whats in the query. So you actually never add or delete data directly from a DBGrid. It is just a mirror of the content in its associated SQLQuery. If you want to add content to the DBGrid then you do it indirectly, by modifying the SQL select statement. The data then simply shows up in the grid without you having to do anything. Its not clear to me exactly what you are doing, but perhaps you could have a button that closes the current query, assigns a new select statement and re-opens the query. If you need to keep your position in the grid, then run the Locate command after re-opening the query. If you explain a bit more, maybe we can come up with another answer. Also, you dont need to create the Param if you assign the select statement first: Code: [Select]zquery1.SQL.Text:= 'SELECT title FROM table WHERE x= :someparam' The select statement doesnt need to be cleared when you assign to SQL.Text. And then you can just use the param, it has been created for you automatically: Code: [Select]zquery1.ParamByName('someparam').asstring:= dbtext1.field.asstring; zquery.Open; ---------------------------------------------------------------------- doniuzas · 2015-02-03 18:50 i can make zquery.Open; zquery.close and re-opening the query but i need make like a virtual table or someting for save previously assigned select statment results and show in dbgrid. my schema is: zconn1->zquery1->dbgrid1 dbtext1.field.asstring give me string parameter from dbgrid1 (zquery2.ParamByName('someparam').asstring:= dbtext1.field.asstring;) zconn1->zquery2->dbgrid2 zquery2.SQL.Text:= 'SELECT title FROM table WHERE x= :someparam' i need make someting like this: zconn1->zquery2-> virtual table or virtual dataset(for adding executed zquery2 statments results)->dbgrid2 ---------------------------------------------------------------------- balazsszekely · 2015-02-03 19:56 Still not clear what are you after! Lets say you have a table: POEPLE, with the following fields ID, NAME, AGE(integer, varchar(32), integer) ID     NAME    AGE 1       AAA      25 2       BBB      43 3       CCC     15 4       DDD     64  5       FFF       22 zconn1->zquery1->dbgrid1 Your zquery1 SQL looks like this: select * from PEOPLE where age <= :pAge Code: [Select]//... zquery1.ParamByName('pAge').AsInteger := 25 zquery.Open; //.. Now your grid will be populated with the following values: 1       AAA      25 3       CCC     15 5       FFF       22 Do you wish to add more data to table People, with the possibility to show the current configuration at later time? Please be more specific. Try do describe it with a few example. How table people should look like, what do you want to see in the grid, etc... ---------------------------------------------------------------------- doniuzas · 2015-02-03 20:57 OK with this example I have database with two tables, first is people(names,age), second is details(age, adress). from first query  I get results with age <=25 and populated to dbgrid1, now from dbtext.field.as string (pointed to dbgrid1) I'm get parameter for second query (query2). query2 pointed to dbgrid2. now if I execute query2 with param 25 I'm see in dbgrid2 25 and adress1, I need make second query2 with param 15 but need keep populated info in dbgrid2 (25 and adress1). Need something like virtual table(keep info in memory) between query2 and dbgrid2. Query2 inserting(updating) into virtual table(dataset) and this virtual table is pointed to dbgrid2. But how this make? need full code. may by now its clear? ---------------------------------------------------------------------- doniuzas · 2015-02-03 21:01 ... if its not clear... I have two query components and two dbgrid components dropped in to form... ---------------------------------------------------------------------- mangakissa · 2015-02-05 08:27 I'll try to undersand why you want old data on screen, but if you look at my unit, you can find a way to create a memorytable (TBufdataset). Just rename it to .pas file I think it's better to use a master / detail relation of the two tables. ---------------------------------------------------------------------- doniuzas · 2015-02-05 10:46 why old data on screen need? ok this is application for collecting data from database with different statments and all this "data pack" print as barcode(prices) labels. this apps is for two big supermarkets. ---------------------------------------------------------------------- doniuzas · 2015-02-05 10:56 sorry mayby im noob, but i not understand your unit, for my is too hard ---------------------------------------------------------------------- mangakissa · 2015-02-05 12:17 Wait. Your developping for two big supermarkets and you can't understand my (very) simple class code? This is what you need for memorytable (TBufdataset) Code: [Select]constructor TGlobals.Create; begin   inherited;     ClDataSet := TBufDataset.Create(nil);   with ClDataSet do   begin     FieldDefs.Add('field1',ftString, 50);     FieldDefs.Add('field2',ftString, 50);   end;   ClDataset.CreateDataset;   ClDataSet.Active := true; end; ---------------------------------------------------------------------- doniuzas · 2015-02-05 13:03 im not asking how create bufdataset, i need code sample how from query to bufdataset replace info (insert,copy,append or etc.) ---------------------------------------------------------------------- mangakissa · 2015-02-05 13:47 TBufdataset is not like TClientdataset in Delphi. QuoteI know i need add some like a memodataset or bufdataset or jvmemorydata, but how whole code?Code: [Select]procedure AddSomethingToMemtable(aDataset : TDataset); var index : integer; begin   while not aDataset.eof do   begin       for index := 0 to aDataset.fieldcount - 1 do         Bufdataset[index].Value := aDataset[index].Value;   end; end; Jus behave tbufdataset as a normal TDataset