Board index » delphi » Newcomer to Delphi - Retrieving data from tables without having to use form

Newcomer to Delphi - Retrieving data from tables without having to use form

-------------------------------------
Name: G.K.Sukumar
E-mail: suku...@singnet.com.sg
Date: @date@
Time: @time@
-------------------------------------

I have recently purchased Delphi and am trying to write
functions which will retrieve data from tables in the database.

I beleive to achieve this, I have to create a form and define a
query component and then assign my SQL statement to the
component to retrieve my data.

If I have to write general functions without any forms
involved (i.e functions which will be used across applications)
how do I do it?

Thanks in advance

 

Re:Newcomer to Delphi - Retrieving data from tables without having to use form


In article <4l88b0$...@lantana.singnet.com.sg>, suku...@singnet.com.sg says...

Quote
>I have recently purchased Delphi and am trying to write
>functions which will retrieve data from tables in the database.

>I beleive to achieve this, I have to create a form and define a
>query component and then assign my SQL statement to the
>component to retrieve my data.

>If I have to write general functions without any forms
>involved (i.e functions which will be used across applications)
>how do I do it?

You can create standalone database components that do not have a form.

You would just do something like:

Function LookupName(aCode: LongInt): String;
  var
    InputTable: TTable;
  begin
  InputTable := TTable.Create(Nil);
  InputTable.DatabaseName := 'd:\data';
  InputTable.TableName := 'Input.db';
  InputTable.Open;

  [insert code here]

  InputTable.Free;
  end;

This works with TQuery as well.  Check the reference to see what fields
you need to set before opening.

I don't have any code in front of me to check, but I believe that
calling TTable.Create(Nil) is what you have to do when there is no
form to act as owner.  

Brian

Re:Newcomer to Delphi - Retrieving data from tables without having to use form


Hi, Sukumar.

Quote
"G.K.Sukumar" <suku...@singnet.com.sg> wrote:
>I have recently purchased Delphi and am trying to write
>functions which will retrieve data from tables in the database.
>I beleive to achieve this, I have to create a form and define a
>query component and then assign my SQL statement to the
>component to retrieve my data.
>If I have to write general functions without any forms
>involved (i.e functions which will be used across applications)
>how do I do it?

I recently did this.

This is a little example. Base your code in this:

{ This function return the field "name" of a given table (or query) }
function GetName(data:DataSet) : string;
var aField : TField;
begin
  aField :=  data.FindField('name');
  if aField <> nil then
    result := aField.asString
  else
    raise Exception.create('Field "name" not encountered');
end;

Then you can write:

        name := GetName(aTable);
        name := GetName(aQuery);

Hope this helps. If you have a more "concrete" question, let me hear
it.

And sorry for my BAD english.

--
Juan Jos Navarro Mnguez  <jnava...@redestb.es>
Finger me to get my PGP public key.

Other Threads