Board index » delphi » FieldByName('blahblah').Value for different field types

FieldByName('blahblah').Value for different field types

I need to do this but wouldn't it be easier to use the Values of the Fields?

    for i := 0 to Pred(Query1.FieldCount) do
    begin
      case Query1.Fields[i].DataType of
        ftString:
          begin
            sRecord := sRecord + Query1.Fields[i].FieldName + '='''+
                       Query1.FieldByName(Query1.Fields[i].FieldName).Value
+ ''' ';
          end;
        ftInteger:
          begin
            sRecord := sRecord + Query1.Fields[i].FieldName + '='''+

IntToStr(Query1.FieldByName(Query1.Fields[i].FieldName).AsInteger) + ''' ';
          end;
      end;
    end;

How would I do this using
Query1.FieldByName(Query1.Fields[i].FieldName).Value ??

sRecord is a string variable.

 

Re:FieldByName('blahblah').Value for different field types


Eddie,

Not sure if I get what you are aiming at, but if all you want to do is
have the value of a field come out as a string, no matter what it is
then:

Query1.FieldByName('SomeName').AsString should do the trick.....
 for i := 0 to Pred(Query1.FieldCount) do
          sRecord := sRecord + Query1.Fields[i].FieldName + '='''+

Query1.FieldByName(Query1.Fields[i].FieldName).AsString + ''' ';

HTH

On Tue, 6 Jul 1999 17:28:22 -0500, "Eddie Shipman"

Quote
<Ed...@acadsoft.com> wrote:
>I need to do this but wouldn't it be easier to use the Values of the Fields?

>    for i := 0 to Pred(Query1.FieldCount) do
>    begin
>      case Query1.Fields[i].DataType of
>        ftString:
>          begin
>            sRecord := sRecord + Query1.Fields[i].FieldName + '='''+
>                       Query1.FieldByName(Query1.Fields[i].FieldName).Value
>+ ''' ';
>          end;
>        ftInteger:
>          begin
>            sRecord := sRecord + Query1.Fields[i].FieldName + '='''+

>IntToStr(Query1.FieldByName(Query1.Fields[i].FieldName).AsInteger) + ''' ';
>          end;
>      end;
>    end;

>How would I do this using
>Query1.FieldByName(Query1.Fields[i].FieldName).Value ??

>sRecord is a string variable.

--Donovan

Donovan J. Edye
Namadgi Systems
Web: www.namsys.com.au
E-Mail: dono...@namsys.com.au
Voice: +61 2 6285-3460
Fax: +61 2 6285-3459
TVisualBasic := Class(None);

Re:FieldByName('blahblah').Value for different field types


On Tue, 6 Jul 1999 17:28:22 -0500, "Eddie Shipman" <Ed...@acadsoft.com>
wrote:

Quote
>I need to do this but wouldn't it be easier to use the Values of the Fields?

>    for i := 0 to Pred(Query1.FieldCount) do
>    begin
>      case Query1.Fields[i].DataType of
>        ftString:
>          begin
>            sRecord := sRecord + Query1.Fields[i].FieldName + '='''+
>                       Query1.FieldByName(Query1.Fields[i].FieldName).Value
>+ ''' ';
>          end;
>        ftInteger:
>          begin
>            sRecord := sRecord + Query1.Fields[i].FieldName + '='''+

>IntToStr(Query1.FieldByName(Query1.Fields[i].FieldName).AsInteger) + ''' ';
>          end;
>      end;
>    end;

>How would I do this using
>Query1.FieldByName(Query1.Fields[i].FieldName).Value ??

>sRecord is a string variable.

First, the field reference provided by the Fields property and FieldByName
are interchangeable. So using Fields to get the field name for use with
FieldByName is redundant and too much typing <grin>. Just use the Fields
property.

Second, to cause a field value to be extracted as a string, use the
TField.AsString property.

  sRecord := sRecord + Query1.Fields[i].FieldName + '=' +
    QuotedStr(Query1.Fields[i].AsString);

This should work for simple field types, like string and integer. Numeric
values are automatically converted to string by AsString.

When dealing with other field types, though, this can cause problems. For
instance, trying to retrieve the data from a BLOB field with image data
using AsString can be vexing. If you have these field types in the table,
consider checking for whole groups of like fields: simple (string, integer,
etc.) or complex (BLOB, memo, etc.). Handle each category of field
appropriately.

  if (Fields[i].DataType in [ftString, ftInteger, ftFloat, ...]) then begin
    { handle as simple field type }
  end
  else if (Fields[i].DataType in [ftBlob, ftMemo, ...]) then begin
    { handle as complex field type }
  end;

Also worth mentioning is that the VarToStr function can be used on the
value returned by the TField.Value property to force it to be treated as a
string -- even if the field contains a null or a numeric value.

  ShowMessage(VarToStr(Query1.Fields[i].Value));

==========================================================================
Steve Koterski                  "Computers are useless. They can only give
Technical Publications          you answers."
Borland                                       -- Pablo Picasso (1881-1973)
http://www.borland.com/techpubs/delphi

Re:FieldByName('blahblah').Value for different field types


there are times when you may need to use fieldbyname.
If you use Infopower, they have lots of examples and requirements.
Sometimes, you can't refer to the table or you want the code portable
between two different tables with the same field names so fieldbyname
is helpful there.  Let's face it, the pros at Borland knew what they were
doing when they wrote Delphi.

As an example, you could have a function with a TTable as
a parameter and you know the name of the fields in the files but
not the TField.name.  (Is that unclear enuf?)

Quote
Eddie Shipman <Ed...@acadsoft.com> wrote in message

news:7ltvnu$dii7@forums.borland.com...
Quote
> I need to do this but wouldn't it be easier to use the Values of the
Fields?

>     for i := 0 to Pred(Query1.FieldCount) do
>     begin
>       case Query1.Fields[i].DataType of
>         ftString:
>           begin
>             sRecord := sRecord + Query1.Fields[i].FieldName + '='''+

