Forum > Database
Problem with TField data type.
MakorDal:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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:
Try this:
1. Add memds, db to the uses clauses
2. Drop a TMemDataSet, TMemo and a TButton to your form
3.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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:
minor glitch (at least with Laz 1.4.4) - line 37 should be
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---if Fields[I].DataType = ftDateTime then
GetMem:
--- Quote ---@howardpc
minor glitch (at least with Laz 1.4.4) - line 37 should be:
if Fields.DataType = ftDateTime then
--- End quote ---
Thank you Howard! I edited my post.
MakorDal:
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.
Navigation
[0] Message Index
[#] Next page