Solved

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

Posted on 2006-06-18
4
446 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 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Virtuailstring tree compare node issue 14 105
FMX enumerated colours 2 84
FMX and jaudiotracker playing memory stream 29 66
update joined tables 2 31
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

920 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

17 Experts available now in Live!

Get 1:1 Help Now