[SOLVED] DataModule messed me up lazarus / 16-Databases ---------------------------------------------------------------------- guest48180 · 2019-07-25 12:35 On a form, I had a TSQLite3Connection, TSQLTransaction, TSQLTransaction, and a TSQLQuery. Life was good. I got it wired up and it was rolling. But then I decided to show off a little. So I added the TSQLite3Connection and the TSQLTransaction to a TDataModule, named DM1. On Unit1, I added Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses  Unit2;Made all the necessary changes to the prior uses of TSQLite3Connection and TSQLite3Connection on Unit1, i.e.: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i DM1.SQLite3Connection1.Connected := True;  DM1.SQLTransaction1.Active := True;But now, when I run it, I get an error: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Project ProjectName raised exception class 'External SIGSEGV'and references this line, which is also the first line in Unit1 that makes reference to my DM1 of Unit2: Code: [Select]DM1.SQLite3Connection1.DatabaseName := 'data.db'; Again, all of this worked until I started using the DataModule. I don't mind putting it all back on one form, but the DataModule seems kind of handy. Here's all the code on Unit1: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, db, sqldb, sqlite3conn, Forms, Controls, Graphics, Dialogs,  DBGrids, DBCtrls; type   { TForm1 }   TForm1 = class(TForm)    DataSource1: TDataSource;    DBGrid1: TDBGrid;    DBNavigator1: TDBNavigator;    SQLQuery1 : TSQLQuery ;    procedure DBNavigator1Click(Sender: TObject; Button: TDBNavButtonType);    procedure FormCreate(Sender: TObject);  private   public   end; var  Form1: TForm1; implementation {$R *.lfm}uses  Unit2; { TForm1 } procedure TForm1.DBNavigator1Click(Sender: TObject; Button: TDBNavButtonType);begin  case Button of  nbPost:    begin      SQLQuery1.ApplyUpdates;      DM1.SQLTransaction1.CommitRetaining;    end;  nbDelete:    begin      SQLQuery1.ApplyUpdates;      DM1.SQLTransaction1.CommitRetaining;    end;   end;end;   procedure TForm1.FormCreate(Sender: TObject);begin  DM1.SQLite3Connection1.DatabaseName := 'data.db';  SQLQuery1.SQL.Text := 'PRAGMA foreign_keys = ON';  SQLQuery1.ExecSQL;  DM1.SQLTransaction1.Commit;   SQLQuery1.SQL.Text :=    'CREATE TABLE IF NOT EXISTS customers(' +    'cust_id INTEGER PRIMARY KEY AUTOINCREMENT, ' +    'cust_name VARCHAR(30) NOT NULL)';  DM1.SQLite3Connection1.Connected := True;  DM1.SQLTransaction1.Active := True;  SQLQuery1.ExecSQL;  DM1.SQLTransaction1.Commit;   SQLQuery1.SQL.Text :=    'CREATE TABLE IF NOT EXISTS sites(' +    'site_id INTEGER PRIMARY KEY AUTOINCREMENT, ' +    'site_name VARCHAR(30) NOT NULL, ' +    'site_cust VARCHAR(30) NOT NULL, ' +    'FOREIGN KEY (site_cust) REFERENCES customers(cust_id) '  +    'ON UPDATE CASCADE ON DELETE CASCADE)';  SQLQuery1.ExecSQL;  DM1.SQLTransaction1.Commit;   SQLQuery1.SQL.Text := 'select * from customers';  SQLQuery1.Open;   DBGrid1.Columns[0].Visible := False;end; end.  What am I doing wrong? ---------------------------------------------------------------------- JanRoza · 2019-07-25 12:55 You're using DM1 regularly but is nowhere declared. Whant's the name of your DataModule unit? Add that name to your uses clause (so where you now have uses unit2, change it to uses unit2, . ---------------------------------------------------------------------- lucamar · 2019-07-25 13:38 Quote from: JanRoza on July 25, 2019, 02:55:08 pmYou're using DM1 regularly but is nowhere declared. Whant's the name of your DataModule unit? From the original post, one may assume that unit2 is the DataModule unit and that DM1 is declared inside? If not it wouldn't compile, would it? There would be lots of "Identifier not found" compile-time errors. The most probable cause of the error is that the form's OnCreate event is being triggered before the data module is (fully) created, so it references objects that don't exist yet. Check the creation order either in the "Project options" windows or directly in the program source (menu "Project->View project source"). Don't forget to check that the data module is on the auto-create list! If not, its objects will never be "alive" ---------------------------------------------------------------------- guest48180 · 2019-07-25 14:24 lucamar: That was it. I moved the code to OnActivate for Form 1, and it runs good now. Thanks for pointing that out to me. That got me rolling again ---------------------------------------------------------------------- lucamar · 2019-07-25 14:52 Glad it helped. I've fallen on that trap quite a few times too