{"id":1572,"date":"2014-06-24T14:53:20","date_gmt":"2014-06-24T04:53:20","guid":{"rendered":"http:\/\/www.malcolmgroves.com\/blog\/?p=1572"},"modified":"2014-06-25T04:38:00","modified_gmt":"2014-06-24T18:38:00","slug":"no-code-calculated-fields-in-rad-studio-and-appmethod","status":"publish","type":"post","link":"http:\/\/www.malcolmgroves.com\/blog\/?p=1572","title":{"rendered":"&#8220;No Code&#8221; Calculated Fields in RAD Studio and AppMethod"},"content":{"rendered":"<p>In Perth last week I was showing the new <a href=\"http:\/\/docwiki.embarcadero.com\/Libraries\/en\/Data.DB.TFieldOptions\" target=\"_blank\">FieldOptions property<\/a> on datasets. As part of the demo, I had to create a calculated field, and when I brought up the New Field dialog an audience member suggested I create an InternalCalc field instead.<\/p>\n<p>I&#8217;d never heard of an InternalCalc field at the time, so later I did some research into what they are. In <a href=\"http:\/\/www.embarcadero.com\/products\/rad-studio\/firedac\" target=\"_blank\">FireDAC<\/a> they do everything a normal Calculated Field does, but in addition, you can define the value using the DefaultExpression property of the field, rather than having to write an OnCalcFields event handler.<\/p>\n<p><!--more--><\/p>\n<p><a href=\"http:\/\/www.malcolmgroves.com\/blog\/wp-content\/uploads\/2014\/06\/Screen-Shot-2014-06-24-at-14.01.44.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1574 size-full\" src=\"http:\/\/www.malcolmgroves.com\/blog\/wp-content\/uploads\/2014\/06\/Screen-Shot-2014-06-24-at-14.01.44.png\" alt=\"The New Field dialog, showing the 5 different types of fields you can create\" width=\"518\" height=\"325\" srcset=\"http:\/\/www.malcolmgroves.com\/blog\/wp-content\/uploads\/2014\/06\/Screen-Shot-2014-06-24-at-14.01.44.png 518w, http:\/\/www.malcolmgroves.com\/blog\/wp-content\/uploads\/2014\/06\/Screen-Shot-2014-06-24-at-14.01.44-300x188.png 300w\" sizes=\"auto, (max-width: 518px) 100vw, 518px\" \/><\/a><\/p>\n<p>The New Field dialog, showing the 5 different types of fields you can create<\/p>\n<p>In the Object Inspector screen shot below, you can see I have a TStringField with its FieldKind set to fkInternalCalc and the DefaultExpression set to:<\/p>\n<p><code>LAST_NAME + ', ' + FIRST_NAME<\/code><\/p>\n<div id=\"attachment_1573\" style=\"width: 343px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/www.malcolmgroves.com\/blog\/wp-content\/uploads\/2014\/06\/Screen-Shot-2014-06-24-at-14.00.23.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1573\" class=\"wp-image-1573 size-full\" src=\"http:\/\/www.malcolmgroves.com\/blog\/wp-content\/uploads\/2014\/06\/Screen-Shot-2014-06-24-at-14.00.23.png\" alt=\"Object Inspector showing the DefaultExpression and FieldKind properties of my InternalCalc field\" width=\"333\" height=\"251\" srcset=\"http:\/\/www.malcolmgroves.com\/blog\/wp-content\/uploads\/2014\/06\/Screen-Shot-2014-06-24-at-14.00.23.png 333w, http:\/\/www.malcolmgroves.com\/blog\/wp-content\/uploads\/2014\/06\/Screen-Shot-2014-06-24-at-14.00.23-300x226.png 300w\" sizes=\"auto, (max-width: 333px) 100vw, 333px\" \/><\/a><p id=\"caption-attachment-1573\" class=\"wp-caption-text\">Object Inspector showing the DefaultExpression and FieldKind properties of my InternalCalc field<\/p><\/div>\n<p>This not only lets me avoid adding code for simple field calculations, but further, the calculated value is visible at design-time, unlike normal calculated fields.<\/p>\n<div id=\"attachment_1575\" style=\"width: 376px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/www.malcolmgroves.com\/blog\/wp-content\/uploads\/2014\/06\/Screen-Shot-2014-06-24-at-14.05.02.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1575\" class=\"wp-image-1575 size-full\" src=\"http:\/\/www.malcolmgroves.com\/blog\/wp-content\/uploads\/2014\/06\/Screen-Shot-2014-06-24-at-14.05.02.png\" alt=\"The InternalCalc field at design-time\" width=\"366\" height=\"295\" srcset=\"http:\/\/www.malcolmgroves.com\/blog\/wp-content\/uploads\/2014\/06\/Screen-Shot-2014-06-24-at-14.05.02.png 366w, http:\/\/www.malcolmgroves.com\/blog\/wp-content\/uploads\/2014\/06\/Screen-Shot-2014-06-24-at-14.05.02-300x241.png 300w\" sizes=\"auto, (max-width: 366px) 100vw, 366px\" \/><\/a><p id=\"caption-attachment-1575\" class=\"wp-caption-text\">The InternalCalc field at design-time<\/p><\/div>\n<p>There are more details\u00a0in the docs <a href=\"http:\/\/docwiki.embarcadero.com\/RADStudio\/en\/Calculated_and_Aggregated_Fields_(FireDAC)#Expression_Calculated_Fields\" target=\"_blank\">here<\/a>\u00a0and more details on writing the expressions <a href=\"http:\/\/docwiki.embarcadero.com\/RADStudio\/XE6\/en\/Writing_Expressions_(FireDAC)\" target=\"_blank\">here<\/a>. On the same page it discusses Aggregated Fields, something else I haven&#8217;t played with. Have to add it to my list.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Perth last week I was showing the new FieldOptions property on datasets. As part of the demo, I had to create a calculated field, and when I brought up the New Field dialog an audience member suggested I create an InternalCalc field instead. I&#8217;d never heard of an InternalCalc field at the time, so [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101,120],"tags":[103,19,48,102],"class_list":["post-1572","post","type-post","status-publish","format-standard","hentry","category-db","category-firemonkey","tag-appmethod","tag-delphi","tag-embarcadero","tag-firedac"],"_links":{"self":[{"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1572","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1572"}],"version-history":[{"count":7,"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1572\/revisions"}],"predecessor-version":[{"id":1582,"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1572\/revisions\/1582"}],"wp:attachment":[{"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1572"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1572"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1572"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}