Recent

Author Topic: Problem with TField data type.  (Read 5640 times)

MakorDal

  • Newbie
  • Posts: 3
Problem with TField data type.
« on: January 14, 2016, 06:42:10 am »
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][+][-]
  1. function createField(fieldName, fieldLabel : String ; fieldType : TFieldType;
  2.                      group : Integer) : TField;
  3. begin
  4.   Result := TField.Create(nil);
  5.   Result.FieldName := fieldName;
  6.   Result.SetFieldType(fieldType);
  7.   Result.FieldKind := fkData;
  8.   if fieldLabel = '' then
  9.     Result.DisplayLabel := fieldName
  10.   else
  11.     Result.DisplayLabel := fieldLabel;
  12.   result.DataSet := memDataset;
  13.  
  14.   Result.Tag:= group;
  15.  
  16. end;    
The call is quite simple :

Code: Pascal  [Select][+][-]
  1. 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 ?

GetMem

  • Hero Member
  • *****
  • Posts: 3782
Re: Problem with TField data type.
« Reply #1 on: January 14, 2016, 11:25:13 am »
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][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   I, Counter: Integer;
  4. begin
  5.   with MemDataset1 do
  6.   begin
  7.     //create fields
  8.     FieldDefs.Clear;
  9.     FieldDefs.Add('FirstName', ftString, 16, False);
  10.     FieldDefs.Add('LastName', ftString, 16, False);
  11.     FieldDefs.Add('BirthDate', ftDateTime, 0, False);
  12.     CreateTable;
  13.     Open;
  14.  
  15.     //add data
  16.     Append;
  17.     FieldByName('FirstName').AsString := 'John';
  18.     FieldByName('LastName').AsString := 'Smith';
  19.     FieldByName('BirthDate').AsDateTime := EncodeDate(1994, 12, 14);
  20.     Post;
  21.  
  22.     Append;
  23.     FieldByName('FirstName').AsString := 'Free';
  24.     FieldByName('LastName').AsString := 'Pascal';
  25.     FieldByName('BirthDate').AsDateTime := EncodeDate(1970, 06, 21);
  26.     Post;
  27.  
  28.     //display data
  29.     First;
  30.     Counter := 0;
  31.     Memo1.Clear;
  32.     while not EOF do
  33.     begin
  34.       Inc(Counter);
  35.       for I := 0 to Fields.Count - 1 do
  36.       begin
  37.        // if Fields[I].FieldDef.DataType  = ftDateTime then
  38.         if Fields[I].DataType  = ftDateTime then
  39.           Memo1.Lines.Add(Fields[I].FieldName + ' : ' + FormatDateTime('YYYY.MM.DD', Fields[I].AsDateTime))
  40.         else
  41.           Memo1.Lines.Add(Fields[I].FieldName + ' : ' + Fields[I].AsString);
  42.       end;
  43.       Memo1.Lines.Add('');
  44.       Next;
  45.     end;
  46.     Close;
  47.   end;
  48. end;

PS: Edited, see @howardpc's suggestion.
« Last Edit: January 14, 2016, 12:10:46 pm by GetMem »

howardpc

  • Hero Member
  • *****
  • Posts: 3998
Re: Problem with TField data type.
« Reply #2 on: January 14, 2016, 12:00:01 pm »
minor glitch (at least with Laz 1.4.4) - line 37 should be
Code: Pascal  [Select][+][-]
  1. if Fields[I].DataType  = ftDateTime then

GetMem

  • Hero Member
  • *****
  • Posts: 3782
Re: Problem with TField data type.
« Reply #3 on: January 14, 2016, 12:11:00 pm »
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

  • Newbie
  • Posts: 3
Re: Problem with TField data type.
« Reply #4 on: January 14, 2016, 05:43:51 pm »
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

  • Hero Member
  • *****
  • Posts: 3998
Re: Problem with TField data type.
« Reply #5 on: January 14, 2016, 08:18:58 pm »
You could devise a routine of this sort:

Code: Pascal  [Select][+][-]
  1. uses db;
  2.  
  3. procedure AddField(aDSet: TDataSet; const aFName, aFLabel: string;
  4.   aFType: TFieldType; aSize, aGroup: integer; aRequired: boolean=False);
  5. var
  6.   fld: TField;
  7.   fldDef: TFieldDef;
  8. begin
  9.   aDSet.Close;
  10.   fldDef:=aDSet.FieldDefs.AddFieldDef;
  11.   with fldDef do begin
  12.     Name:=aFName;
  13.     if (aFLabel = '') then
  14.       DisplayName:=aFName
  15.     else
  16.       DisplayName:=aFLabel;
  17.     DataType:=aFType;
  18.     Size:=aSize;
  19.     Required:=aRequired;
  20.   end;
  21.   fld:=fldDef.CreateField(aDSet);
  22.   fld.Tag:=aGroup;
  23.   aDSet.FieldDefs.Update;
  24. end;  

which you might call thus:
Code: Pascal  [Select][+][-]
  1.   AddField(MemDataset1, 'Extra', 'Extra Field', ftString, 30, 99);

MakorDal

  • Newbie
  • Posts: 3
Re: Problem with TField data type.
« Reply #6 on: January 15, 2016, 06:52:26 pm »
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"

 

TinyPortal © 2005-2018