2977:Listing the field structures of a table.
KEYWORDS: fields, field types AREA: Application Interop
Q: How can I list the field structures of a table?
A: This project demonstrates listing the field structure from
a given table, using the Fields and IndexDefs arrays, and
displaying them in a listbox.
There is a demo (dbbrowsr.dpr) approaches this task
differently. You may want to compare the two versions of this
code.
Note: This code works with 16 bit only.
procedure TForm1.Button1Click(Sender: TObject); const
MyFielddefs: array[ftUnknown..ftGraphic] of string [8] =
('Unknown', 'String', 'Smallint', 'Integer', 'Word',
'Boolean', 'Float', 'Currency', 'BCD', 'Date',
'Time', 'DateTime', 'Bytes', 'VarBytes', 'Blob',
'Memo', 'Graphic');
var
i, Indx: integer;
Definition: string;
begin
for i := 0 to Table1.FieldCount - 1 do begin
Definition := Table1.Fields[i].DisplayLabel;
Definition := Definition + ' ' +
MyFieldDefs[Table1.Fields[i].DataType];
Table1.IndexDefs.Update;
if Table1.Fields[i].IsIndexField then begin
Indx := Table1.IndexDefs.Indexof(Table1.Fields[i].Name);
if Indx > -1 then
if ixPrimary in Table1.IndexDefs[Indx].Options then
Definition := Definition + ' (Primary)';
end;
Listbox1.Items.Add(Definition);
end;
end;
The version above does not work with the 32 bit version as
there are more field types that must now be taken into
account. Here is a version that works with the 32 bit version:
procedure TForm1.Button1Click(Sender: TObject);
const
MyFielddefs: array[ftUnknown..ftTypedBinary] of string [11] =
('Unknown', 'String', 'Smallint', 'Integer',
'Word', 'Boolean', 'Float', 'Currency', 'BCD',
'Date', 'Time', 'DateTime', 'Bytes', 'VarBytes',
'AutoInc', 'Blob', 'Memo', 'Graphic', 'FmtMemo',
'ParadoxOle', 'DBaseOle', 'TypedBinary');
var
i, Indx: integer;
Definition: string;
begin
for i := 0 to Table1.FieldCount - 1 do begin
Definition := Table1.Fields[i].DisplayLabel;
Definition := Definition + ' ' +
MyFieldDefs[Table1.Fields[i].DataType];
Table1.IndexDefs.Update;
if Table1.Fields[i].IsIndexField then begin
Indx := Table1.IndexDefs.Indexof(Table1.Fields[i].Name);
if Indx > -1 then
if ixPrimary in Table1.IndexDefs[Indx].Options then
Definition := Definition + ' (Primary)';
end;
Listbox1.Items.Add(Definition);
end;
end;
TI