Is there any function that it could return me the datatype name of the field in string?

Hy

I use delphi 5 and I'd like to know if there is a function that can return the datetype of my field in string that I could display in a memoedit.

thanks
Alex
LVL 1
hidrauAsked:
Who is Participating?
 
Emmanuel PASQUIERFreelance Project ManagerCommented:
you can use RTTI information to get the string equivalent of the values in an enum type declaration

1) Add TypInfo unit in uses

2) use GetEnumName function to retrieve the constant name (used in code) corresponding to a value (enumerated, so in fact an integer) of a certain type

3) for the above, you use TypeInfo function to get a pointer on RTTI information of a given type declaration

Delphi is wonderful :o)

Here, a full example listing in a memo all the TFieldType constants strings
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Memo: TMemo;
    procedure FormCreate(Sender: TObject);
  private
    { Déclarations privées }
  public
    { Déclarations publiques }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
Uses TypInfo,DB;

procedure TForm1.FormCreate(Sender: TObject);
var
 FT:TFieldType;
begin
 for FT := Low(TFieldType) to High(TFieldType) do
  Memo.Lines.Add(GetEnumName(TypeInfo(TFieldType),Integer(FT)));
end;

end.

Open in new window

0
 
8080_DiverCommented:
Are you refering to a column in a database, a field in a data file, or what?

Usually there is a way to get the information from a database, assuming you have sufficient permissions to allow you to do so.

As for the Data File, that sort of depends on the data file and the manner in which you are accessing it.  

Other than that, the general IT answer, as always, is going to be, "It depends."
0
 
hidrauAuthor Commented:
When I open a query in an adoquery and I load the columns I want to display in memoedit all the fields name e its datatype:

something like this:

field = product  datatype = ftstring size = 30

the fieldname and size I was able to inform, but datatype not.

The only way I found is that:

  if Qexcel.FieldByName(AFieldName).DataType = ftDateTime Then str :=   'ftDateTime '
  if Qexcel.FieldByName(AFieldName).DataType = ftFloat   Then str := 'ftFloat'

I need to compare with exited datatypes

0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
hidrauAuthor Commented:
Hi,

I got it :))

this was what I would like

function TFListCotImporta.TipoField(campo: String): String;
const
  cmp : array[0..37] of string = ('ftUnknown', 'ftString', 'ftSmallint', 'ftInteger', 'ftWord', 'ftBoolean', 'ftFloat', 'ftCurrency', 'ftBCD', 'ftDate', 'ftTime', 'ftDateTime', 'ftBytes',
                           'ftVarBytes', 'ftAutoInc', 'ftBlob', 'ftMemo', 'ftGraphic', 'ftFmtMemo', 'ftParadoxOle', 'ftDBaseOle', 'ftTypedBinary', 'ftCursor', 'ftFixedChar', 'ftWideString',
                           'ftLargeint', 'ftADT', 'ftArray', 'ftReference', 'ftDataSet', 'ftOraBlob', 'ftOraClob', 'ftVariant', 'ftInterface', 'ftIDispatch', 'ftGuid', 'ftTimeStamp', 'ftFMTBcd');

var
  x : variant;
begin
  x := TFieldType(Adoquery.FieldByName(campo).DataType);
  showMessage(cmp[StrToInt(x)])
end;
0
 
Emmanuel PASQUIERFreelance Project ManagerCommented:
I forgot to mention that this technique of course works with ALL enumerated types, even your own, and does not require that you manually maintain another table of string constants for each and everyone of those enums types where you would need to get their value to string equivalent

Delphi do it for you. Which is precisely why I said Delphi is wonderful :o).
Did I said how great Delphi's RTTI (Run-Time Type Information) system is ?
0
 
hidrauAuthor Commented:
thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.