Solved

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

Posted on 2006-06-18
4
451 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 50 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Convert a string into a TDateTime 5 80
tidtcpserver connection lost handle 2 124
Multi-layered image in FireMonkey 9 93
QuickReport, Delphi, Shape 12 48
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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

739 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