How to check data in multiple tables on application-startup

Hi there,

This is what I'm trying to do:
I have multiple TADOTables on a DataModule.

During application startup I want to check some of my tables for mandatory data.

Basically (in NON-code) the loop it should follow is something like this:

  begin
     For i := 0 to NumberOfTables-1 do
     begin
        "Check for invalid data in TADOTable[i]";
        "Open form[i] for TADOTable[i]";
        "Correct/add data TADOTable[i]";
        "Save TADOTable[i]";
        "Close Form[i]";
     end;
    "Open Application's Mainform"
  end;

NOTE: Each table uses it's own Form.

Can anybody give me some example project on:
How to check data in multiple tables on application-startup and correct invalid data using multiple forms. And after that the mainform must be displayed.

Thanks for your help,
Stef
Stef MerlijnDeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vadim_tiCommented:
// main unit

procedure ToForm(FormClass: TFormClass; var Reference): Integer;
begin
  try
    Application.CreateForm(FormClass, Reference);
    if TForm(Reference).Tag <> 0 then begin
      TForm(Reference).ShowModal;
    end
  finally
    TForm(Reference).Release;
    Application.ProcessMessages;
  end
end;


procedure TMainForm.FormCreate(Sender: TObject);
begin
   while true do begin
      ToForm(TCheckTable1Form, CheckTable1Form);
      ..........
      ToForm(TCheckTableNForm, CheckTableNForm);
   end
end;

// tableN unit
function TCheckTableNForm.TableNOK: Boolean;
begin
  result := tableisok;
end;

procedure TCheckTableNForm.FormCreate(Sender: TObject);
begin
  if not TableNOK then
    Tag := 1;
end;
0
Stef MerlijnDeveloperAuthor Commented:
This solution looks very nice. But I have to keep two things in mind:
1. All forms are displayed on my main form onto a Parent panel. So the main form
    must be displayed in order to let the program work wel.
    The main form is used as a controle-panel for all other forms (f.e. save-, cancel-,
    add-,  delete- and navigator-buttons are all on the main form).
2. Another thing is, all my forms are created at startup (from within the application unit)
    showing a splashscreen while loading. So my mainform will only be shown when all
    other forms have been created.

=======================================
My procedure to add a form to the panel on the main form is:
=======================================
procedure TFMainform.OpenParentScherm(MyForm : TForm);
begin
  With MyForm do
  begin
    Parent        := FMainform.DetailPanel;
    align          := alClient;
    Borderstyle := bsNone;
    Show;
  end;
end;
============ END ================


So the mainform must be loaded always and after that I can do the checking.
So maybe this checking procedure can be build into an AfterShow-event of the mainform.

The Aftershow event doesn't exist by default so I'll have to create it. (thanks to someone else for this code :-)


==========================
The way I create the aftershow event is:
==========================
unit mainform;

interface

uses
  Windows, Messages, Classes, Menus, Forms, Controls, Dialogs;

const
  WM_AFTER_SHOW = WM_USER + 300;
type
  TMainform = class(TForm)
    procedure FormShow(Sender: TObject);
  private
    procedure WmAfterShow(var Msg: TMessage); message WM_AFTER_SHOW;
  public
  end;

var
  Fmainform: TFmainform;

implementation

{$R *.DFM}

procedure TFmainform.WmAfterShow(var Msg: TMessage);
begin
  ShowMessage('WM_AFTER_SHOW received!');
  ===>>> Do checking here.
end;

procedure TFmainform.FormShow(Sender: TObject);
begin
  // Post the custom message WM_AFTER_SHOW to our form
  PostMessage(Self.Handle, WM_AFTER_SHOW, 0, 0);
end;

end.
============ END ================

Hopefully you have a solution for these two remarks too.

Thanks Stef
0
vadim_tiCommented:
if your CheckTable forms are not autocreated:

// main unit

