Link to home
Start Free TrialLog in
Avatar of cwtang
cwtang

asked on

code causing Einvalid/access violation

Hi,
I seem to keep getting access violation or einvalid point whenever I try to close a form. I have attach the code of the form for reference. The form is used to perform a query on Access Database and it would insert the data in the query into Microsoft Excel. Everything work ok except when trying to close the form which will cause the error. Would be great if anyone could indicate what is causing the porblem.

>>>>>>>>>>>>>

unit MassMail;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons, ExtCtrls,Comobj, Gauges, OleServer,
  Excel97, ComCtrls, ADODB, Db;

type
  TMail = class(TForm)
    ProgressBar1: TProgressBar;
    ADOConnection1: TADOConnection;
    MailQuery: TADOQuery;
    Bevel1: TBevel;
    StaticText1: TStaticText;
    Gauge1: TGauge;
    Label3: TLabel;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormActivate(Sender: TObject);
    procedure FormShow(Sender: TObject);

  private
    { Private declarations }
    iCount, iRowCount, iRecCount : Integer;
    sSQLSTR : String;
    SPORE : String;
    sPathName : String;
    Text : String;
  public
    { Public declarations }
  end;

var
  Mail: TMail;
  MSExcel : variant;


implementation

uses Functs;

{$R *.DFM}

procedure TMail.FormClose(Sender: TObject; var Action: TCloseAction);
begin
   FreeAndNil(Mail);
   Action := CAFree;

end;

procedure TMail.FormActivate(Sender: TObject);
begin
String:='abctext ';
Text :='Loading...';
       iRowCount := 4;

        with MailQuery do
        begin

        Close;  SQL.Clear;

                sSQLSTR := 'Select AField,BField,CField,DField,EField,FField,GField,HField from Table1';

                SQL.Add(sSQLSTR);
                open;

     if EOF then
     begin
       MessageDlg('No Data To Process .', mtInformation, [mbOk], 0);
       Abort;  Exit;
     end

     else
     begin
       iCount := MailQuery.RecordCount;
       ProgressBar1.Position := 0;
       ProgressBar1.Min := 0;
       ProgressBar1.Max := iCount;
       iRecCount := 0;
 
       while not EOF do
        begin

         Next;
         Inc(iRecCount);
         ProgressBar1.StepBy(1);
         StaticText1.Caption := IntToStr(iRecCount);
         Mail.Repaint ;

        end;
        MailQuery.First;

        MSExcel := CreateOleObject('Excel.Application');
        MSExcel.Visible := False;
        MSExcel.Workbooks.Open('C:\Excel.xls', ReadOnly:=False);
        MSExcel.WindowState := 2;

        StaticText1.Caption :=Text;
        StaticText1.Repaint ;
        Progressbar1.Hide ;
        Gauge1.Show;
        Gauge1.MaxValue :=iRecCount;

        while not EOF do
        begin


       MSEXCEL.WorkSheets[1].Range['A' + IntToStr(iRowCount)].Value := FieldByName('AField').AsString +FieldByName('BField').AsString + FieldByName('CField').AsString + FieldByName('DField').AsString;

       MSEXCEL.WorkSheets[1].Range['B' + IntToStr(iRowCount)].Value := FieldByName('EField').AsString;
      MSEXCEL.WorkSheets[1].Range['C' + IntToStr(iRowCount)].Value := FieldByName('FField').AsString;
       MSEXCEL.WorkSheets[1].Range['D' + IntToStr(iRowCount)].Value := FieldByName('GField').AsString;
       MSEXCEL.WorkSheets[1].Range['E' + IntToStr(iRowCount)].Value := String1 + FieldByName('HField').AsString;
       Next;
       Gauge1.AddProgress(1);
       Inc(iRowCount);
       StaticText1.Repaint ;
        end;
               end;
       end;

       StaticText1.Caption :='Done';
       StaticText1.Repaint ;
       MSExcel.Visible := true;
       MSExcel.WindowState := 1;
       MSExcel.WorkSheets[1].PrintOut(Preview:=True);
       StaticText1.Caption :='Done';
       StaticText1.Repaint ;


end;

procedure TMail.FormShow(Sender: TObject);
begin
Gauge1.Hide ;
Progressbar1.show;
end;

end.


<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


Any help is appreciated. Thanks.
Avatar of cwtang
cwtang

ASKER