Query1.FieldByName(Query1.Fields[i].FieldName).Value
Quote
> + ''' ';
>           end;
>         ftInteger:
>           begin
>             sRecord := sRecord + Query1.Fields[i].FieldName + '='''+

> IntToStr(Query1.FieldByName(Query1.Fields[i].FieldName).AsInteger) + '''
';
>           end;
>       end;
>     end;

> How would I do this using
> Query1.FieldByName(Query1.Fields[i].FieldName).Value ??

> sRecord is a string variable.

Re:FieldByName('blahblah').Value for different field types


In article <7ltvnu$d...@forums.borland.com>, Ed...@acadsoft.com says...

Hi Eddie,

I've just tried this and it does what I think you are trying to do. The
key factor is the use of the Text property of TField.

procedure TForm1.Button1Click(Sender: TObject);
var
  Field: TField;
  sRecord: String;
  i: Integer;
begin
  Query1.Open;
  Query1.First;
  while not Query1.EOF do
  begin
    sRecord := '';
    for i := 0 to Pred(Query1.FieldCount) do
    begin
      Field := Query1.Fields[i];
      if Field.DataType in [ftString, ftInteger] then
        sRecord := sRecord + Field.FieldName + '=' + Field.Text + ' ';
    end;
    ListBox1.Items.Add(sRecord);
    Query1.Next;
  end;
  Query1.Close;
end;

Cheers,
Carl

---8<---

Quote

> How would I do this using
> Query1.FieldByName(Query1.Fields[i].FieldName).Value ??

> sRecord is a string variable.

Re:FieldByName('blahblah').Value for different field types


Quote
<Steve Koterski (Borland)> wrote in message

news:37828ade.27390550@forums.inprise.com...

<SNIP>

Quote
> Also worth mentioning is that the VarToStr function can be used on the
> value returned by the TField.Value property to force it to be treated as a
> string -- even if the field contains a null or a numeric value.

>   ShowMessage(VarToStr(Query1.Fields[i].Value));

This is what I was looking for...Thaks Steve...
How's the Klingon coming?

Re:FieldByName('blahblah').Value for different field types


In article <7ltvnu$d...@forums.borland.com>,
  "Eddie Shipman" <Ed...@acadsoft.com> wrote:
Quote
> I need to do this but wouldn't it be easier to use the Values of the
Fields?

>     for i := 0 to Pred(Query1.FieldCount) do
>     begin
>       case Query1.Fields[i].DataType of
>         ftString:
>           begin
>             sRecord := sRecord + Query1.Fields[i].FieldName + '='''+

Query1.FieldByName(Query1.Fields[i].FieldName).Value

Quote
> + ''' ';
>           end;
>         ftInteger:
>           begin
>             sRecord := sRecord + Query1.Fields[i].FieldName + '='''+

> IntToStr(Query1.FieldByName(Query1.Fields[i].FieldName).AsInteger) +
''' ';
>           end;
>       end;
>     end;

> How would I do this using
> Query1.FieldByName(Query1.Fields[i].FieldName).Value ??

> sRecord is a string variable.

--

Why cant you simply use

 sRecord := sRecord + Query1.Fields[i].FieldName + '='''+
     Query1.FieldByName(Query1.Fields[i].FieldName).AsString + ''' ';
                                                    ********
You need not even bother to check for the fieldtype.

*************************************
Sukasu Computers Private Limited
*************************************

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.

Re:FieldByName('blahblah').Value for different field types


In article <7m2tog$kk...@nnrp1.deja.com>,
  Gopala Raghavan <suk...@vsnl.com> wrote:

Quote
> In article <7ltvnu$d...@forums.borland.com>,
>   "Eddie Shipman" <Ed...@acadsoft.com> wrote:
> > I need to do this but wouldn't it be easier to use the Values of the
> Fields?

> >     for i := 0 to Pred(Query1.FieldCount) do
> >     begin
> >       case Query1.Fields[i].DataType of
> >         ftString:
> >           begin
> >             sRecord := sRecord + Query1.Fields[i].FieldName + '='''+

> Query1.FieldByName(Query1.Fields[i].FieldName).Value
> > + ''' ';
> >           end;
> >         ftInteger:
> >           begin
> >             sRecord := sRecord + Query1.Fields[i].FieldName + '='''+

> > IntToStr(Query1.FieldByName(Query1.Fields[i].FieldName).AsInteger) +
> ''' ';
> >           end;
> >       end;
> >     end;

> > How would I do this using
> > Query1.FieldByName(Query1.Fields[i].FieldName).Value ??

> > sRecord is a string variable.

> --

> Why cant you simply use

>  sRecord := sRecord + Query1.Fields[i].FieldName + '='''+
>      Query1.FieldByName(Query1.Fields[i].FieldName).AsString + ''' ';
>                                                     ********
> You need not even bother to check for the fieldtype.

> *************************************
> Sukasu Computers Private Limited
> *************************************

> Sent via Deja.com http://www.deja.com/
> Share what you know. Learn what you don't.

--

As an afterthought, I think even this will do

sRecord := sRecord + Query1.Fields[i].FieldName + '='''+
   Query1.Fields[i].AsString + ''' ';
                    ********

*************************************
Sukasu Computers Private Limited
*************************************

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.

Other Threads