procedure ToForm(FormClass: TFormClass; var Reference): Integer;
begin
  try
    Application.CreateForm(FormClass, Reference);
    if TForm(Reference).Tag <> 0 then begin
      OpenParentScherm(Reference);
    end
  finally
    TForm(Reference).Release;
    Application.ProcessMessages;
  end
end;


procedure TFmainform.WmAfterShow(var Msg: TMessage);
begin
   while true do begin
      ToForm(TCheckTable1Form, CheckTable1Form);
      ..........
      ToForm(TCheckTableNForm, CheckTableNForm);
   end
end;

// tableN unit
function TCheckTableNForm.TableNOK: Boolean;
begin
  result := tableisok;
end;

procedure TCheckTableNForm.FormCreate(Sender: TObject);
begin
  if not TableNOK then
    Tag := 1;
end;

=============================================================
if your CheckTable forms are autocreated, inherite them from some form (TSomeProtoForm) with protected method
DoTableCheck

// main unit

procedure ToForm(Reference:TSomeProtoForm): Integer;
begin
  try
    if not Reference.DoTableCheck then
      OpenParentScherm(Reference);
  finally
    Reference.Close;
    Application.ProcessMessages;
  end
end;


procedure TFmainform.WmAfterShow(var Msg: TMessage);
begin
   while true do begin
      ToForm(CheckTable1Form as TSomeProtoForm);
      ..........
      ToForm(CheckTableNForm as TSomeProtoForm);
   end
end;

// tableN unit
function TCheckTableNForm.DoTableCheck: Boolean;
begin
  result := tableisok;
end;


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Stef MerlijnDeveloperAuthor Commented:
Can you explain this part?

>> if your CheckTable forms are autocreated, inherite them from some form (TSomeProtoForm)
>> with protected method DoTableCheck

How would the unit of the TSoemProtoForm look like?
I have no idea how to inherit

Thanks Stef
0
vadim_tiCommented:
// SomeProtoUnit.Pas

unit SomeProtoUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TSomeProtoForm = class(TForm)
  private
    { Private declarations }
  protected
    function DoTableCheck: boolean; virtual; abstract;
  public
    { Public declarations }
  end;

var
  SomeProtoForm: TSomeProtoForm;

implementation

{$R *.dfm}

end.

// SomeProtoUnit.Dfm
object SomeProtoForm: TSomeProtoForm
  Left = 192
  Top = 133
  Width = 696
  Height = 480
  Caption = 'SomeProtoForm'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
end

//  CheckTableN.Pas
unit CheckTableN;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Unit1, SomeProtoUnit;

type
  TCheckTableNForm = class(TSomeProtoForm)
  private
    { Private declarations }
  protected
    function DoTableCheck: boolean; override;
  public
    { Public declarations }
  end;

var
  CheckTableNForm: TCheckTableNForm;

implementation

{$R *.dfm}

function TCheckTableNForm.DoTableCheck: boolean;
begin
// here you make your checks
end;

end.

//  CheckTableN.Dfm
inherited CheckTableNForm: TCheckTableNForm
  Width = 596
  Height = 388
  Caption = 'CheckTableNForm'
  PixelsPerInch = 96
  TextHeight = 13
end
0
Stef MerlijnDeveloperAuthor Commented:
When I try to use to first option No autocreate at startup, then
One screen after another is opened and it's very messy.
Somehow it doesn't stop at one Form, so user can enter data end save, before it goes to the next.
0
vadim_tiCommented:
use in your OpenParentScherm
ShowModal instead of Show
0
Stef MerlijnDeveloperAuthor Commented:
That works but then my application freezes.
0
vadim_tiCommented:
post your main form  WmAfterShow procedure,
pseudocode of one of CheckTable form, and OpenParentScherm
0
Stef MerlijnDeveloperAuthor Commented:
How do I do that?
0
vadim_tiCommented:
Copy and paste, no need in table checking functions
0
Stef MerlijnDeveloperAuthor Commented:
I've know idea what went wrong, but after I restored a backup of my sources and applied changes again everything works perfect.
Thank you very much for you solution and patience :-)
You've earned your points big time.

Thanks Stef
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.