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,DFiel d,EField,F Field,GFie ld,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.App lication') ;
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].Rang e['A' + IntToStr(iRowCount)].Value := FieldByName('AField').AsSt ring +FieldByName('BField').AsS tring + FieldByName('CField').AsSt ring + FieldByName('DField').AsSt ring;
MSEXCEL.WorkSheets[1].Rang e['B' + IntToStr(iRowCount)].Value := FieldByName('EField').AsSt ring;
MSEXCEL.WorkSheets[1].Rang e['C' + IntToStr(iRowCount)].Value := FieldByName('FField').AsSt ring;
MSEXCEL.WorkSheets[1].Rang e['D' + IntToStr(iRowCount)].Value := FieldByName('GField').AsSt ring;
MSEXCEL.WorkSheets[1].Rang e['E' + IntToStr(iRowCount)].Value := String1 + FieldByName('HField').AsSt ring;
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].Prin tOut(Previ ew:=True);
StaticText1.Caption :='Done';
StaticText1.Repaint ;
end;
procedure TMail.FormShow(Sender: TObject);
begin
Gauge1.Hide ;
Progressbar1.show;
end;
end.
<<<<<<<<<<<<<<<<<<<<<<<<<< <<<<
Any help is appreciated. Thanks.
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:
begin
String:='abctext ';
Text :='Loading...';
iRowCount := 4;
with MailQuery do
begin
Close; SQL.Clear;
sSQLSTR := 'Select AField,BField,CField,DFiel
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.App
MSExcel.Visible := False;
MSExcel.Workbooks.Open('C:
MSExcel.WindowState := 2;
StaticText1.Caption :=Text;
StaticText1.Repaint ;
Progressbar1.Hide ;
Gauge1.Show;
Gauge1.MaxValue :=iRecCount;
while not EOF do
begin
MSEXCEL.WorkSheets[1].Rang
MSEXCEL.WorkSheets[1].Rang
MSEXCEL.WorkSheets[1].Rang
MSEXCEL.WorkSheets[1].Rang
MSEXCEL.WorkSheets[1].Rang
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].Prin
StaticText1.Caption :='Done';
StaticText1.Repaint ;
end;
procedure TMail.FormShow(Sender: TObject);
begin
Gauge1.Hide ;
Progressbar1.show;
end;
end.
<<<<<<<<<<<<<<<<<<<<<<<<<<
Any help is appreciated. Thanks.
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,DFiel d,EField,F Field,GFie ld,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.App lication') ;
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].Rang e['A' + IntToStr(iRowCount)].Value := FieldByName('AField').AsSt ring +FieldByName('BField').AsS tring + FieldByName('CField').AsSt ring + FieldByName('DField').AsSt ring;
MSEXCEL.WorkSheets[1].Rang e['B' + IntToStr(iRowCount)].Value := FieldByName('EField').AsSt ring;
MSEXCEL.WorkSheets[1].Rang e['C' + IntToStr(iRowCount)].Value := FieldByName('FField').AsSt ring;
MSEXCEL.WorkSheets[1].Rang e['D' + IntToStr(iRowCount)].Value := FieldByName('GField').AsSt ring;
MSEXCEL.WorkSheets[1].Rang e['E' + IntToStr(iRowCount)].Value := String1 + FieldByName('HField').AsSt ring;
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].Prin tOut(Previ ew:=True);
StaticText1.Caption :='Done';
StaticText1.Repaint ;
end;
procedure TMail.FormShow(Sender: TObject);
begin
Gauge1.Hide ;
Progressbar1.show;
end;
end.
<<<<<<<<<<<<<<<<<<<<<<<<<< <<<<
>>>>>>>>>>>>>
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:
begin
String1:='abctext ';
Text :='Loading...';
iRowCount := 4;
with MailQuery do
begin
Close; SQL.Clear;
sSQLSTR := 'Select AField,BField,CField,DFiel
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.App
MSExcel.Visible := False;
MSExcel.Workbooks.Open('C:
MSExcel.WindowState := 2;
StaticText1.Caption :=Text;
StaticText1.Repaint ;
Progressbar1.Hide ;
Gauge1.Show;
Gauge1.MaxValue :=iRecCount;
while not EOF do
begin
MSEXCEL.WorkSheets[1].Rang
MSEXCEL.WorkSheets[1].Rang
MSEXCEL.WorkSheets[1].Rang
MSEXCEL.WorkSheets[1].Rang
MSEXCEL.WorkSheets[1].Rang
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].Prin
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;
procedure TMail.FormClose(Sender: TObject; var Action: TCloseAction);
begin
FreeAndNil(Mail);
Action := CAFree;
end;
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sorry for the late reply. Was away.
Thanks to everyone. Got it solved:)
Thanks to everyone. Got it solved:)
ASKER
Thanks