Solved

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

Posted on 2006-06-18
4
443 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
Comment Utility
if Assigned(SQuery) then
  SQuery.Free ;

or

if SQuery <> nil then
  SQuery.Free ;
0
 

Author Comment

by:Mahsa60
Comment Utility
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 50 total points
Comment Utility
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
Comment Utility
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 your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now