Recent

Author Topic: TDataset(from DataModule) event from main form unsuccess (SQLite)  (Read 1682 times)

totya

  • Sr. Member
  • ****
  • Posts: 453
Hi!

1. Create new application
2. Create new DataModule
3. Drop one Sqlite3Dataset to DataModule
4. Drop one DataSource to DataModule
5. DataSource.dataset:= Sqlite3Dataset

6. Drop on DBGrid to form1
7. DBGrid.DataSource:=DataModule.DataSource

Now, I want to access the DataModule/Dataset/AfterOpen event from the Form1, so:

8. Click on the DBGrid events Datasource Dataset AfterOpen event
9. Fill this event empty: //

After run, I get exception: Invalid value for property.

What's wrong?

Thank you.

Lazarus 1.6.4 (64 bit)

taazz

  • Hero Member
  • *****
  • Posts: 5362
Re: TDataset(from DataModule) event from main form unsuccess (SQLite)
« Reply #1 on: March 06, 2018, 09:57:31 pm »
I'm guessing the creation order. Form1 can not find and link with the controls in datamodule if the datamodule is not created before the form1 and datamodule can not find and link with the event in form1 during creation before the form1 is created. That is a paradox and can not be resolved automatically you need assign the event manually after creation.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

totya

  • Sr. Member
  • ****
  • Posts: 453
Re: TDataset(from DataModule) event from main form unsuccess (SQLite)
« Reply #2 on: March 06, 2018, 10:14:26 pm »
Hi!

Thank you taazz!

IIRC under Delphi it's working in similar situation, but possible my memory is wrong.

So, how can I assign manually this event after creation?

Thanks!

taazz

  • Hero Member
  • *****
  • Posts: 5362
Re: TDataset(from DataModule) event from main form unsuccess (SQLite)
« Reply #3 on: March 06, 2018, 10:27:31 pm »
you make sure that the datamodule is created before form1 and you write a OnCreate event in form with the code below.
Code: Pascal  [Select]
  1. procedure TForm1.FormCreate(Sender :TObject);
  2. begin
  3.   DataModule1.Sqlite3Dataset1.AfterOpen = @Sqlite3Dataset1AfterOpen;
  4. end;
  5.  
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

totya

  • Sr. Member
  • ****
  • Posts: 453
Re: TDataset(from DataModule) event from main form unsuccess (SQLite)
« Reply #4 on: March 06, 2018, 10:36:29 pm »
Thank you!

Form creation order is changed as you suggest. But then this code:

Code: Pascal  [Select]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, DBGrids, db;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     DBGrid1: TDBGrid;
  16.     procedure FormCreate(Sender: TObject);
  17.   private
  18.     procedure Sqlite3Dataset1AfterOpen(DataSet: TDataSet);
  19.   end;
  20.  
  21. var
  22.   Form1: TForm1;
  23.  
  24. implementation
  25.  
  26. uses Unit2;
  27.  
  28. {$R *.lfm}
  29.  
  30. { TForm1 }
  31.  
  32. procedure TForm1.FormCreate(Sender: TObject);
  33. begin
  34.   DataModule1.Sqlite3Dataset1.AfterOpen = @Sqlite3Dataset1AfterOpen;
  35. end;
  36.  
  37. procedure TForm1.Sqlite3Dataset1AfterOpen(DataSet: TDataSet);
  38. begin
  39.  
  40. end;
  41.  
  42.  
  43. end.
  44.  

...cause compiler error: illegal expression

taazz

  • Hero Member
  • *****
  • Posts: 5362
Re: TDataset(from DataModule) event from main form unsuccess (SQLite)
« Reply #5 on: March 07, 2018, 06:46:10 am »
...cause compiler error: illegal expression
No problems here find attached your example corrected, it compiles properly.
« Last Edit: March 07, 2018, 07:11:55 am by taazz »
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

totya

  • Sr. Member
  • ****
  • Posts: 453
Re: TDataset(from DataModule) event from main form unsuccess (SQLite)
« Reply #6 on: March 07, 2018, 06:13:04 pm »
Thanks, compile OK, but I got exception when project started.
 
Edit: But yes, I see my one error ":=" needed OMG. But your sample halted for me when I run it, see attached picture.

Lazarus 1.6.4 x64
« Last Edit: March 07, 2018, 06:22:51 pm by totya »

taazz

  • Hero Member
  • *****
  • Posts: 5362
Re: TDataset(from DataModule) event from main form unsuccess (SQLite)
« Reply #7 on: March 08, 2018, 05:52:59 am »
Thanks, compile OK, but I got exception when project started.
 
Edit: But yes, I see my one error ":=" needed OMG. But your sample halted for me when I run it, see attached picture.

Lazarus 1.6.4 x64
clear the after post event in the datamodule.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

totya

  • Sr. Member
  • ****
  • Posts: 453
Re: TDataset(from DataModule) event from main form unsuccess (SQLite)
« Reply #8 on: March 08, 2018, 06:46:18 pm »
Thank you taazz!