Board index » delphi » Table1.FieldByName('x').DisplayFormat runtime problem.

Table1.FieldByName('x').DisplayFormat runtime problem.

Hi,

We have a simple problem I hope someone may be able to help with, even if the
answer's 'No you can't!'.

  We have a database linked to a TTable component at design time
and wish to assign their field properties at run time.
We do not want to use the fields editor to include their properties
as the database will change when it is used in different scenarios.
  We are tring to write to a TFloatField by using the
FieldByName procedure but are having a problem when it comes to
the DisplayFormat property.

  with Table1 do
  begin
    FieldByName('OffsetID').DisplayWidth  := 19;        {OK}
    FieldByName('OffsetID').DisplayFormat := '##.####'; {Error}
  end;                      |
                            |  Error 44:Field identifier expected

  We are able to write to the DisplayWidth property with no trouble
but not the DisplayFormat. If we include the field using the
fields editor then we can write to the property as above however we
do not wish to use the fields editor.

I would be grateful if you could please e-mail me with any solutions as well.

Thanks in advance

--
Alan
----
 /          o  \      Alan Fisher - Leicester - England
|  ~   ___  o ~ |
|   |\/  .\o    |     e-mail : a...@fishbowl.foobar.co.uk
| ~ |/\___/  ~  |
 \_____________/

 

Re:Table1.FieldByName('x').DisplayFormat runtime problem.


Quote
Alan Fisher (a...@fishbowl.foobar.co.uk) wrote:

:   with Table1 do
:   begin
:     FieldByName('OffsetID').DisplayWidth  := 19;        {OK}
:     FieldByName('OffsetID').DisplayFormat := '##.####'; {Error}
:   end;                      |
:                             |  Error 44:Field identifier expected

TField does not have an DisplayFormat property, TFloatField has.

