TQuery as a result of a function 
Author Message
 TQuery as a result of a function

I tried to make up a function which creates a TQuery, extracts some
information and returns the query result to the caller, but it doesn't work.
I did something like this:

function query(SqlString): TQuery;
var
   MyQuery: TQuery;
begin
  MyQuery := TQuery.Create(nil);
  with MyQuery do begin
    sql.clear;
    Sql.Add(SqlString);
     try
      Open;
     except
      Application.MessageBox('Error','Database error',IDOK);
      MyQuery.Close;
      MyQuery.Free;
    end;

   result:= MyQuery;

   MyQuery.Close;
    MyQuery.Free;
end;

The function does its job, but the module calling the function fails when it
tries to access the query result.
I guess it can't be done!  Anyone nkows why ?



Mon, 07 Mar 2005 05:10:27 GMT  
 TQuery as a result of a function

Quote:
> I tried to make up a function which creates a TQuery, extracts some
> information and returns the query result to the caller, but it doesn't
work.
> I did something like this:

> function query(SqlString): TQuery;
> var
>    MyQuery: TQuery;
> begin
>   MyQuery := TQuery.Create(nil);
>   with MyQuery do begin
>     sql.clear;
>     Sql.Add(SqlString);
>      try
>       Open;
>      except
>       Application.MessageBox('Error','Database error',IDOK);
>       MyQuery.Close;
>       MyQuery.Free;
>     end;

>    result:= MyQuery;

>    MyQuery.Close;
>     MyQuery.Free;
> end;

> The function does its job, but the module calling the function fails when
it
> tries to access the query result.
> I guess it can't be done!  Anyone nkows why ?

Hi,

By calling MyQuery.Free your Result points to a destroyed instance.

Try something like this

function CreateThisQuery(SqlString: string): TQuery;
begin
  result := TQuery.Create(nil);
  result.Sql.Add(SqlString);
  try
    result.Open;
  except
    on e: Exception do
    begin
      Showmessage( e.message );
      result.Free;
      result := nil;
  end;
end;

procedure DoSomething;
var
  qryTemp: TQuery;
begin
  qryTemp := CreateThisQuery( 'select * from SomeTable' );
  if Assigned( qryTemp ) then
    try
       ....
    finally
      qryTemp.Close;
      qryTemp.Free;
    end;
end;

HTH.

Raymond.



Mon, 07 Mar 2005 07:34:37 GMT  
 TQuery as a result of a function
I tried and, yes, it works ! I am now wondering whether this thing is of any
help, since I need to have a TQuery within the function that does the
extract and another one in the calling program where the results go ...



Quote:


> > I tried to make up a function which creates a TQuery, extracts some
> > information and returns the query result to the caller, but it doesn't
> work.
> > I did something like this:

> > function query(SqlString): TQuery;
> > var
> >    MyQuery: TQuery;
> > begin
> >   MyQuery := TQuery.Create(nil);
> >   with MyQuery do begin
> >     sql.clear;
> >     Sql.Add(SqlString);
> >      try
> >       Open;
> >      except
> >       Application.MessageBox('Error','Database error',IDOK);
> >       MyQuery.Close;
> >       MyQuery.Free;
> >     end;

> >    result:= MyQuery;

> >    MyQuery.Close;
> >     MyQuery.Free;
> > end;

> > The function does its job, but the module calling the function fails
when
> it
> > tries to access the query result.
> > I guess it can't be done!  Anyone nkows why ?

> Hi,

> By calling MyQuery.Free your Result points to a destroyed instance.

> Try something like this

> function CreateThisQuery(SqlString: string): TQuery;
> begin
>   result := TQuery.Create(nil);
>   result.Sql.Add(SqlString);
>   try
>     result.Open;
>   except
>     on e: Exception do
>     begin
>       Showmessage( e.message );
>       result.Free;
>       result := nil;
>   end;
> end;

> procedure DoSomething;
> var
>   qryTemp: TQuery;
> begin
>   qryTemp := CreateThisQuery( 'select * from SomeTable' );
>   if Assigned( qryTemp ) then
>     try
>        ....
>     finally
>       qryTemp.Close;
>       qryTemp.Free;
>     end;
> end;

> HTH.

> Raymond.



Thu, 10 Mar 2005 01:34:32 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. TQuery from another TQuery result??

2. TQuery on TQuery result set??

3. Using Tquery results as input for an next TQuery

4. Function result

5. Function result type

6. function resulting in array?

7. function result type

8. Working with TQuery Result Set

9. Retrieving a result set with a TQuery

10. TQuery & Live Result

11. Getting a read/write SQL query result using a TQuery and a TTable

12. TQuery and Live Result Set

 

 
Powered by phpBB® Forum Software