Pages

Thursday, 14 February 2013

How to use FieldName and FieldValue in Delphi XE2?

How to use FieldName and FieldValue in Delphi XE2?

In this delphi programming tutorial, we will come to know how to use FieldName and FieldValue in delphi. Sometimes while coding in delphi, we need to get the field or column name of the table depending upon some certain conditions. In this case FieldName and FieldValue come into play.

In the following delphi code, we have created a delphi function which will fetch various columns from a table in database and will return the name of that column. For example, following delphi program gets ColA, ColB, ColC, ColD from a table named tblABC. After getting all the columns, it iterate through the column list and finds out which one is null and returns it. If it finds no column which is null, it returns empty string.

(Note: ColA, ColB, ColC, ColD are numeric columns, so I am checking for null. If your columns are varchar, you can use '' for checking empty column)

Delphi Program to show usage of FieldName and FieldValue
 
function TForm.MyDelphiFunction : String;
var
  i : integer;
begin
  try
    Result := '';
    with qryABC do
    begin
      Close;
      SQL.Clear;
      SQL.Text := 'SELECT ColA, ColB, ColC, ColD FROM tblABC WHERE ID = :ID';
      ParamByName('ID').AsString := anyID;
      Open;
      if RecordCount > 0 then
      begin
        for i:=0 to FieldCount -1 do
        begin
          if (Fields[i].Value) = Null then
          begin
             Result := Fields[i].FieldName;
             Break;
          end;
        end;
      end;
      Close;
    end;
  except
    on E : Exception do
    begin
      Showmessage('Error occured in function MyDelphiFunction: ' + E.Message);
      Result := '';
    end;
  end;
end;

No comments:

Post a Comment

About the Author

I have more than 10 years of experience in IT industry. Linkedin Profile

I am currently messing up with neural networks in deep learning. I am learning Python, TensorFlow and Keras.

Author: I am an author of a book on deep learning.

Quiz: I run an online quiz on machine learning and deep learning.