I forgot to add that the compiler being used is Delphi 5. Any help is appreciated.

Thanks
Avatar of cwtang

ASKER

Realsie there was some typing error on previous code, I have updated the code.

>>>>>>>>>>>>>

unit MassMail;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons, ExtCtrls,Comobj, Gauges, OleServer,
  Excel97, ComCtrls, ADODB, Db;

type
  TMail = class(TForm)
    ProgressBar1: TProgressBar;
    ADOConnection1: TADOConnection;
    MailQuery: TADOQuery;
    Bevel1: TBevel;
    StaticText1: TStaticText;
    Gauge1: TGauge;
    Label3: TLabel;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormActivate(Sender: TObject);
    procedure FormShow(Sender: TObject);

  private
    { Private declarations }
    iCount, iRowCount, iRecCount : Integer;
    sSQLSTR : String;
    String1 : String;
    sPathName : String;
    Text : String;
  public
    { Public declarations }
  end;

var
  Mail: TMail;
  MSExcel : variant;


implementation

uses Functs;

{$R *.DFM}

procedure TMail.FormClose(Sender: TObject; var Action: TCloseAction);
begin
   FreeAndNil(Mail);
   Action := CAFree;

end;

procedure TMail.FormActivate(Sender: TObject);
begin
String1:='abctext ';
Text :='Loading...';
       iRowCount := 4;

        with MailQuery do
        begin

        Close;  SQL.Clear;

                sSQLSTR := 'Select AField,BField,CField,DField,EField,FField,GField,HField from Table1';

                SQL.Add(sSQLSTR);
                open;

     if EOF then
     begin
       MessageDlg('No Data To Process .', mtInformation, [mbOk], 0);
       Abort;  Exit;
     end

     else
     begin
       iCount := MailQuery.RecordCount;
       ProgressBar1.Position := 0;
       ProgressBar1.Min := 0;
       ProgressBar1.Max := iCount;
       iRecCount := 0;
 
       while not EOF do
        begin

         Next;
         Inc(iRecCount);
         ProgressBar1.StepBy(1);
         StaticText1.Caption := IntToStr(iRecCount);
         Mail.Repaint ;

        end;
        MailQuery.First;

        MSExcel := CreateOleObject('Excel.Application');
        MSExcel.Visible := False;
        MSExcel.Workbooks.Open('C:\Excel.xls', ReadOnly:=False);
        MSExcel.WindowState := 2;

        StaticText1.Caption :=Text;
        StaticText1.Repaint ;
        Progressbar1.Hide ;
        Gauge1.Show;
        Gauge1.MaxValue :=iRecCount;

        while not EOF do
        begin


       MSEXCEL.WorkSheets[1].Range['A' + IntToStr(iRowCount)].Value := FieldByName('AField').AsString +FieldByName('BField').AsString + FieldByName('CField').AsString + FieldByName('DField').AsString;

       MSEXCEL.WorkSheets[1].Range['B' + IntToStr(iRowCount)].Value := FieldByName('EField').AsString;
      MSEXCEL.WorkSheets[1].Range['C' + IntToStr(iRowCount)].Value := FieldByName('FField').AsString;
       MSEXCEL.WorkSheets[1].Range['D' + IntToStr(iRowCount)].Value := FieldByName('GField').AsString;
       MSEXCEL.WorkSheets[1].Range['E' + IntToStr(iRowCount)].Value := String1 + FieldByName('HField').AsString;
       Next;
       Gauge1.AddProgress(1);
       Inc(iRowCount);
       StaticText1.Repaint ;
        end;
               end;
       end;

       StaticText1.Caption :='Done';
       StaticText1.Repaint ;
       MSExcel.Visible := true;
       MSExcel.WindowState := 1;
       MSExcel.WorkSheets[1].PrintOut(Preview:=True);
       StaticText1.Caption :='Done';
       StaticText1.Repaint ;


end;

procedure TMail.FormShow(Sender: TObject);
begin
Gauge1.Hide ;
Progressbar1.show;
end;

end.


<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Shouldnt need this

procedure TMail.FormClose(Sender: TObject; var Action: TCloseAction);
begin
   FreeAndNil(Mail);
   Action := CAFree;
end;
SOLUTION
Avatar of robert_marquardt
robert_marquardt

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cwtang

ASKER

Sorry for the late reply. Was away.

Thanks to everyone. Got it solved:)