Find if field exists in a dataset

Hi.

I'm attempting to use Table.FindField('Field2') to determine if a field is  in a table or not.

This works exactly as I would expect if NO fields are defined in the field  editor.

If I define fields with the field editor, FindFields will only find fields in this list.

Is this REALLY the expected result?  If it is, just how DO I determine if a field exists in a table?

I have also tried Table.GetFieldNames(ListBox1.Items);  but the same limitations apply.

Best Wishes
Tony Hunt
TonyDelphiHuntAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kenpemCommented:
You don't state your method of data access - are you using ADO or BDE tables? If BDE, consider swtiching to ADO as soon as possible as BDE support is getting really old and creaky.

Assuming you're on ADO, try opening the table with a query, then checking qryGenData.FieldList.Fields[x].FieldName for each column value of x.
TonyDelphiHuntAuthor Commented:
Hi,
Thanks for your suggestion, I'm using neither BDE or ADO. I'm using ADS from Advantage, a client-server solution. However I have tried various data access methods and the same peculiarity appears in all.
It's obviously a 'feature' of Delphis field  editor.

As a workround I've written the enclosed function FieldCheck. Similar principals to your suggestion.

However I'd still like to find a neater solution, without opening another instance of the table.

function TForm1.FieldCheck(oTable:TAdsTable;cField:String): Boolean;
var
  oTry: TAdsTable;
begin
  oTry := TAdsTable.Create(Self);
  oTry.AdsConnection := oTable.AdsConnection;
  oTry.TableName := oTable.TableName;
  oTry.Open;
  result := not (oTry.FindField(cField) = Nil);
  oTry.Close;
  oTry.Free;
end;

Open in new window

SteveBayCommented:
Try this:
procedure TForm1.Button1Click(Sender: TObject);
var FieldDef : TFieldDef;
begin
     FieldDef := AdsTable1.FieldDefs.Find('Field2');
     if FieldDef <> nil then
          ShowMessage(FieldDef.Name);
end;

Open in new window

Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

TonyDelphiHuntAuthor Commented:
Hi,
Thank you SteveBay, that raises an exception if not found.

Best Wishes

Tony Hunt.
Mike LittlewoodEngineerCommented:
> I have also tried Table.GetFieldNames(ListBox1.Items);  

Don't do it via the table/query object if you want the full list of field names from a table, do it on the database connection of the query/table

   Database1.GetFieldNames( 'table1', ListBox1.Items);
   ADOConnection1.GetFieldNames( 'table1', ListBox1.Items);
TonyDelphiHuntAuthor Commented:
Thanks everybody,

THIS works.... With and without fields being defined in the fields editor...


     if Table.FieldDefs.IndexOf('User1') > -1 then
       ShowMessage('Found')
     else
       ShowMessage('NOT Found') ;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.