Problem with TField data type. lazarus / 63-Database ---------------------------------------------------------------------- MakorDal · 2016-01-14 05:42 I have some problem defining the fields of a TmemDataset. I want to use a memDataset with some files to keep and manipulate the data for a small personal project. here are the code snipet I use to create the fields : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function createField(fieldName, fieldLabel : String ; fieldType : TFieldType;                    group : Integer) : TField;begin  Result := TField.Create(nil);  Result.FieldName := fieldName;  Result.SetFieldType(fieldType);  Result.FieldKind := fkData;  if fieldLabel = '' then    Result.DisplayLabel := fieldName  else    Result.DisplayLabel := fieldLabel;  result.DataSet := memDataset;   Result.Tag:= group; end;    The call is quite simple : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i createField('FIELDNAME', 'FieldDisplay', ftInteger, 2);Unfortunately, this does not properly set the field type and compromise the whole data editing afterwards. I get a ftUnknown field type. How can I set the proper type for my fields ? ---------------------------------------------------------------------- balazsszekely · 2016-01-14 10:25 Try this: 1. Add memds, db to the uses clauses 2. Drop a TMemDataSet, TMemo and a TButton to your form 3. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);var  I, Counter: Integer;begin  with MemDataset1 do  begin    //create fields    FieldDefs.Clear;    FieldDefs.Add('FirstName', ftString, 16, False);    FieldDefs.Add('LastName', ftString, 16, False);    FieldDefs.Add('BirthDate', ftDateTime, 0, False);    CreateTable;    Open;     //add data    Append;    FieldByName('FirstName').AsString := 'John';    FieldByName('LastName').AsString := 'Smith';    FieldByName('BirthDate').AsDateTime := EncodeDate(1994, 12, 14);    Post;     Append;    FieldByName('FirstName').AsString := 'Free';    FieldByName('LastName').AsString := 'Pascal';    FieldByName('BirthDate').AsDateTime := EncodeDate(1970, 06, 21);    Post;     //display data    First;    Counter := 0;    Memo1.Clear;  while not EOF do    begin      Inc(Counter);      for I := 0 to Fields.Count - 1 do      begin       // if Fields[I].FieldDef.DataType  = ftDateTime then        if Fields[I].DataType  = ftDateTime then        Memo1.Lines.Add(Fields[I].FieldName + ' : ' + FormatDateTime('YYYY.MM.DD', Fields[I].AsDateTime))        else        Memo1.Lines.Add(Fields[I].FieldName + ' : ' + Fields[I].AsString);    end;      Memo1.Lines.Add('');      Next;    end;    Close;  end;end; PS: Edited, see @howardpc's suggestion. ---------------------------------------------------------------------- howardpc · 2016-01-14 11:00 minor glitch (at least with Laz 1.4.4) - line 37 should be Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i if Fields[I].DataType  = ftDateTime then ---------------------------------------------------------------------- balazsszekely · 2016-01-14 11:11 Quote@howardpc minor glitch (at least with Laz 1.4.4) - line 37 should be:     if Fields.DataType  = ftDateTime then Thank you Howard! I edited my post. ---------------------------------------------------------------------- MakorDal · 2016-01-14 16:43 Thanks... Except your solution allows me neither to set a field display name - I found a workaround - nor a tag, which is quite important to my project. ---------------------------------------------------------------------- howardpc · 2016-01-14 19:18 You could devise a routine of this sort: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses db; procedure AddField(aDSet: TDataSet; const aFName, aFLabel: string;  aFType: TFieldType; aSize, aGroup: integer; aRequired: boolean=False);var  fld: TField;  fldDef: TFieldDef;begin  aDSet.Close;  fldDef:=aDSet.FieldDefs.AddFieldDef;  with fldDef do begin    Name:=aFName;    if (aFLabel = '') then      DisplayName:=aFName    else      DisplayName:=aFLabel;    DataType:=aFType;    Size:=aSize;    Required:=aRequired;  end;  fld:=fldDef.CreateField(aDSet);  fld.Tag:=aGroup;  aDSet.FieldDefs.Update;end; which you might call thus: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   AddField(MemDataset1, 'Extra', 'Extra Field', ftString, 30, 99); ---------------------------------------------------------------------- MakorDal · 2016-01-15 17:52 Thanks, this works ! I had decided not to use fielddef at first because it did not support two memDataSet with similar field name. I had error message saying that "a field with this name already exists"