我的界面1是一個button控件.界面2會么都沒有但是有一個adoconnection1和adoquery
datasource1,dbgrid1,button1.我運行程序后一切如常不會出錯但是我用界面1的button1來打開界面2.再用界面2的button1來關閉界面2.然后再用界面1的button1來打開界面2,問題就來了.出錯信息是這樣的:
message'access violation at address 00000000.read of address 00000000'代碼如下
Form1:procedure TForm1.Button1Click(Sender: TObject);
begin
  form2.Show;
end;form2:
procedure TForm2.FormShow(Sender: TObject);
begin
  adoconnection1.ConnectionString:='Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;User ID=sa;Initial Catalog=car;Data Source=sport_chen;Password=123';
  adoconnection1.LoginPrompt:=false;
  adoconnection1.Connected:=true;
  adoquery1.Connection:=adoconnection1;
  adoquery1.SQL.Add('select * from car');
  adoquery1.Active:=true;
end;請幫忙!!!!!!!!!!!!!!!!1

解决方案 »

  1.   

    在form2的OnClose事件里写:
    Form2.Free;
      

  2.   

    等一下,你Create Form2的语句写在什么地方了?
      

  3.   

    就是在form1的button1
    你看一下我的代碼不就知道了.
      

  4.   

    我還少貼了一段代碼:(這是關閉窗口2的代碼).
    form2:
      procedure TForm2.Formclose(Sender: TObject); 
      begin
        action:=cafree;
      

  5.   

    在form2的close事件中写
    form2:=nil;
    (假定form2为你的界面2的名字)
      

  6.   

    我知道問題出在哪了.
    就是adoconnection1連接到sqlserver數據庫后在form2關閉后沒有釋放.
    沒有完全關掉程序時再打開form2就又一次把adoconnection1鏈接到sqlserver中.
    但是我要怎樣才能在關閉form2時順便關掉adoconnection1的鏈接呢?
      

  7.   

    symsss(大明)說的和我用action:=cafree一樣.
    所以沒有用.出錯信息一樣.
      

  8.   

    把form1的button1改为
    if form2=nil then
       form2:=TForm2.create(application);
    form2.show;(或是form2.showmodal)
      

  9.   

    form2.show;//是可以的,但是工程文件必须是已经创建了该form
    adoconnection1.ConnectionString:='Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;User ID=sa;Initial Catalog=car;Data Source=sport_chen;Password=123';
    注意上面一句语法对吗?
      

  10.   

    thanks!
    我忘了我用這個更好!
    form1的button1的click事件寫
    application.createform(tform2,form2);
    form2.show;
    這樣就不會出錯了.
    我會給你分的請放心.
      

  11.   

    chenquan(嘉威王子) 
    我上面的代碼沒有錯啊!