Sorry, I didn't understand, actually! I'm not an English speaker and my English is terrible...
I have good experience in C++ Builder but I abandoned it because the licenses are overpriced and I very recently turned to Lazarus although I know nothing about Pascal.
But I think I understand the problem: it's a variable scope problem.
It's going to take up a lot of space but here is my code (unfortunately with variable names in French)
Master unit:
unit Maitre;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ActnList, Menus, Unit1,UIdentification;
type
{ TForm1 }
TForm1 = class(TForm)
MenuGen: TMainMenu;
MenuItem1: TMenuItem;
MenuItem10: TMenuItem;
MenuItem11: TMenuItem;
MenuItem12: TMenuItem;
MenuItem13: TMenuItem;
MenuItem14: TMenuItem;
MenuItem15: TMenuItem;
MenuItem16: TMenuItem;
MenuItem17: TMenuItem;
MenuItem18: TMenuItem;
MenuItem19: TMenuItem;
MenuItem2: TMenuItem;
MenuItem3: TMenuItem;
MenuItem4: TMenuItem;
MenuItem5: TMenuItem;
MenuItem6: TMenuItem;
MenuItem7: TMenuItem;
MenuItem8: TMenuItem;
MenuItem9: TMenuItem;
Retour: TMenuItem;
procedure FormActivate(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure MenuItem10Click(Sender: TObject);
procedure MenuItem9Click(Sender: TObject);
private
public
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
begin
end;
procedure TForm1.FormActivate(Sender: TObject);
begin
FormIdentif.Show;
end;
procedure TForm1.MenuItem10Click(Sender: TObject);
begin
Application.MainForm.Close;
end;
procedure TForm1.MenuItem9Click(Sender: TObject);
begin
FormAPropos.Show;
end;
end.
uoutils (uTools) where global variables are defined
unit uoutils;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils;
var
IpServeur :String;
NomBase :String;
iniPath :String;
implementation
end.
Unit uidentification where variables are assigned from the contents of the ini file
unit uIdentification;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, Forms, IniFiles, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls,uoutils;
type
{ TFormIdentif }
TFormIdentif = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
SaisUser: TEdit;
SaisMdp: TEdit;
Edit3: TEdit;
Edit4: TEdit;
GroupBox1: TGroupBox;
GroupBox2: TGroupBox;
GroupBox3: TGroupBox;
GroupBox4: TGroupBox;
GroupBox5: TGroupBox;
Label1: TLabel;
Label2: TLabel;
Panel1: TPanel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
function iniRead(s : string) : string;
private
public
end;
const
Section = 'Serveur';
var
FormIdentif: TFormIdentif;
INI :TINIFile;
Ok : Boolean = false;
implementation
{$R *.lfm}
{ TFormIdentif }
procedure TFormIdentif.Button2Click(Sender: TObject);
begin
Application.MainForm.Close;
end;
procedure TFormIdentif.FormCreate(Sender: TObject);
var
callResult : String;
begin
iniPath := Application.Location;
callResult := iniRead(iniPath);
end;
function TFormIdentif.iniRead(s : string) : string;
var
str: string;
begin
INI :=TINIFile.Create('Luges.ini');
try
IpServeur :=INI.ReadString(Section,'IP','');
NomBase :=INI.ReadString(Section,'Base','');
finally
INI.Free;
str := 'Process Worked';
Result := str;
end;
end;
procedure TFormIdentif.Button1Click(Sender: TObject);
begin
if (SaisUser.Text='') and (SaisMdp.Text='') then
begin
Application.MessageBox('Veuillez saisir votre nom d utilisateur et votre mot de passe!', 'Alerte Utilisateur');
end
else
begin
if SaisUser.Text='' then
begin
Application.MessageBox('Veuillez saisir votre nom d utilisateur', 'Alerte Utilisateur');
end;
if SaisMdp.Text='' then
begin
Application.MessageBox('Veuillez saisir votre mot de passe!', 'Alerte Utilisateur');
end;
end
end;
end.
unit1 (unit About...) where certain variables (IpServeur) are displayed after assigning the contents of the ini file
unit Unit1;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls, uoutils;
type
{ TFormAPropos }
TFormAPropos = class(TForm)
ShowPath: TEdit;
GroupBox5: TGroupBox;
ShowIpServ: TEdit;
ShowNomBase: TEdit;
GroupBox2: TGroupBox;
Base: TGroupBox;
Panel4: TPanel;
ShowDateCompil: TEdit;
GroupBox1: TGroupBox;
GroupBox3: TGroupBox;
GroupBox4: TGroupBox;
Label4: TLabel;
Label6: TLabel;
Label7: TLabel;
Label8: TLabel;
Label9: TLabel;
Panel1: TPanel;
Panel2: TPanel;
Panel3: TPanel;
procedure FormActivate(Sender: TObject);
private
public
end;
var
FormAPropos: TFormAPropos;
implementation
{$R *.lfm}
{ TFormAPropos }
procedure TFormAPropos.FormActivate(Sender: TObject);
begin
ShowDateCompil.Text :=DateTimeToStr(FileDateToDateTime(FileAge('Luges4.exe')));
ShowIpServ.Text :=IpServeur;
ShowNomBase.Text :=NomBase;
ShowPath.Text := iniPath;
end;
end.
Unit DataAccess where the connection to the database is made
unit DataAccess;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, mysql80conn, SQLDB, Dialogs,db,uoutils;
type
{ TDataModule1 }
TDataModule1 = class(TDataModule)
SQLConnection1: TMySQL80Connection;
SQLTransaction: TSQLTransaction;
procedure DataModuleCreate(Sender: TObject);
private
public
function Login: Boolean;
procedure Logoff;
end;
var
DataModule1: TDataModule1;
implementation
{$R *.lfm}
{ TDataModule1 }
procedure TDataModule1.DataModuleCreate(Sender: TObject);
begin
If not Login then
begin
Login;
end;
end;
function TDataModule1.Login: Boolean;
begin
Result := true;
//IpServeur:='10.0.0.7';
ShowMessage('IP: ' + IpServeur);
SQLConnection1.Hostname := IpServeur;
SQLConnection1.DatabaseName := '######';
SQLConnection1.UserName := '######';
SQLConnection1.Password := '######';
try
SQLConnection1.Connected := true;
SQLTransaction.Active := true;
except
on e : ESQLDatabaseError do
begin
MessageDlg('Erreur de connexion à la base :'#10#10#13 + IntToStr(e.ErrorCode) + ' : ' + e.Message + #10#10#13'Application terminée.',mtError,[mbOK],0);
Result := false;;
end;
on e : EDatabaseError do
begin
MessageDlg('Erreur de connexion à la base :'#10#10#13'Application terminée.',mtError,[mbOK],0);
Result := false;;
end;
end;
if Result = true then
MessageDlg('Base Luges disponible!',mtError,[mbOK],0);
end;
procedure TDataModule1.Logoff;
begin
if SQLTransaction.Active then
SQLTransaction.Active := false;
if SQLConnection1.Connected then
SQLConnection1.Connected := false;
end;
end.
The problem is that the global variables assigned to the contents of the ini file do not pass their contents to DataAccess, although they pass them to Unit1.Why ?