Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Check to know , Is TADOQuery created or not ...

Posted on 2006-06-18
4
Medium Priority
?
456 Views
Last Modified: 2010-04-05
hi
i have a problem with TADOQuery,
i made a Query in my form like this :

var SQuery:TADOQuery;
     SDataSource;TDataSource;
begin
  SQuery:=TADOQuery.create(nil);
  SQuery.connection:=my_connection; //this connection exist and set to DB
  SQuery.Close;
  SQuery.SQL.Clear;
  SQuery.SQL.Text := 'select * from car';
  SQuery.Open;
  SDataSource := TDataSource.Create(nil);
  SDataSource.DataSet := SQuery;
  DBGrid1.DataSource := SDataSource;
end;

i wanna release Query and i know that i must write    SQuery.Free;      but i wanna first  check that my Query created or not ?
if Query exist then i wanna free it else i dont want to do anything,
how can i check it, what's the commands for that ?

tanxfull
Mahsa
 
0
Comment
Question by:Mahsa60
  • 2
4 Comments
 
LVL 8

Expert Comment

by:RickJ
ID: 16929317
if Assigned(SQuery) then
  SQuery.Free ;

or

if SQuery <> nil then
  SQuery.Free ;
0
 

Author Comment

by:Mahsa60
ID: 16929688
i need to check query in 3 places , and when i check Assigned(Qry1) for the first time it works correctly , but in the second time it returns TRUE again and i get errors .
why does it return TRUE when i FREE it before?
in Submit BUTTON i FREE it but when i press EXIT BUTTON , Assign return TRUE and in the FREE line i get error , i think it must return FALSE for second time , is it right?
what's my problem ?
how can i correct it ?






unit U_Car;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, U_Base_Template, DBCtrls, StdCtrls, Mask, Buttons, Grids,
  DBGrids, ExtCtrls, DB, ADODB;

type
  TF_Car = class(TF_Base_Template)
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    E_Registry_Id: TDBEdit;
    E_Plate_No: TDBEdit;
    E_Car_Id: TDBEdit;
    C_Car_TId: TDBLookupComboBox;
    Panel3: TPanel;
    E_RName: TDBEdit;
    E_RHouse_No: TDBEdit;
    E_RDate: TDBEdit;
    Label6: TLabel;
    Label12: TLabel;
    Label5: TLabel;
    B_Search: TBitBtn;
    procedure B_AddClick(Sender: TObject);
    procedure B_SubmitClick(Sender: TObject);
    procedure B_CloseClick(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure B_SearchClick(Sender: TObject);
  private
    Qry1 : TADOQuery;
    DS1 :TDataSource;
  public
    { Public declarations }
  end;

var
  F_Car: TF_Car;

implementation

uses U_DModule;

{$R *.dfm}
//******************************************************************************
procedure TF_Car.FormActivate(Sender: TObject);
begin
  inherited;
  F_DModule.T_Car.Open;
  B_Search.Enabled := False;
end;
//******************************************************************************
procedure TF_Car.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  inherited;
  F_DModule.T_Car.Close;
  if Assigned(Qry1) then
    begin
      DS1.Free;  Qry1.Free;
    end;  
end;
//******************************************************************************
procedure TF_Car.B_AddClick(Sender: TObject);
begin
  inherited;
  F_DModule.T_Car.Append;
  B_Search.Enabled := True;
  E_Registry_Id.SetFocus;
end;
//******************************************************************************
procedure TF_Car.B_SubmitClick(Sender: TObject);
begin
  inherited;
  F_DModule.T_Car.Post;
  B_Search.Enabled := False;
  if Assigned(Qry1) then
    begin
      DS1.Free;  Qry1.Free;
    end;
end;
//******************************************************************************
procedure TF_Car.B_CloseClick(Sender: TObject);
begin
  inherited;
  F_DModule.T_Car.Cancel;
  if Assigned(Qry1) then
    begin
      DS1.Free;  Qry1.Free;
    end;
end;
//******************************************************************************
procedure TF_Car.B_SearchClick(Sender: TObject);
begin
  inherited;
  try
   //this a procedure in another form that create my query .
    CreateADOQuery(Qry1,DS1,' SELECT R.NAME,R.HOUSE_NO,'+
                   ' (SUBSTRING(R.DATE,1,4) +''/''+ SUBSTRING(R.DATE,5,2)+''/''+'+
                   ' SUBSTRING(R.DATE,7,2))DATE'+
                   ' FROM REGISTRY R'+
                   ' WHERE REGISTRY_ID ='+ QuotedStr(E_Registry_Id.Text) );
    E_RName.DataSource := DS1;
    E_RHouse_No.DataSource := DS1;
    E_RDate.DataSource := DS1;
  except
    DS1.Free; Qry1.Free;
  end;
end;
//******************************************************************************
end.
0
 
LVL 8

Accepted Solution

by:
RickJ earned 200 total points
ID: 16931285
Sorry Mahsa60,
Should be...

if Assigned(SQuery) then
  FreeAndNil(SQuery) ;

This ensures that var is set to nil.
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 16936950
Assigned will sometimes still return True if you do not set the pointer to nil

  SQuery.Free;
  SQuery := nil;

or use

FreeAndNil(SQuery).
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question