I thought moving the Data Module to the beginning of the Uses clause in every form would eliminate having the tables Dbf's and DataSources on each form that allows editing of tables, i.e. States, Countries, Categories, etc.
This is what I did so remove the clutter of every form having the Dbf/DS components on every form. I removed them and moved them all to a Data Module. Corrected all the code on forms by adding the Data Module unit at the beginning (Thaddy's advice) USES on every form. The way this app is designed is the opening form is the Clients/Customers. All tables are used on this form, Clients, Contacts, Categories, States, Countries, ReferredBy, etc. After moving to DataModule, the main form, Clients Mgt., no Access Violation errors (Abort or Continue risking data corruption). But, if the user decides to rebuild the tables under File-Maintenance, I can run data tables packing, regenerating indexes, etc. But, when I close, Access Violation and abort. If the user attempts to maintain the Client Categories, same problem when you close the Categories Maint. form, Access Violation and Abort, same with States, Countries, Referrals, etc. I have the Data Module at the beginning of every form's Uses clause. Also, when loading each form from the main, I'm ShowModal.
procedure TFrmPMSMain.MnuFileMaintClick(Sender: TObject);
begin
try
if PMSDM.DbfClients.Modified then
PMSDM.Dbf.ClientsPost;
PMSDM.DbfStates.Close;
PMSDM.DbfCntries.Close;
PMSDM.DbfCategories.Close;
PMSDM.DbfClients.Close;
Application.CreateForm(TFrmRebldPMSDB, FrmRebldPMSDB);
FrmRebldPMSDB.ShowModal;
finally
PMSDM.DbfClients.Open;
PMSDM.DbfStates.Open;
PMSDM.DbfCntries.Open;
PMSDM.DbfCategories.Open;
FrmPMSMain.Refresh;
end;
end;
Another bit of info. In the App's.lpr I also put the DM unit at the beginning of it's Uses clause because that's where the DataModule form is created just before the main form is created.
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
{$IFDEF HASAMIGA}
athreads,
{$ENDIF}
Interfaces, // this includes the LCL widgetset
Forms, datetimectrls, printer4lazarus, lhelpcontrolpkg,
{ Main Unit added below ... }
DmPMS, ClientsMain;
{$R *.res}
begin
RequireDerivedFormResource:=True;
Application.Title:='PMS';
Application.Scaled:=True;
Application.Initialize;
Application.CreateForm(TPMSDM, PMSDm);
Application.CreateForm(TFrmClientsMain, FrmClientsMain);
Application.Run;