[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to check data in multiple tables on application-startup

Posted on 2004-11-21
12
Medium Priority
?
167 Views
Last Modified: 2010-04-05
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
0
Comment
Question by:Stef Merlijn
  • 6
  • 6
12 Comments
 
LVL 6

Expert Comment

by:vadim_ti
ID: 12642591
// 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
 

Author Comment

by:Stef Merlijn
ID: 12652314
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
 
LVL 6

Accepted Solution

by:
vadim_ti earned 2000 total points
ID: 12652396
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:Stef Merlijn
ID: 12653640
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
 
LVL 6

Assisted Solution

by:vadim_ti
vadim_ti earned 2000 total points
ID: 12654109
// 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
 

Author Comment

by:Stef Merlijn
ID: 12656427
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
 
LVL 6

Expert Comment

by:vadim_ti
ID: 12656484
use in your OpenParentScherm
ShowModal instead of Show
0
 

Author Comment

by:Stef Merlijn
ID: 12657418
That works but then my application freezes.
0
 
LVL 6

Expert Comment

by:vadim_ti
ID: 12659783
post your main form  WmAfterShow procedure,
pseudocode of one of CheckTable form, and OpenParentScherm
0
 

Author Comment

by:Stef Merlijn
ID: 12660244
How do I do that?
0
 
LVL 6

Expert Comment

by:vadim_ti
ID: 12660264
Copy and paste, no need in table checking functions
0
 

Author Comment

by:Stef Merlijn
ID: 12662955
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question