Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with OpenDialog - raising exception on second call to Execute

Status
Not open for further replies.

pritcham

Programmer
Dec 5, 2005
6
FR
Hi

I've searched the forums here for problems using the OpenDialog control but nothing similar seems to have been posted before.

I've been asked to put together a file import app to get some info imported into a DBISAM DB which I'm progressing with, albeit slowly as I'm new to both Delphi and DBISAM (although not new to programming itself).

I have some code on the OnClick event of a button on my form that opens the OpenDialog dialog so the user can select the file to import - all works fine on the first click but if I click the button a second time it crashes out with an exception error (the error message says its trying to read a portion of memory - I don't have the exact message with me at the moment).

As I'm new to Delphi I just wanted to check I wasn't doing something dumb that's likely to cause this problem - the code for the OnClick is below:

Thanks a lot in advance
Martin

procedure TForm1.Button1Click(Sender: TObject);
begin
With DBISAMTable1 do
Begin
DatabaseName:='Memory';
TableName:='NewTable';
if not exists then
begin
with FieldDefs do
begin
Add('fDate',ftDate,0,True);
Add('fCost_Centre',ftString,50,False);
Add('fVehicleNum',ftString,50,False);
Add('fMileage',ftInteger,0,False);
Add('fCentre',ftString,50,False);
Add('fNotes',ftMemo,0,False);
end;
end;

createtable;
Active:=True;
end;

DBISAMDatabase1.Connected:=True;

{opendialog1.Options:=[ofNoChangeDir]; -- Added this line to see if it solved the problem, it doesn’t}

If opendialog1.execute then
Begin
QImport2XLS1.FileName:=opendialog1.FileName;
qimport2xls1.Execute;
qimport2xls1.Free;
{opendialog1.Files.Clear; -- Added this to see if it solved the problem, nope! }
opendialog1.Free;
end;
end;
 
you're destroying the opendialog1 object with the line
'opendialog1.Free;'
so the second time you click the button, the object isn't there anymore causing an AV. if you dropped the opendialog component onto a form, there's no need to free the object yourself as this is handled by delphi...

so ditch the 'opendialog1.Free;' line to make it work...



-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Hi

To be honest I've only added that line to check whether it solved the problem (which needless to say it didn't) - the app bombs out with or without it. Is there anything obvious I'm doing?

Thanks again
Martin
 
duh,

I see you do the same thing with the qimport2xls1 object...

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Yup - again this was to see whether it was because I wasn't freeing them up that was causing the problem, these calls to .free() were added to try to solve the problem but haven't.

Cheers
Martin
 
Can you post the absolute minimum code that displays the problem that you are getting?

Including statements that you've added that don't solve the problem causes extra work for those of us trying to help.

An additional benefit of minimising the code is that the process sometimes helps you identify the problem and its solution yourself..

It would also help if you use the TGML tags so that code is displayed like this:
Code:
procedure TForm1.Button1Click(Sender: TObject);
begin
With DBISAMTable1 do
Begin

Andrew
Hampshire, UK
 
Thanks Andrew - I'll repost shortly (after I've checked up on the tags!).

Martin
 
As suggested, here's the minimum code that doesn't include any attempts by me to fix the issue:

Code:
procedure TForm1.Button1Click(Sender: TObject);
begin 
With DBISAMTable1 do
Begin 
  DatabaseName:='Memory';
  TableName:='NewTable';
  if not exists then
    begin 
    with FieldDefs do
    	begin 
    	Add('fDate',ftDate,0,True);
    	Add('fCost_Centre',ftString,50,False);
    	Add('fVehicleNum',ftString,50,False);
    	Add('fMileage',ftInteger,0,False);
    	Add('fCentre',ftString,50,False);
    	Add('fNotes',ftMemo,0,False);
    	end; 
  	   end; 

createtable;
Active:=True;
end; 

DBISAMDatabase1.Connected:=True;
 
  If opendialog1.execute then
  Begin 
QImport2XLS1.FileName:=opendialog1.FileName;
qimport2xls1.Execute;
  end; 
end;

The code always throws the exception when the opendialog1.execute method is called for the second time and when the exception is thrown and I go into debug mode, it's that line (opendialog1.execute) that's highlighted. I've also added a breakpoint on the line to see whether the exception is possibly thrown before it gets there but no exception is raised until the second click on the button (and the second call to opendialog1.execute)

Thanks again in advance
Martin
 
and you are sure the opendialog1 component doesnt't get destroyed somewhere else??

try adding a testbutton with only a call to opendialog.execute and see what happens

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
When you say that you click for a second time do mean that the open file has worked once, read the data and then closed the dialog?
I am just trying to get clear what is happening as it should not be possible to click a button when an open dialog is displayed.


[red]GNBM TBA[/red] More on and other neat UK stuff at forum1091
Steve: Delphi a feersum engin indeed.
 
Hi

I think I've narrowed it down - although the exception (seems) to have been raised at the point of clicking the button that runs the opendialog.execute command for the second time, it appears that this isn't the case. I've moved the code that dealt with creation of a DBISAM in memory table out of the OnClick event of the button into the OnCreate event of the form and this exception doesn't occur now (another one does but that's a different story which I'll try to crack myself before posting again).

Many thanks for all of your help in the meantime - it was really appreciated.
Martin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top