所有的分求助一个解决方案!!!!(有关ADOQUery控件和报表) (40分)

  • 主题发起人 主题发起人 luckyon
  • 开始时间 开始时间
L

luckyon

Unregistered / Unconfirmed
GUEST, unregistred user!
用另个Form将Form1的查询结果显示预览到Form2报表上,但是运行的时候无法通过,显示如下
错误:Why?/???????
Project Project2.exe raised exception class EAccessViolation with message'Access
violation at address 004B5C1F in molule'Project2.exe',Read of address 000002D0',
Process stopped.Use step or Run to continue

程序是这样的:

Unit1 上的程序:
------------------------------------------------------
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls, Db, ADODB;

type
TForm1 = class(TForm)
RadioGroup1: TRadioGroup;
Button1: TButton;
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);

private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}
uses Unit2;



procedure TForm1.FormCreate(Sender: TObject);
begin
RadioGroup1.ItemIndex:=0;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
adoquery1.close;
adoquery1.sql.clear;

if RadioGroup1.ItemIndex=0 then
adoquery1.sql.add('select * from nan_one');
if RadioGroup1.ItemIndex=1 then
adoquery1.sql.add('select * from nan_two');
if RadioGroup1.ItemIndex=2 then
adoquery1.sql.add('select * from nan_three');
if RadioGroup1.ItemIndex=3 then
adoquery1.sql.add('select * from nan_four');

adoquery1.open;


end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Form2.QuickRep1.preview;
end;

end.
-----------------------------------------------------
Unit2上的程序是:
unit Unit2;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Db, ADODB, QuickRpt, Qrctrls, ExtCtrls;

type
TForm2 = class(TForm)
QuickRep1: TQuickRep;
DetailBand1: TQRBand;
QRDBText1: TQRDBText;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form2: TForm2;

implementation

uses Unit1;

{$R *.DFM}

procedure TForm2.FormCreate(Sender: TObject);

begin
Form2.QuickRep1.Dataset:=Form1.adoQuery1;
Form2.QRDBText1.Dataset:=Form1.adoQuery1;
Form2.QRDBText1.Datafield:='student_ID';
end;

end.

--------------------------------------------------
ADOQuery和ADOConnect都放置在FORM1上,但是不知道该往ADOQuery的SQL那一栏怎么填写,
所以没有填.
 
from2 要动态创建
Form2:=TForm2.create(self);
Form2.QuickRep1.preview;
 
能说具体一些马?
就是把这个动态创建放在哪里?
From2的OnCreate时间里面马?
还是????
其他要怎么样相应改呢?
 
好像动态创建也不行。试定义一个全局变量。
 
nplang
能说说怎么定义马?
在线等待,着急!!!!!谢谢
 
放在这里
procedure TForm1.Button2Click(Sender: TObject);
begin
Form2:=TForm2.create(self);
Form2.QuickRep1.preview;
Form2.free;
end;
如果不行,把下面的form2去掉
procedure TForm2.FormCreate(Sender: TObject);

begin
Form2.QuickRep1.Dataset:=Form1.adoQuery1;
~~~~~
Form2.QRDBText1.Dataset:=Form1.adoQuery1;
~~~~~
Form2.QRDBText1.Datafield:='student_ID';
~~~~~
end;
 
mlzhou:
我的程序改成这样了,但是还是显示一处的错误就是Form2没有定义,可以我定义了啊!!
procedure TForm1.Button2Click(Sender: TObject);
var form:tform2;
begin
Form2:=TForm2.create(self);
Form2.QuickRep1.preview;
Form2.free;
end;
而且
procedure TForm2.FormCreate(Sender: TObject);

begin
QuickRep1.Dataset:=Form1.adoQuery1;
QRDBText1.Dataset:=Form1.adoQuery1;
QRDBText1.Datafield:='student_ID';
end;



 
我想,错误应该处在这里
procedure TForm1.FormCreate(Sender: TObject);
begin
RadioGroup1.ItemIndex:=0;
end;
想想看,当Form1的OnCreate事件发生时,RadioGroup1还没创建呢,怎么能设置它的ItemIndex呢?
把这句放到Form1的OnShow事件中吧。其他的好像没看出什么问题来。
 
我觉的在form2中能不直接用form1.***最好,你可以重载form2.oncreate方法,让它代上常数
不过这是动态生成窗体的.
另:RadioGroup1.ItemIndex:=0;你可以在设计的时候指定的吧?

具体:
procedure TForm1.Button1Click(Sender: TObject);
begin
with TForm2.Create(self,'sfs有要') do
begin
showmodal;
free;
end;
end;
************************
unit Unitoverload;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm2 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
FName:string;
public
{ Public declarations }
constructor create(AOwner:Tcomponent;AName:string);overload;
end;

var
Form2: TForm2;

implementation

{$R *.dfm}

{ TForm2 }

constructor TForm2.create(AOwner:Tcomponent;AName: string);
begin
FName:=AName;
inherited Create(AOwner);
end;

procedure TForm2.Button1Click(Sender: TObject);
begin
showmessage(Fname);
end;
end.
 
郁闷拉郁闷拉
我用那个Button2来显示Form2的程序
procedure TForm1.Button2Click(Sender: TObject);
var form2:tform2;
begin
Form2:=TForm2.create(self);
Form2.QuickRep1.preview;
Form2.free;
end;
可是还是显示Form2和TForm2没有定义
就差这一点点
:(
 
搞定了
Form的定义是犯了文字错误
 
多人接受答案了。
 
感谢各位的帮助!!!!!!!
 
后退
顶部