Solved

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Best Firemonkey component pack 1 104
Internet Explorer View Settings Question 15 111
Multiple image collision 13 74
Breakpoint doesn't stop in my variable 3 27
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

772 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