So:
  TFloatField(Table1.FieldByName('OffsetID').DisplayFormat:='##.####';

Tijs.

Re:Table1.FieldByName('x').DisplayFormat runtime problem.


Quote
Tijs Huisman (t...@dutecaj.et.tudelft.nl) wrote:

: Alan Fisher (a...@fishbowl.foobar.co.uk) wrote:

: :   with Table1 do
: :   begin
: :     FieldByName('OffsetID').DisplayWidth  := 19;        {OK}
: :     FieldByName('OffsetID').DisplayFormat := '##.####'; {Error}
: :   end;                      |
: :                             |  Error 44:Field identifier expected

: TField does not have an DisplayFormat property, TFloatField has.

: So:
:   TFloatField(Table1.FieldByName('OffsetID').DisplayFormat:='##.####';

Oops, forgot the ')':
TFloatField(Table1.FieldByName('OffsetID')).DisplayFormat:='##.####';

: Tijs.

--
Ferm dag,

Tijs.

Re:Table1.FieldByName('x').DisplayFormat runtime problem.


Quote
Alan Fisher wrote:

> Hi,

> We have a simple problem I hope someone may be able to help with, even if the
> answer's 'No you can't!'.

>   We have a database linked to a TTable component at design time
> and wish to assign their field properties at run time.
> We do not want to use the fields editor to include their properties
> as the database will change when it is used in different scenarios.
>   We are tring to write to a TFloatField by using the
> FieldByName procedure but are having a problem when it comes to
> the DisplayFormat property.

>   with Table1 do
>   begin
>     FieldByName('OffsetID').DisplayWidth  := 19;        {OK}
>     FieldByName('OffsetID').DisplayFormat := '##.####'; {Error}
>   end;                      |
>                             |  Error 44:Field identifier expected

>   We are able to write to the DisplayWidth property with no trouble
> but not the DisplayFormat. If we include the field using the
> fields editor then we can write to the property as above however we
> do not wish to use the fields editor.

> I would be grateful if you could please e-mail me with any solutions as well.

> Thanks in advance

> --
> Alan
> ----
>  /          o  \      Alan Fisher - Leicester - England
> |  ~   ___  o ~ |
> |   |\/  .\o    |     e-mail : a...@fishbowl.foobar.co.uk
> | ~ |/\___/  ~  |
>  \_____________/

Result of FieldByName() method is TField. Property DisplayFormat is
assigned to :
TDateField, TDateTimeField, TIntegerField, TSmallintField, TTimeField, TWordField components.

If type of field 'OffsetId' is e.g. TIntegerField ( integer ), you may write :

 with Table1 do
  begin
    FieldByName('OffsetId').DisplayWidth  := 19;        {OK}
    TIntegerField( FieldByName('OffsetId') ).DisplayFormat := '##.####'; {OK}
  end;

It works, I try.

Re:Table1.FieldByName('x').DisplayFormat runtime problem.


Quote
In article <4qn91k$...@quark.foobar.co.uk> a...@fishbowl.foobar.co.uk (Alan Fisher) writes:
>From: a...@fishbowl.foobar.co.uk (Alan Fisher)
>Subject: [Q] Table1.FieldByName('x').DisplayFormat runtime problem.
>Date: 24 Jun 1996 23:40:36 GMT
>Hi,
>We have a simple problem I hope someone may be able to help with, even if the
>answer's 'No you can't!'.
>  We have a database linked to a TTable component at design time
>and wish to assign their field properties at run time.
>We do not want to use the fields editor to include their properties
>as the database will change when it is used in different scenarios.
>  We are tring to write to a TFloatField by using the
>FieldByName procedure but are having a problem when it comes to
>the DisplayFormat property.
>  with Table1 do
>  begin
>    FieldByName('OffsetID').DisplayWidth  := 19;        {OK}
>    FieldByName('OffsetID').DisplayFormat := '##.####'; {Error}
>  end;                      |
>                            |  Error 44:Field identifier expected
>  We are able to write to the DisplayWidth property with no trouble
>but not the DisplayFormat. If we include the field using the
>fields editor then we can write to the property as above however we
>do not wish to use the fields editor.
>I would be grateful if you could please e-mail me with any solutions as well.
>Thanks in advance
>--
>Alan
>----

Alan:  Just curious why you don't want to use the fields editor?

-Dave

Re:Table1.FieldByName('x').DisplayFormat runtime problem.


Quote
Alan Fisher (a...@fishbowl.foobar.co.uk) wrote:
> Hi,

> We have a simple problem I hope someone may be able to help with, even if the
> answer's 'No you can't!'.
--snip snip---
>   We are tring to write to a TFloatField by using the
> FieldByName procedure but are having a problem when it comes to
> the DisplayFormat property.

>   with Table1 do
>   begin
>     FieldByName('OffsetID').DisplayWidth  := 19;        {OK}
>     FieldByName('OffsetID').DisplayFormat := '##.####'; {Error}
>   end;                      |
>                             |  Error 44:Field identifier expected

Hi,

The problem is that TTable.FieldByName (as well Fields[idx] and FindField)
method returns object of TField class. TField class doesn't have a properies
like DisplayFormat, MinValue, MaxValue and so on. There properties are
defined in a subclasses like TFloatField (TFloatField derives TField).
In this case you have to make a typecast from TField to TFloatField. You
might do something like this:

  var
   offsetField: TFloatField;
  begin
   ...
  with Table1 do begin
   offsetField := FieldByName('OffsetID') as TFloatField;
   offsetField.DisplayWidth := 19;
   offsetField.DisplayFormat := '###.###';
  end;

You can change these values in design time, because Object Inspector makes
the typecast for you. You can see the actual type of table field in a
Object Inspector by looking at the component name combobox in OI. In your
case there would be something like 'Table1OffsetID: TFloatField'.

I hope this helps.
  Mika

Re:Table1.FieldByName('x').DisplayFormat runtime problem.


Quote
>>     FieldByName('OffsetID').DisplayFormat := '##.####'; {Error}

     TFloatField(FieldByName('OffsetID')).DisplayFormat := '##.####'; {Error}

_
******************************************************************
NOTE: This software is currently in early alpha. If you notice any
problems, or RFC non-compliance, please report it to p...@pbe.com
 \------------------------------------------------------------\
  \           Chad Z. Hower  -  phoe...@pobox.com              \
   \  Phoenix Business Enterprises - p...@pbe.com - www.pbe.com  \
    \     Physically in Church Hill, TN - Logically Not Sure     \
     \------------------------------------------------------------\

Quote
>>SQUID - The ultimate 95/NT offline databasing reader

**Special Compile: 3.000A (Alpha)

Other Threads