Opening datasource on 1 form in another raises error

0

I have a delphi project with multiple forms in it. On one of these forms I have a dataset which contains fields, I would like to open this dataset on another form, however I get the following error message which I've not seen before :

Project raised exception class $C0000005 with message access violation at 0x005f536f:read of address 0x000000e8.

Am I doing something wrong or going about this the wrong way?

Any help would be much appreciated.

Thanks,

Code:

uses frm1

procedure Tfrm2.FormCreate(Sender: TObject); 
begin
  frm1.ds.DataSet.Open;
end;

Code for button click on frm1 to launch frm2

procedure Tfrm1.btnCreateClick(Sender: TObject);
var Form2 : tfrm2;    
begin
  Form2 := tfrm2.Create(self);
  Form2.Editing := False;
  try
    Form2.ShowModal
  finally
    Form2.Free;
  end;
end;
delphi
delphi-xe2
asked on Stack Overflow Jul 17, 2015 by Sharpie • edited Jul 17, 2015 by Sharpie

3 Answers

1

Your code sample demonstrate very bad practice:

Unit1 using Unit2

Unit2 using Unit1

Create DataModule unit and use it from Unit1 and Unit2

Will be like this:

Unit1 using Unit2 and DataModule unit

Unit2 using DataModule unit

Anyway, your code should work. Reason why you get AccessViolation -- perhaps you have some events for TADOQuery/TADOTable and/or TDAtaSource which are not listed in your code sample

answered on Stack Overflow Jul 17, 2015 by Zam • edited Jul 17, 2015 by Zam
0

Problably code is imcomplete but the problem is that you have not created an instance of form1. So frm1 is not available. So this code will avoid Access Violation.

uses frm1

procedure Tfrm2.FormCreate(Sender: TObject); 
begin
  frm1 := TForm1.Create(Application);
  frm1.ds.DataSet.Open;
end;
answered on Stack Overflow Jul 17, 2015 by Leo Melo
0

First of all, I would not recommend using data access components individually in each form, rather I would put them all in a Data Module. That way all forms of your application can have access to your data access components and also your application will look clean and organized. About the error, it is important to mention that form1(with dataset) should call form2 through its Interface uses, and Form2(the one to call form1 dataset) should call form1 through its Implementation uses. I believe it will solve your problem. Let me know whether it worked. J

answered on Stack Overflow Jul 17, 2015 by Jay

User contributions licensed under CC BY-SA 3.0