Reputation: 134
The scenario is I have an IbQuery that is dynamically created with many possible variations of fields. After its execution I want to test to see if one specific field was included and has data. Something similar to this:
ibqry.fetchall();
while not ibqry.eof do
begin
if (ibqry.FieldByName(‘Lastname’) <> null)
lname := ibqry. FieldByName(‘Lastname’).tostring()
else
lname := ‘’
ect....
end
How can I do this. Thanks.
Upvotes: 1
Views: 523
Reputation: 125708
TDataSet.FieldByName
raises an exception if you try to access a field that isn't in the dataset.
You can use TDataSet.FindField
instead. It returns a reference to the field if it is present, or nil
if it isn't. Something like this should work for you:
var
LastNameField: TField;
begin
LastNameField := ibqry.FindField('LastName');
ibqry.fetchall();
while not ibqry.Eof do
begin
lName := '';
if Assigned(LastNameField) then
if not LastNameField.IsNull then
lName := LastNameField.AsString;
// Do whatever with query content
ibqry.Next;
end;
end;
Upvotes: 1