Standard Dataset Actions lazarus / 31-LCL ---------------------------------------------------------------------- Walmir · 2014-07-02 13:47 Hi, I am in trouble with standard dataset actions. In a main form, I have an ACtionList with standard dataset actions. The database components (ZConnection, ZQuery, DataSource, ...) are in a data module. The data aware controls are in a form that is put in a TDI component. The actions are linked to main menu items and toobuttons in a toolbar but don't work. Even when I set the actions DataSource directly in the Object Inspector they don't work. A DBNavigator in the same form as the data aware components works fine. In utter dispair I put the data aware controls directly in the main form and  -- tada! -- the actions still don't work. What am I mising? Best regards. Walmir. Lazarus 1.2.2, FPC 2.6.4. ---------------------------------------------------------------------- Walmir · 2014-07-11 18:59 Even if I put the ActionList with the standard database actions in the same form as the data aware controls and set each DataSource property of the actions to the same Data Source of the data aware controls, the standard DB actions don't work. But a DBNavigator in the same form works correctly... Is it a bug? Best regards, Walmir. ---------------------------------------------------------------------- howardpc · 2014-07-11 19:51 One way to hook this up is to create an OnExecute event for each standard database action you have added (by double-clicking on the event in the Object Inspector) and (assuming your navigator control is named DBNav) set the event code like this: Code: [Select]procedure TForm1.acPostExecute(Sender: TObject); begin   DBNav.BtnClick(nbPost); end; for a Post action, use nbNext for a Next action etc. You don't need to set any Datasource property if the navigator is already working OK. ---------------------------------------------------------------------- taazz · 2014-07-11 19:59 Quote from: howardpc on July 11, 2014, 09:51:16 pmOne way to hook this up is to create an OnExecute event for each standard database action you have added well that would work for every action there is no need to add a tdatasetaction. If the dataset action is not executed when the datasource is set then the dataset actions have a bug that needs to be reported. I'll try to test it later this week my self to see how it works if it works that is. ---------------------------------------------------------------------- rvk · 2014-07-11 23:20 Quote from: taazz on July 11, 2014, 09:59:14 pmIf the dataset action is not executed when the datasource is set then the dataset actions have a bug that needs to be reported. I'll try to test it later this week my self to see how it works if it works that is. I just answered Walmir in another post about this: See here http://forum.lazarus.freepascal.org/index.php/topic,25140.msg 152333.html#msg152333 It only seems to occur when the data controls are in a data module and the data aware controls are on another form. Then the default ExecuteTarget of the DataSetActions don't work. The weird part is that i only had to add one TDataSource to the Form pointing to the dataset in the data module for it to work again. I don't have to connect the datasetaction to this datasource. (They can stay connected to the datasource in the datamodule.) I'm also very new so i'm not yet familiar with the bugtracker... (but will be looking at that shortly) so if you could look at this bug for him... (In the meantime i advised him to set an extra TDataSource on his form to get the ExecuteTarget and thus also the shortcuts to work) ( I tested this on Win7 64bit / Laz 1.2.4 win32 / FPC 2.6.4 ) ---------------------------------------------------------------------- taazz · 2014-07-12 02:06 Quote from: rvk on July 12, 2014, 01:20:02 amQuote from: taazz on July 11, 2014, 09:59:14 pmIf the dataset action is not executed when the datasource is set then the dataset actions have a bug that needs to be reported. I'll try to test it later this week my self to see how it works if it works that is. I just answered Walmir in another post about this: See here http://forum.lazarus.freepascal.org/index.php/topic,25140.msg 152333.html#msg152333 It only seems to occur when the data controls are in a data module and the data aware controls are on another form. Then the default ExecuteTarget of the DataSetActions don't work. The weird part is that i only had to add one TDataSource to the Form pointing to the dataset in the data module for it to work again. I don't have to connect the datasetaction to this datasource. (They can stay connected to the datasource in the datamodule.) I'm also very new so i'm not yet familiar with the bugtracker... (but will be looking at that shortly) so if you could look at this bug for him... (In the meantime i advised him to set an extra TDataSource on his form to get the ExecuteTarget and thus also the shortcuts to work) ( I tested this on Win7 64bit / Laz 1.2.4 win32 / FPC 2.6.4 ) If that is the case I do need to test anything it is by design. You can't have actions not connected to a datasource selecting the active datasource by them self's you should write an onActivate event to set the action's datasource, keep in mind that the form might be modal mdi or inside a page control so it makes it impossible for the action to auto select the correct datasource and if you move the datasource to a datamodule instead of the form it makes it even more difficult to know exactly which datasource to select. In any case I would appreciate a test application that shows the problem, before I start looking. ---------------------------------------------------------------------- rvk · 2014-07-12 08:01 Quote from: taazz on July 12, 2014, 04:06:21 amIn any case I would appreciate a test application that shows the problem, before I start looking. Argh... there are two topics at this moment with the same (base) issue. I posted some sample-code (and explanation) here: http://forum.lazarus.freepascal.org/index.php/topic,25140.msg152354.ht ml#msg152354 So the issue seems to be already in bugtracker but isn't assigned and is still open. http://bugs.freepascal.org/view.php?id=25441 (and maybe this http://bugs.freepascal.org/view.php?id=25988) ---------------------------------------------------------------------- percentage · 2014-07-12 17:23 Could this be related to my problem here? http://forum.lazarus.freepascal.org/index.php/topic,24138.msg144920.ht m ---------------------------------------------------------------------- Walmir · 2014-07-15 15:48 Even when a place a TDataSource in the same form as the data aware controls, the standard database actions still don't work. In summary, what I have: - the data componentes in a data module; - the action list with the standar DB actions in the main form; - the data aware controls in another form that is opend inside a TDI Component Although I think this is the "natural" way of doing things, the standar DB actions apparently are not prepared to work in this way. Conclusion: I can't expect to do in Lazarus the same as I did in Delphi with DB actions and, so, they are useless. ---------------------------------------------------------------------- BigChimp · 2014-07-15 15:52 Walmir, if you feel this is a bug and if you have a test project that demonstrates the problem (including any needed database table definitions/DDL) could you please raise an item in the bugtracker so this can get looked at and hopefully addressed by the Lazarus developers? Thanks. ---------------------------------------------------------------------- rvk · 2014-07-15 16:55 Walmir, did you put a TDataSource (pointing to your DataSet in the DataModule) on your main form? The TDataSetActions in your TActionList need a TDataSource on the same form as the TActionList itself, otherwise they don't work. BigChimp, this issue is already in the bugtracker here (#0025441). I added an extra note with sample code there. I'm not sure of the status but Luigi also posted this (@0025988) there. Not sure if that's the (correct) fix. (It is strange the TDataSource doesn't even need to be connected to the TDatasetAction's. It just needs to be on the form.) ---------------------------------------------------------------------- Walmir · 2014-07-16 13:49 What I observed is that not only the TDataSource must be on the same form as the actionlist, but the controls that will be linked to the actions (menu items, buttons) must also be in that form. So itf I put the TDataSource and the action list in a data module, the database actions will not work. If this is so by design, it must be well documented. Conclusion: In order to do the standard actions work, all the following must be in the same form: - the action list with the standard database actions; - the TDataSource componentes; - the controls that will be linked to the actions. ---------------------------------------------------------------------- Walmir · 2014-07-16 16:09 Another gotcha! I tried to place a TDataSource in the same form as the database actions and set its DataSet property at run time, when the form with the data aware controls are activated. This don't work, too! The DataSet property of the TDataSource must be set at design time! What a buggy thing! ---------------------------------------------------------------------- taazz · 2014-07-16 18:11 Ooops. Sorry guys forgot all about this problem. I have downloaded the supplied samples and I'm going to take a closer look asap (if not tonight tomorrow night for sure). sorry again. ---------------------------------------------------------------------- taazz · 2014-07-17 08:44 I took a good look yesterday it seems that the problem is on the TDataSetAction object specifically on the function HandlesTarget which is implemented like Code: [Select]function TDataSetAction.HandlesTarget(Target: TObject): Boolean; begin   Result:=(DataSource<>Nil);   if Result and (DataSource=Target) then     Result:=(DataSource.DataSet<>Nil)   else     Result:=(Target is TDataSource) and (TDataSource(Target).DataSet<>Nil); end; As you can see on the first if the implementor makes a hard coded that the active control that is passed from the suite to the action must be the same as the linked in datasource. That overrides any design time link and makes it impossible to have the datasource anywhere else but the form. This needs to be changed to something along the lines of Code: [Select]function TDataSetAction.HandlesTarget(Target: TObject): Boolean; begin   Result:=(DataSource<>Nil) and (DataSource.DataSet<>Nil);   if not Result  then     Result:=(Target is TDataSource) and (TDataSource(Target).DataSet<>Nil); end; short circuit boolean evaluation must be on for the above code to work properly you can always make a multi if statement if you want. That will solve the main problem but it will introduce a new bug coming from the GetDataSet function which needs to be changed from Code: [Select]function TDataSetAction.GetDataSet(Target: TObject): TDataSet; begin   Result:=(Target as TDataSource).DataSet; end; to Code: [Select]function TDataSetAction.GetDataSet(Target: TObject): TDataSet; begin   if assigned(FDataSource) then     Result := FDataSource.dataset   else     Result:=(Target as TDataSource).DataSet; end; that should take care most of the problems. Please keep in mind that the above have not been tested and there might be more problems introduced that I haven't seen yet. In any case this should put you back on track for now. I'll try to create a custom set of dataset actions to test the changes and report back. I haven't looked at the patch supplied on the bug tracker so I can't be sure what it changes and if it will do the job yet and I must find the bug reports to add a comment there too. ---------------------------------------------------------------------- taazz · 2014-07-18 14:20 Quote from: Walmir on July 18, 2014, 03:41:16 pmHi, The modifications in DBActns had no effect. So, I will use normal actions, with an OnExecute firing the respective buttons of a DBNavigator placed in the main form. This is the only way to offer the user a GUI, a menu that can be read by a screen reader and a set of shortcuts to work with the databse. I find almost impossible that it had no effect. It is more likely that the modified dbActns did not linked in and the old one was used. I haven't found the time to test them my self so far because to test them properly I need to create a 3rd party package with new actions different names build it and install it on the IDE etc etc, to avoid linking problems. In any case I'll do something along those lines and get back to you. ---------------------------------------------------------------------- rvk · 2014-07-18 14:36 Quote from: Walmir on July 18, 2014, 03:41:16 pmThe modifications in DBActns had no effect. Did you recompile the LCL ? ( I think it's in Tools / Configure "Rebuild Lazarus". I'm not sure if there is an easier way. Also newby here ) It did the trick for me and it works now. (Not sure yet what negative effects this might have, if there are any) Quote from: taazz on July 17, 2014, 10:44:28 amI haven't looked at the patch supplied on the bug tracker so I can't be sure what it changes and if it will do the job yet and I must find the bug reports to add a comment there too. I think the patch Luigi did is not the preferred one, as far as i can see. (He doesn't even mention anything in the bug-report about what the patch actually does) Maybe it works but it uses the Data Aware controls in dbctrls.pp to update the actions (which is wrong, i think, because there might not even be any data aware controls on your form and then the actions should still work). (Luigi's original bugreport #25441) (What i believe is his suggested solution #25988, but can't be sure because of missing information) ---------------------------------------------------------------------- Walmir · 2014-07-18 19:36 I finally discovered why it was not working here after DBActns modification. The cuprit is TDINoteBook. When I open a form inside it, the standard DB actions stop working and I can't even set  the DataSource of a DBNavigator in the main form at run time. When I open the form outside the TDINoteBook, all is correct. ---------------------------------------------------------------------- Walmir · 2014-07-21 16:52 Another discovery... The TDINoteBook behavior is not unique. I tried opening a form inside a TabSheet of a PageControl and the problem is the same. Only when I open the form wit a simple Show the database standard actions work corrrectly. I also noted that I cannot change in code the DataSource of a DBNavigator placed in the main form from a secondary formwhen this form is inside a TabSheet. So, the Tabbed Document Interfaceof my application will not work with the standar DB actionhs and the alternative - using common actions with onExecute events that fire the DBNavigator respective BtnClick - also don't work because I can't change the DBNavigator data source at run time. What can I do? ---------------------------------------------------------------------- taazz · 2014-07-21 16:58 I think that you are hitting the following bug. http://bugs.freepascal.org/view.php?id=24793 ---------------------------------------------------------------------- taazz · 2014-07-24 18:01 If you download the code from the bug report I have posted you will find out that by re linking through code at runtime the actions work properly it probably is only a problem of streaming. ---------------------------------------------------------------------- rvk · 2014-07-25 14:21 Quote from: Walmir on July 25, 2014, 03:42:57 pmIf I open the form inside a TabSheet, the actions don't work. As I also mentioned, this behavior is shared by a DBNavigator placed in the main form - and I can't modify its DataSource through code. For me the Datasetactions work even when i have absolutely no data- aware components in my whole project. (So they don't seem to be depended on any data-aware components, as it should be) Could you provide a simple sample-project where you show exactly what going wrong? ---------------------------------------------------------------------- taazz · 2014-07-25 14:36 Quote from: Walmir on July 25, 2014, 03:42:57 pmQuote from: taazz on July 24, 2014, 08:01:12 pmIf you download the code from the bug report I have posted you will find out that by re linking through code at runtime the actions work properly it probably is only a problem of streaming. If you are referring to the modifications in DBActns the bug report at http://bugs.freepascal.org/view.php?id=24793 has some code attached it shows that if you re link the actions at runtime they work as expected. ---------------------------------------------------------------------- Walmir · 2014-08-07 12:27 Quote from: Walmir on July 31, 2014, 07:51:19 pm,.. I got the time to test with SQLdb and found that the standard DB actions don't work too with it. But with MemDataSet all is ok. Again, I have to say that  in this manner, standard DB actions are useless in a real world application, with the actions in the main form, the data controls in a data module and the data aware controls in secondary forms opened in tabsheets. Can anyone confirm his behavior? ---------------------------------------------------------------------- rvk · 2014-08-07 14:50 Quote from: Walmir on July 31, 2014, 07:51:19 pm... with the actions in the main form, the data controls in a data module and the data aware controls in secondary forms opened in tabsheets. With the changes from this post they work perfectly here with the situation you described (even with a form opened in a TTabSheet of a TPageControl). If it's not working for you the only thing i could suggest is making a small sample-app (where your problem presents itself) and posting that project here. ---------------------------------------------------------------------- rvk · 2014-08-08 23:32 For the problem with the dbnavigator i answered here: http://forum.lazarus.freepascal.org/index.php/topic,25370.msg154465.ht ml#msg154465 For the problem with you datasetaction.... Where do you assign the datasource to the datasetactions?? Shouldn't you also do this in all the FormActivate's? Code: [Select]  frmMain.DataSetNext1.DataSource:=dmData.dsCli;   frmMain.DataSetCancel1.DataSource:=dmData.dsCli;   frmMain.DataSetDelete1.DataSource:=dmData.dsCli;   frmMain.DataSetEdit1.DataSource:=dmData.dsCli;   frmMain.DataSetFirst1.DataSource:=dmData.dsCli;   frmMain.DataSetInsert1.DataSource:=dmData.dsCli;   frmMain.DataSetLast1.DataSource:=dmData.dsCli;   frmMain.DataSetNext1.DataSource:=dmData.dsCli;   frmMain.DataSetPost1.DataSource:=dmData.dsCli;   frmMain.DataSetPrior1.DataSource:=dmData.dsCli; B.T.W. it would be better to loop through all the action from the TActionList and check if it is inherited from TDataSetAction. If it is then assign the datasource. Like this (in all your forms): Code: [Select]procedure TfrmCadCli.FormActivate(Sender: TObject); var   i: Integer; begin   frmMain.dbnNav.DataSource:=dmData.dsCli;   for i := 0 to frmMain.aclActions.ActionCount - 1 do   begin     if frmMain.aclActions.Actions[i].InheritsFrom(TDataSetAction) then       TDataSetAction(frmMain.aclActions.Actions[i]).DataSource:=dmData .dsCli;   end; end; Yet another way would be to do it in the mainform in the onpagechange- event (i showed in the other topic for the OnActivate). You can loop there through all the actions and set the datasource to the same as the dbnavigator: Code: [Select]procedure TfrmMain.tdiAbasChange(Sender: TObject); var   i: Integer; begin   if tdiAbas.ActivePage is TTDIPage then     if Assigned(TTDIPage(tdiAbas.ActivePage).FormInPage) then       if Assigned(TTDIPage(tdiAbas.ActivePage).FormInPage.OnActivate) then       begin         TTDIPage(tdiAbas.ActivePage).FormInPage.OnActivate(nil);         for i := 0 to frmMain.aclActions.ActionCount - 1 do         begin           if frmMain.aclActions.Actions[i].InheritsFrom(TDataSetAction) then             TDataSetAction(frmMain.aclActions.Actions[i]).DataSource:= dbnNav.DataSource;         end;       end; end; Choose one of these last two methods. ---------------------------------------------------------------------- Walmir · 2014-08-09 16:20 Rvk, In Delphi I did never set the DataSource property of the DB standard actions and all worked fine... In some way it autommatically detected the DataSource property of the controls in tthe MDI child forms. So, by what you are showing me, this is not considered to be a bug in Lazarus. Thank you for the help. ---------------------------------------------------------------------- rvk · 2014-08-09 16:30 Quote from: Walmir on August 09, 2014, 06:20:56 pmIn Delphi I did never set the DataSource property of the DB standard actions and all worked fine... In some way it autommatically detected the DataSource property of the controls in tthe MDI child forms. I'm not sure if the DatasetAction automatically takes the datasource in Delphi. But you have many Datasources so i can't imagine that in this case you wouldn't have to do anything in Delphi. What datasource would Delphi connect the Datasetaction to? Did it automatically switch when choosing another active form? Maybe this is the behavior if there is only one datasource... but with multiple datasources i almost can't believe Delphi would do this automatically. But at least the way i showed you, you're sure the correct datasource is set for the actions. ---------------------------------------------------------------------- taazz · 2014-08-09 16:41 Quote from: rvk on August 09, 2014, 06:30:24 pmI'm not sure if the DatasetAction automatically takes the datasource in Delphi. But you have many Datasources so i can't imagine that in this case you wouldn't have to do anything in Delphi. What datasource would Delphi connect the Datasetaction to? Did it automatically switch when choosing another active form? Maybe this is the behavior if there is only one datasource... but with multiple datasources i almost can't believe Delphi would do this automatically. But at least the way i showed you, you're sure the correct datasource is set for the actions. I'm assuming that in delphi the focused data aware control is used to determine which datasource is used. Thinking it this way it seems possible to accomplish and a pain in the behind to implement since there is no interface that all the data aware controls support that can answer the datasource question and there is no single component in the hierarchy that implements datasource support that can be used to abstract the access. I say "I assume" because I can't remember by heard and I don't have access to delphi at this time to check. ---------------------------------------------------------------------- Walmir · 2014-08-11 17:35 I'm having these "problems" with DB standard actions exacttly because I assumed Lazarus will have the same behavior as Delphi. The application I am porting use this feature extensively and nowhere I needed to set the DataSource of the DB actions. I don't know how Delphi did this "magic", but I can assure you it worked and it is one that I'll really miss in Lazarus. ---------------------------------------------------------------------- rvk · 2014-08-11 18:44 Quote from: Walmir on August 11, 2014, 07:35:46 pmI don't know how Delphi did this "magic", but I can assure you it worked and it is one that I'll really miss in Lazarus. taazz already explained how Delphi could do (or does) this. You could try to mimic this behavior yourself by adding some code (like Delphi does) to detect the datasource of the current control. You could use an event (OnActiveControlChange for example, if Lazarus has this one) and check if the ActiveControl has a property "DataSource". If it does you could set the DataSource of all your TDataSetAction on your form to that same DataSource (it would be just a few lines of code in the OnActiveControlChange-event). All this is assuming you have an activecontrol with a datasource. What if the activecontrol does not have a datasource? How does Delphi handle that? If there are multiple datasources on a form and your control is on a non-data-aware control.... what datasource does Delphi take then? (so many possibilities...) Otherwise you could use the method i showed you in the last procedure of my post. You could even create a generic procedure to use in different programs. (For the program you showed it was just a few lines needed to be added) It can't be helped that Lazarus/FPC sometimes behaves slightly differently (it's not a direct clone). Sometimes these differences are deliberate (to not blindly follow Delphi in bad design/programming) and sometimes it is unintentional (or just a bug). In case it is unintentional (or a bug) you could try to code new features or patch Lazarus/FPC and see if these changes are of benefit to others. If others find the code useful it can be added to the next release. In this case you stumbled on a (rarely used) "missing feature". You could try adding an entry in the bugtracker to see if it is deemed important enough (for others) to fix (or attach the fix yourself). ---------------------------------------------------------------------- taazz · 2014-08-13 05:11 I took a closer look on the delphi action implementation and as far as I can see the magic is in the TDBEdit.ExecuteAction and TDBEdit.UpdateAction (and other data aware controls) which has been extended to call the FDatalink.ExecuteAction as well. I do not know how I fill about it. It gives the most control over to the control designer and at the same time it forces code duplication on all the data aware controls. In the bright side it is easy to correct and the TDatalink seems to already have the action methods implemented.