I am working on developing an application with a DataModule in Delphi and I’m running into Access Violations when I reference the DataModule from the main form of the application It appears that DataModule1
is nil
which would explain the AV error, but I’m not sure how to solve the problem given everything seems to be defined properly in each of the files.
Here is the main forms code (with AV line marked):
unit mainform;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
Data.DB, Vcl.Grids, Vcl.DBGrids, Vcl.ExtCtrls, Vcl.Buttons, Vcl.DBCtrls,
Vcl.StdCtrls, datamod;
type
TForm1 = class(TForm)
GroupBox1: TGroupBox;
DBNavigator1: TDBNavigator;
DBGrid1: TDBGrid;
procedure Form1Create(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Form1Create(Sender: TObject);
begin
left:=(screen.width-width) div 2;
top:=(screen.height-height) div 2;
with DataModule1.dbConnect do // <---- This line gives the AV
begin
Close;
with Params do
begin
Clear;
Add('DriverID=SQLite');
Add('Database=c:kkastencodedelphitestcasebatool.db');
end;
Open;
end;
DataModule1.FDQuery1.Open;
end;
end.
Here is the data module code:
unit datamod;
interface
uses
System.SysUtils, System.Classes, FireDAC.Stan.Intf, FireDAC.Stan.Option,
FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Def,
FireDAC.Stan.Pool, FireDAC.Stan.Async, FireDAC.Phys, FireDAC.VCLUI.Wait,
FireDAC.Stan.Param, FireDAC.DatS, FireDAC.DApt.Intf, FireDAC.DApt, Data.DB,
FireDAC.Comp.DataSet, FireDAC.Comp.Client, FireDAC.Phys.SQLite,
FireDAC.Phys.SQLiteDef, FireDAC.Stan.ExprFuncs,
FireDAC.Phys.SQLiteWrapper.Stat;
type
TDataModule1 = class(TDataModule)
DataSource1: TDataSource;
dbConnect: TFDConnection;
FDQuery1: TFDQuery;
FDQuery1MRS_Item: TIntegerField;
FDQuery1MRS_Desc: TStringField;
FDQuery1MRS_Amount: TBCDField;
FDQuery1MRS_Actual: TBCDField;
FDQuery1MRS_Adjust: TBCDField;
FDQuery1MRS_NewAmt: TBCDField;
private
{ Private declarations }
public
{ Public declarations }
end;
var
DataModule1: TDataModule1;
implementation
{%CLASSGROUP 'Vcl.Controls.TControl'}
{$R *.dfm}
end.